In this demo, you'll run publisher and subscriber microservices locally, and send/receive messages using Solace Cloud, to demonstrate how Dapr enables a publish-subscribe pattern.
The publisher service (checkout
) will generate messages and send these to a specific topic (topic:marc/orders
), while the subscriber service (order-processor
) will listen for messages at the same topic. See the Dapr pub/sub overview to understand when this pattern might be a good choice for your software architecture.
With Dapr, the application code is using set of common building block APIs. The pub/sub API is such a building block. Dapr allows to switch between different pub/sub implementations by using component files. These are yaml files that contain resource specific information. This means the application code is not coupled to the implementation of the pub/sub resource. In this demo the Solace AMQP component is used.
- .NET 6 SDK
- Dapr CLI
- Solace Cloud account. If you don't have one, you can sign up for a free trial.
- In the Solace Cloud portal, go to Cluster Manager and create a new service.
- Provide a service name, service type, and choose a cloud where the service will be deployed.
- Click Create Service and wait until the service is deployed.
- Once the service is deployed, navigate to the Connect tab of the service and expand the AMQP section. Note the information underneath the Connection Details. This will be needed in the Dapr pubsub component file in the next step.
-
Clone this repository locally and open it in your IDE.
-
Rename the
resources/pubsub.yaml.template
file toresources/pubsub.yaml
. This is the Dapr pub/sub component file that will contain the details to connect to Solace Cloud.The
pubsub.yaml
file has been added to.gitignore
to prevent accidental check-in of credentials for this demo. For production use, the yaml files should be checked into source control and secret store references should be used, instead of plain text values. -
Copy the username, password, and the secured AMQP host values from the Solace AMQP Connection Details to the
pubsub.yaml
file.The result should look like this:
apiVersion: dapr.io/v1alpha1 kind: Component metadata: name: pubsub-component spec: type: pubsub.solace.amqp version: v1 metadata: - name: url value: "amqps://<SOLACE_SECURED_AMQP_HOST>" - name: username value: "solace-cloud-client" - name: password value: "<SOLACE_PASSWORD>"
-
Open a terminal, navigate to the
order-processor
directory and build the project:cd ./order-processor dotnet build
-
Run the .NET subscriber app with Dapr:
dapr run --app-id order-processor --resources-path ../resources/ --app-port 7002 -- dotnet run
-
Open another terminal, navigate to the
checkout
directory and build the project:cd ./checkout dotnet restore dotnet build
-
Run the .NET publisher app with Dapr:
dapr run --app-id checkout-sdk --resources-path ../resources/ -- dotnet run
-
The following output should be visible in the terminals:
Publisher output:
== APP == Published data: Order { OrderId = 1 } == APP == Published data: Order { OrderId = 2 } == APP == Published data: Order { OrderId = 3 } == APP == Published data: Order { OrderId = 4 } == APP == Published data: Order { OrderId = 5 } == APP == Published data: Order { OrderId = 6 } == APP == Published data: Order { OrderId = 7 } == APP == Published data: Order { OrderId = 8 } == APP == Published data: Order { OrderId = 9 } == APP == Published data: Order { OrderId = 10 }
Subscriber output:
== APP == Subscriber received : Order { OrderId = 1 } == APP == Subscriber received : Order { OrderId = 2 } == APP == Subscriber received : Order { OrderId = 3 } == APP == Subscriber received : Order { OrderId = 4 } == APP == Subscriber received : Order { OrderId = 5 } == APP == Subscriber received : Order { OrderId = 6 } == APP == Subscriber received : Order { OrderId = 7 } == APP == Subscriber received : Order { OrderId = 8 } == APP == Subscriber received : Order { OrderId = 9 } == APP == Subscriber received : Order { OrderId = 10 }