Quickstart: Publish and Subscribe

Get started with Dapr’s Publish and Subscribe building block

Let’s take a look at Dapr’s Publish and Subscribe (Pub/sub) building block. In this Quickstart, you will run a publisher microservice and a subscriber microservice to demonstrate how Dapr enables a Pub/sub pattern.

  1. Using a publisher service, developers can repeatedly publish messages to a topic.
  2. A Pub/sub component queues or brokers those messages. Our example below uses Redis, you can use RabbitMQ, Kafka, etc.
  3. The subscriber to that topic pulls messages from the queue and processes them.

Publish and Subscribe - 图1

You can try out this pub/sub quickstart by either:

Run using Multi-App Run

Select your preferred language-specific Dapr SDK before proceeding with the Quickstart.

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

From the root of the Quickstarts directory, navigate into the pub/sub directory:

  1. cd pub_sub/python/sdk

Install the dependencies for the order-processor and checkout apps:

  1. cd ./checkout
  2. pip3 install -r requirements.txt
  3. cd ..
  4. cd ./order-processor
  5. pip3 install -r requirements.txt
  6. cd ..
  7. cd ./order-processor-fastapi
  8. pip3 install -r requirements.txt
  9. cd ..

Step 3: Run the publisher and subscriber

With the following command, simultaneously run the following services alongside their own Dapr sidecars:

  • The order-processor subscriber
  • The checkout publisher
  1. dapr run -f .

Note: Since Python3.exe is not defined in Windows, you may need to change python3 to python in the dapr.yaml file before running dapr run -f .

Expected output

  1. == APP - checkout-sdk == Published data: Order { OrderId = 1 }
  2. == APP - order-processor == Subscriber received : Order { OrderId = 1 }
  3. == APP - checkout-sdk == Published data: Order { OrderId = 2 }
  4. == APP - order-processor == Subscriber received : Order { OrderId = 2 }
  5. == APP - checkout-sdk == Published data: Order { OrderId = 3 }
  6. == APP - order-processor == Subscriber received : Order { OrderId = 3 }
  7. == APP - checkout-sdk == Published data: Order { OrderId = 4 }
  8. == APP - order-processor == Subscriber received : Order { OrderId = 4 }
  9. == APP - checkout-sdk == Published data: Order { OrderId = 5 }
  10. == APP - order-processor == Subscriber received : Order { OrderId = 5 }
  11. == APP - checkout-sdk == Published data: Order { OrderId = 6 }
  12. == APP - order-processor == Subscriber received : Order { OrderId = 6 }
  13. == APP - checkout-sdk == Published data: Order { OrderId = 7 }
  14. == APP - order-processor == Subscriber received : Order { OrderId = 7 }
  15. == APP - checkout-sdk == Published data: Order { OrderId = 8 }
  16. == APP - order-processor == Subscriber received : Order { OrderId = 8 }
  17. == APP - checkout-sdk == Published data: Order { OrderId = 9 }
  18. == APP - order-processor == Subscriber received : Order { OrderId = 9 }
  19. == APP - checkout-sdk == Published data: Order { OrderId = 10 }
  20. == APP - order-processor == Subscriber received : Order { OrderId = 10 }
  21. Exited App successfully

What happened?

When you ran dapr init during Dapr install, the following YAML files were generated in the .dapr/components directory:

Running dapr run -f . in this Quickstart started both the subscriber and publisher applications.

dapr.yaml Multi-App Run template file

Running the Multi-App Run template file with dapr run -f . starts all applications in your project. In this Quickstart, the dapr.yaml file contains the following:

  1. version: 1
  2. common:
  3. resourcesPath: ../../components/
  4. apps:
  5. - appID: order-processor-sdk
  6. appDirPath: ./order-processor/
  7. appPort: 6001
  8. command: ["uvicorn", "app:app"]
  9. - appID: checkout-sdk
  10. appDirPath: ./checkout/
  11. command: ["python3", "app.py"]
pubsub.yaml component file

With the pubsub.yaml component, you can easily swap out underlying components without application code changes.

The Redis pubsub.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: orderpubsub
  5. spec:
  6. type: pubsub.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""

In the component YAML file:

  • metadata/name is how your application talks to the component.
  • spec/metadata defines the connection to the instance of the component.
  • scopes specify which application can use the component.
order-processor subscriber

In the order-processor subscriber, you subscribe to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. This enables your app code to talk to the Redis component instance through the Dapr sidecar.

  1. # Register Dapr pub/sub subscriptions
  2. @app.route('/dapr/subscribe', methods=['GET'])
  3. def subscribe():
  4. subscriptions = [{
  5. 'pubsubname': 'orderpubsub',
  6. 'topic': 'orders',
  7. 'route': 'orders'
  8. }]
  9. print('Dapr pub/sub is subscribed to: ' + json.dumps(subscriptions))
  10. return jsonify(subscriptions)
  11. # Dapr subscription in /dapr/subscribe sets up this route
  12. @app.route('/orders', methods=['POST'])
  13. def orders_subscriber():
  14. event = from_http(request.headers, request.get_data())
  15. print('Subscriber received : ' + event.data['orderid'], flush=True)
  16. return json.dumps({'success': True}), 200, {
  17. 'ContentType': 'application/json'}
  18. app.run(port=5001)
checkout publisher

In the checkout publisher, you publish the orderId message to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. As soon as the service starts, it publishes in a loop:

  1. with DaprClient() as client:
  2. # Publish an event/message using Dapr PubSub
  3. result = client.publish_event(
  4. pubsub_name='orderpubsub',
  5. topic_name='orders',
  6. data=json.dumps(order),
  7. data_content_type='application/json',
  8. )

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

From the root of the Quickstarts directory, navigate into the pub/sub directory:

  1. cd pub_sub/javascript/sdk

Install the dependencies for the order-processor and checkout apps:

  1. cd ./order-processor
  2. npm install
  3. cd ..
  4. cd ./checkout
  5. npm install
  6. cd ..

Step 3: Run the publisher and subscriber

With the following command, simultaneously run the following services alongside their own Dapr sidecars:

  • The order-processor subscriber
  • The checkout publisher
  1. dapr run -f .

Expected output

  1. == APP - checkout-sdk == Published data: Order { OrderId = 1 }
  2. == APP - order-processor == Subscriber received : Order { OrderId = 1 }
  3. == APP - checkout-sdk == Published data: Order { OrderId = 2 }
  4. == APP - order-processor == Subscriber received : Order { OrderId = 2 }
  5. == APP - checkout-sdk == Published data: Order { OrderId = 3 }
  6. == APP - order-processor == Subscriber received : Order { OrderId = 3 }
  7. == APP - checkout-sdk == Published data: Order { OrderId = 4 }
  8. == APP - order-processor == Subscriber received : Order { OrderId = 4 }
  9. == APP - checkout-sdk == Published data: Order { OrderId = 5 }
  10. == APP - order-processor == Subscriber received : Order { OrderId = 5 }
  11. == APP - checkout-sdk == Published data: Order { OrderId = 6 }
  12. == APP - order-processor == Subscriber received : Order { OrderId = 6 }
  13. == APP - checkout-sdk == Published data: Order { OrderId = 7 }
  14. == APP - order-processor == Subscriber received : Order { OrderId = 7 }
  15. == APP - checkout-sdk == Published data: Order { OrderId = 8 }
  16. == APP - order-processor == Subscriber received : Order { OrderId = 8 }
  17. == APP - checkout-sdk == Published data: Order { OrderId = 9 }
  18. == APP - order-processor == Subscriber received : Order { OrderId = 9 }
  19. == APP - checkout-sdk == Published data: Order { OrderId = 10 }
  20. == APP - order-processor == Subscriber received : Order { OrderId = 10 }
  21. Exited App successfully

What happened?

When you ran dapr init during Dapr install, the following YAML files were generated in the .dapr/components directory:

Running dapr run -f . in this Quickstart started both the subscriber and publisher applications.

dapr.yaml Multi-App Run template file

Running the Multi-App Run template file with dapr run -f . starts all applications in your project. In this Quickstart, the dapr.yaml file contains the following:

  1. version: 1
  2. common:
  3. resourcesPath: ../../components/
  4. apps:
  5. - appID: order-processor
  6. appDirPath: ./order-processor/
  7. appPort: 5002
  8. command: ["npm", "run", "start"]
  9. - appID: checkout-sdk
  10. appDirPath: ./checkout/
  11. command: ["npm", "run", "start"]
pubsub.yaml component file

With the pubsub.yaml component, you can easily swap out underlying components without application code changes.

The Redis pubsub.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: orderpubsub
  5. spec:
  6. type: pubsub.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""

In the component YAML file:

  • metadata/name is how your application talks to the component.
  • spec/metadata defines the connection to the instance of the component.
  • scopes specify which application can use the component.
order-processor subscriber

In the order-processor subscriber, you subscribe to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. This enables your app code to talk to the Redis component instance through the Dapr sidecar.

  1. server.pubsub.subscribe("orderpubsub", "orders", (data) => console.log("Subscriber received: " + JSON.stringify(data)));
checkout publisher

In the checkout publisher service, you publish the orderId message to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. As soon as the service starts, it publishes in a loop:

  1. const client = new DaprClient();
  2. await client.pubsub.publish(PUBSUB_NAME, PUBSUB_TOPIC, order);
  3. console.log("Published data: " + JSON.stringify(order));

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

From the root of the Quickstarts directory, navigate into the pub/sub directory:

  1. cd pub_sub/csharp/sdk

Install the dependencies for the order-processor and checkout apps:

  1. cd ./order-processor
  2. dotnet restore
  3. dotnet build
  4. cd ../checkout
  5. dotnet restore
  6. dotnet build
  7. cd ..

Step 3: Run the publisher and subscriber

With the following command, simultaneously run the following services alongside their own Dapr sidecars:

  • The order-processor subscriber
  • The checkout publisher
  1. dapr run -f .

Expected output

  1. == APP - checkout-sdk == Published data: Order { OrderId = 1 }
  2. == APP - order-processor == Subscriber received : Order { OrderId = 1 }
  3. == APP - checkout-sdk == Published data: Order { OrderId = 2 }
  4. == APP - order-processor == Subscriber received : Order { OrderId = 2 }
  5. == APP - checkout-sdk == Published data: Order { OrderId = 3 }
  6. == APP - order-processor == Subscriber received : Order { OrderId = 3 }
  7. == APP - checkout-sdk == Published data: Order { OrderId = 4 }
  8. == APP - order-processor == Subscriber received : Order { OrderId = 4 }
  9. == APP - checkout-sdk == Published data: Order { OrderId = 5 }
  10. == APP - order-processor == Subscriber received : Order { OrderId = 5 }
  11. == APP - checkout-sdk == Published data: Order { OrderId = 6 }
  12. == APP - order-processor == Subscriber received : Order { OrderId = 6 }
  13. == APP - checkout-sdk == Published data: Order { OrderId = 7 }
  14. == APP - order-processor == Subscriber received : Order { OrderId = 7 }
  15. == APP - checkout-sdk == Published data: Order { OrderId = 8 }
  16. == APP - order-processor == Subscriber received : Order { OrderId = 8 }
  17. == APP - checkout-sdk == Published data: Order { OrderId = 9 }
  18. == APP - order-processor == Subscriber received : Order { OrderId = 9 }
  19. == APP - checkout-sdk == Published data: Order { OrderId = 10 }
  20. == APP - order-processor == Subscriber received : Order { OrderId = 10 }
  21. Exited App successfully

What happened?

When you ran dapr init during Dapr install, the following YAML files were generated in the .dapr/components directory:

Running dapr run -f . in this Quickstart started both the subscriber and publisher applications.

dapr.yaml Multi-App Run template file

Running the Multi-App Run template file with dapr run -f . starts all applications in your project. In this Quickstart, the dapr.yaml file contains the following:

  1. version: 1
  2. common:
  3. resourcesPath: ../../components/
  4. apps:
  5. - appID: order-processor
  6. appDirPath: ./order-processor/
  7. appPort: 7006
  8. command: ["dotnet", "run"]
  9. - appID: checkout-sdk
  10. appDirPath: ./checkout/
  11. command: ["dotnet", "run"]
pubsub.yaml component file

With the pubsub.yaml component, you can easily swap out underlying components without application code changes.

The Redis pubsub.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: orderpubsub
  5. spec:
  6. type: pubsub.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""

In the component YAML file:

  • metadata/name is how your application talks to the component.
  • spec/metadata defines the connection to the instance of the component.
  • scopes specify which application can use the component.
order-processor subscriber

In the order-processor subscriber, you subscribe to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. This enables your app code to talk to the Redis component instance through the Dapr sidecar.

  1. // Dapr subscription in [Topic] routes orders topic to this route
  2. app.MapPost("/orders", [Topic("orderpubsub", "orders")] (Order order) => {
  3. Console.WriteLine("Subscriber received : " + order);
  4. return Results.Ok(order);
  5. });
  6. public record Order([property: JsonPropertyName("orderId")] int OrderId);
checkout publisher

In the checkout publisher, you publish the orderId message to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. As soon as the service starts, it publishes in a loop:

  1. using var client = new DaprClientBuilder().Build();
  2. await client.PublishEventAsync("orderpubsub", "orders", order);
  3. Console.WriteLine("Published data: " + order);

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

From the root of the Quickstarts directory, navigate into the pub/sub directory:

  1. cd pub_sub/java/sdk

Install the dependencies for the order-processor and checkout apps:

  1. cd ./order-processor
  2. mvn clean install
  3. cd ..
  4. cd ./checkout
  5. mvn clean install
  6. cd ..

Step 3: Run the publisher and subscriber

With the following command, simultaneously run the following services alongside their own Dapr sidecars:

  • The order-processor subscriber
  • The checkout publisher
  1. dapr run -f .

Expected output

  1. == APP - checkout-sdk == Published data: Order { OrderId = 1 }
  2. == APP - order-processor == Subscriber received : Order { OrderId = 1 }
  3. == APP - checkout-sdk == Published data: Order { OrderId = 2 }
  4. == APP - order-processor == Subscriber received : Order { OrderId = 2 }
  5. == APP - checkout-sdk == Published data: Order { OrderId = 3 }
  6. == APP - order-processor == Subscriber received : Order { OrderId = 3 }
  7. == APP - checkout-sdk == Published data: Order { OrderId = 4 }
  8. == APP - order-processor == Subscriber received : Order { OrderId = 4 }
  9. == APP - checkout-sdk == Published data: Order { OrderId = 5 }
  10. == APP - order-processor == Subscriber received : Order { OrderId = 5 }
  11. == APP - checkout-sdk == Published data: Order { OrderId = 6 }
  12. == APP - order-processor == Subscriber received : Order { OrderId = 6 }
  13. == APP - checkout-sdk == Published data: Order { OrderId = 7 }
  14. == APP - order-processor == Subscriber received : Order { OrderId = 7 }
  15. == APP - checkout-sdk == Published data: Order { OrderId = 8 }
  16. == APP - order-processor == Subscriber received : Order { OrderId = 8 }
  17. == APP - checkout-sdk == Published data: Order { OrderId = 9 }
  18. == APP - order-processor == Subscriber received : Order { OrderId = 9 }
  19. == APP - checkout-sdk == Published data: Order { OrderId = 10 }
  20. == APP - order-processor == Subscriber received : Order { OrderId = 10 }
  21. Exited App successfully

What happened?

When you ran dapr init during Dapr install, the following YAML files were generated in the .dapr/components directory:

Running dapr run -f . in this Quickstart started both the subscriber and publisher applications.

dapr.yaml Multi-App Run template file

Running the Multi-App Run template file with dapr run -f . starts all applications in your project. In this Quickstart, the dapr.yaml file contains the following:

  1. version: 1
  2. common:
  3. resourcesPath: ../../components/
  4. apps:
  5. - appID: order-processor-sdk
  6. appDirPath: ./order-processor/target/
  7. appPort: 8080
  8. command: ["java", "-jar", "OrderProcessingService-0.0.1-SNAPSHOT.jar"]
  9. - appID: checkout-sdk
  10. appDirPath: ./checkout/target/
  11. command: ["java", "-jar", "CheckoutService-0.0.1-SNAPSHOT.jar"]
pubsub.yaml component file

With the pubsub.yaml component, you can easily swap out underlying components without application code changes.

The Redis pubsub.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: orderpubsub
  5. spec:
  6. type: pubsub.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""

In the component YAML file:

  • metadata/name is how your application talks to the component.
  • spec/metadata defines the connection to the instance of the component.
  • scopes specify which application can use the component.
order-processor subscriber

In the order-processor subscriber, you subscribe to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. This enables your app code to talk to the Redis component instance through the Dapr sidecar.

  1. @Topic(name = "orders", pubsubName = "orderpubsub")
  2. @PostMapping(path = "/orders", consumes = MediaType.ALL_VALUE)
  3. public Mono<ResponseEntity> getCheckout(@RequestBody(required = false) CloudEvent<Order> cloudEvent) {
  4. return Mono.fromSupplier(() -> {
  5. try {
  6. logger.info("Subscriber received: " + cloudEvent.getData().getOrderId());
  7. return ResponseEntity.ok("SUCCESS");
  8. } catch (Exception e) {
  9. throw new RuntimeException(e);
  10. }
  11. });
  12. }
checkout publisher

In the checkout publisher, you publish the orderId message to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. As soon as the service starts, it publishes in a loop:

  1. DaprClient client = new DaprClientBuilder().build();
  2. client.publishEvent(
  3. PUBSUB_NAME,
  4. TOPIC_NAME,
  5. order).block();
  6. logger.info("Published data: " + order.getOrderId());

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

From the root of the Quickstarts directory, navigate into the pub/sub directory:

  1. cd pub_sub/go/sdk

Install the dependencies for the order-processor and checkout apps:

  1. cd ./order-processor
  2. go build .
  3. cd ../checkout
  4. go build .
  5. cd ..

Step 3: Run the publisher and subscriber

With the following command, simultaneously run the following services alongside their own Dapr sidecars:

  • The order-processor subscriber
  • The checkout publisher
  1. dapr run -f .

Expected output

  1. == APP - checkout-sdk == Published data: Order { OrderId = 1 }
  2. == APP - order-processor == Subscriber received : Order { OrderId = 1 }
  3. == APP - checkout-sdk == Published data: Order { OrderId = 2 }
  4. == APP - order-processor == Subscriber received : Order { OrderId = 2 }
  5. == APP - checkout-sdk == Published data: Order { OrderId = 3 }
  6. == APP - order-processor == Subscriber received : Order { OrderId = 3 }
  7. == APP - checkout-sdk == Published data: Order { OrderId = 4 }
  8. == APP - order-processor == Subscriber received : Order { OrderId = 4 }
  9. == APP - checkout-sdk == Published data: Order { OrderId = 5 }
  10. == APP - order-processor == Subscriber received : Order { OrderId = 5 }
  11. == APP - checkout-sdk == Published data: Order { OrderId = 6 }
  12. == APP - order-processor == Subscriber received : Order { OrderId = 6 }
  13. == APP - checkout-sdk == Published data: Order { OrderId = 7 }
  14. == APP - order-processor == Subscriber received : Order { OrderId = 7 }
  15. == APP - checkout-sdk == Published data: Order { OrderId = 8 }
  16. == APP - order-processor == Subscriber received : Order { OrderId = 8 }
  17. == APP - checkout-sdk == Published data: Order { OrderId = 9 }
  18. == APP - order-processor == Subscriber received : Order { OrderId = 9 }
  19. == APP - checkout-sdk == Published data: Order { OrderId = 10 }
  20. == APP - order-processor == Subscriber received : Order { OrderId = 10 }
  21. Exited App successfully

What happened?

When you ran dapr init during Dapr install, the following YAML files were generated in the .dapr/components directory:

Running dapr run -f . in this Quickstart started both the subscriber and publisher applications.

dapr.yaml Multi-App Run template file

Running the Multi-App Run template file with dapr run -f . starts all applications in your project. In this Quickstart, the dapr.yaml file contains the following:

  1. version: 1
  2. common:
  3. resourcesPath: ../../components/
  4. apps:
  5. - appID: order-processor
  6. appDirPath: ./order-processor/
  7. appPort: 6005
  8. command: ["go", "run", "."]
  9. - appID: checkout-sdk
  10. appDirPath: ./checkout/
  11. command: ["go", "run", "."]
pubsub.yaml component file

With the pubsub.yaml component, you can easily swap out underlying components without application code changes.

The Redis pubsub.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: orderpubsub
  5. spec:
  6. type: pubsub.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""

In the component YAML file:

  • metadata/name is how your application talks to the component.
  • spec/metadata defines the connection to the instance of the component.
  • scopes specify which application can use the component.
order-processor subscriber

In the order-processor subscriber, you subscribe to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. This enables your app code to talk to the Redis component instance through the Dapr sidecar.

  1. func eventHandler(ctx context.Context, e *common.TopicEvent) (retry bool, err error) {
  2. fmt.Println("Subscriber received: ", e.Data)
  3. return false, nil
  4. }
checkout publisher

In the checkout publisher, you publish the orderId message to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. As soon as the service starts, it publishes in a loop:

  1. client, err := dapr.NewClient()
  2. if err := client.PublishEvent(ctx, PUBSUB_NAME, PUBSUB_TOPIC, []byte(order)); err != nil {
  3. panic(err)
  4. }
  5. fmt.Println("Published data: ", order)

Run one application at a time

Select your preferred language-specific Dapr SDK before proceeding with the Quickstart.

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 3: Subscribe to topics

In a terminal window, from the root of the Quickstarts clone directory navigate to the order-processor directory.

  1. cd pub_sub/python/sdk/order-processor

Install the dependencies:

  1. pip3 install -r requirements.txt

Run the order-processor subscriber service alongside a Dapr sidecar.

  1. dapr run --app-id order-processor --resources-path ../../../components/ --app-port 6002 -- python3 app.py

Note: Since Python3.exe is not defined in Windows, you may need to use python app.py instead of python3 app.py.

In the order-processor subscriber, we’re subscribing to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. This enables your app code to talk to the Redis component instance through the Dapr sidecar.

  1. # Register Dapr pub/sub subscriptions
  2. @app.route('/dapr/subscribe', methods=['GET'])
  3. def subscribe():
  4. subscriptions = [{
  5. 'pubsubname': 'orderpubsub',
  6. 'topic': 'orders',
  7. 'route': 'orders'
  8. }]
  9. print('Dapr pub/sub is subscribed to: ' + json.dumps(subscriptions))
  10. return jsonify(subscriptions)
  11. # Dapr subscription in /dapr/subscribe sets up this route
  12. @app.route('/orders', methods=['POST'])
  13. def orders_subscriber():
  14. event = from_http(request.headers, request.get_data())
  15. print('Subscriber received : ' + event.data['orderid'], flush=True)
  16. return json.dumps({'success': True}), 200, {
  17. 'ContentType': 'application/json'}
  18. app.run(port=5001)

Step 4: Publish a topic

In a new terminal window, navigate to the checkout directory.

  1. cd pub_sub/python/sdk/checkout

Install the dependencies:

  1. pip3 install -r requirements.txt

Run the checkout publisher service alongside a Dapr sidecar.

  1. dapr run --app-id checkout --resources-path ../../../components/ -- python3 app.py

Note: Since Python3.exe is not defined in Windows, you may need to use python app.py instead of python3 app.py.

In the checkout publisher, we’re publishing the orderId message to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. As soon as the service starts, it publishes in a loop:

  1. with DaprClient() as client:
  2. # Publish an event/message using Dapr PubSub
  3. result = client.publish_event(
  4. pubsub_name='orderpubsub',
  5. topic_name='orders',
  6. data=json.dumps(order),
  7. data_content_type='application/json',
  8. )

Step 5: View the Pub/sub outputs

The publisher sends orders to the Dapr sidecar while the subscriber receives them.

Publisher output:

  1. == APP == INFO:root:Published data: {"orderId": 1}
  2. == APP == INFO:root:Published data: {"orderId": 2}
  3. == APP == INFO:root:Published data: {"orderId": 3}
  4. == APP == INFO:root:Published data: {"orderId": 4}
  5. == APP == INFO:root:Published data: {"orderId": 5}
  6. == APP == INFO:root:Published data: {"orderId": 6}
  7. == APP == INFO:root:Published data: {"orderId": 7}
  8. == APP == INFO:root:Published data: {"orderId": 8}
  9. == APP == INFO:root:Published data: {"orderId": 9}
  10. == APP == INFO:root:Published data: {"orderId": 10}

Subscriber output:

  1. == APP == INFO:root:Subscriber received: {"orderId": 1}
  2. == APP == INFO:root:Subscriber received: {"orderId": 2}
  3. == APP == INFO:root:Subscriber received: {"orderId": 3}
  4. == APP == INFO:root:Subscriber received: {"orderId": 4}
  5. == APP == INFO:root:Subscriber received: {"orderId": 5}
  6. == APP == INFO:root:Subscriber received: {"orderId": 6}
  7. == APP == INFO:root:Subscriber received: {"orderId": 7}
  8. == APP == INFO:root:Subscriber received: {"orderId": 8}
  9. == APP == INFO:root:Subscriber received: {"orderId": 9}
  10. == APP == INFO:root:Subscriber received: {"orderId": 10}
pubsub.yaml component file

When you run dapr init, Dapr creates a default Redis pubsub.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\pubsub.yaml
  • On Linux/MacOS, under ~/.dapr/components/pubsub.yaml

With the pubsub.yaml component, you can easily swap out underlying components without application code changes.

The Redis pubsub.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: orderpubsub
  5. spec:
  6. type: pubsub.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""

In the YAML file:

  • metadata/name is how your application talks to the component.
  • spec/metadata defines the connection to the instance of the component.
  • scopes specify which application can use the component.

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 3: Subscribe to topics

In a terminal window, from the root of the Quickstarts clone directory navigate to the order-processor directory.

  1. cd pub_sub/javascript/sdk/order-processor

Install dependencies, which will include the @dapr/dapr package from the JavaScript SDK:

  1. npm install

Verify you have the following files included in the service directory:

  • package.json
  • package-lock.json

Run the order-processor subscriber service alongside a Dapr sidecar.

  1. dapr run --app-port 5002 --app-id order-processing --app-protocol http --dapr-http-port 3501 --resources-path ../../../components -- npm run start

In the order-processor subscriber, we’re subscribing to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. This enables your app code to talk to the Redis component instance through the Dapr sidecar.

  1. server.pubsub.subscribe("orderpubsub", "orders", (data) => console.log("Subscriber received: " + JSON.stringify(data)));

Step 4: Publish a topic

In a new terminal window, from the root of the Quickstarts clone directory, navigate to the checkout directory.

  1. cd pub_sub/javascript/sdk/checkout

Install dependencies, which will include the @dapr/dapr package from the JavaScript SDK:

  1. npm install

Verify you have the following files included in the service directory:

  • package.json
  • package-lock.json

Run the checkout publisher service alongside a Dapr sidecar.

  1. dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 --resources-path ../../../components -- npm run start

In the checkout publisher service, we’re publishing the orderId message to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. As soon as the service starts, it publishes in a loop:

  1. const client = new DaprClient();
  2. await client.pubsub.publish(PUBSUB_NAME, PUBSUB_TOPIC, order);
  3. console.log("Published data: " + JSON.stringify(order));

Step 5: View the Pub/sub outputs

Notice, as specified in the code above, the publisher pushes a random number to the Dapr sidecar while the subscriber receives it.

Publisher output:

  1. == APP == Published data: {"orderId":1}
  2. == APP == Published data: {"orderId":2}
  3. == APP == Published data: {"orderId":3}
  4. == APP == Published data: {"orderId":4}
  5. == APP == Published data: {"orderId":5}
  6. == APP == Published data: {"orderId":6}
  7. == APP == Published data: {"orderId":7}
  8. == APP == Published data: {"orderId":8}
  9. == APP == Published data: {"orderId":9}
  10. == APP == Published data: {"orderId":10}

Subscriber output:

  1. == APP == Subscriber received: {"orderId":1}
  2. == APP == Subscriber received: {"orderId":2}
  3. == APP == Subscriber received: {"orderId":3}
  4. == APP == Subscriber received: {"orderId":4}
  5. == APP == Subscriber received: {"orderId":5}
  6. == APP == Subscriber received: {"orderId":6}
  7. == APP == Subscriber received: {"orderId":7}
  8. == APP == Subscriber received: {"orderId":8}
  9. == APP == Subscriber received: {"orderId":9}
  10. == APP == Subscriber received: {"orderId":10}
pubsub.yaml component file

When you run dapr init, Dapr creates a default Redis pubsub.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\pubsub.yaml
  • On Linux/MacOS, under ~/.dapr/components/pubsub.yaml

With the pubsub.yaml component, you can easily swap out underlying components without application code changes.

The Redis pubsub.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: orderpubsub
  5. spec:
  6. type: pubsub.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""

In the YAML file:

  • metadata/name is how your application talks to the component.
  • spec/metadata defines the connection to the instance of the component.
  • scopes specify which application can use the component.

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 3: Subscribe to topics

In a terminal window, from the root of the Quickstarts clone directory navigate to the order-processor directory.

  1. cd pub_sub/csharp/sdk/order-processor

Recall NuGet packages:

  1. dotnet restore
  2. dotnet build

Run the order-processor subscriber service alongside a Dapr sidecar.

  1. dapr run --app-id order-processor --resources-path ../../../components --app-port 7006 -- dotnet run

In the order-processor subscriber, we’re subscribing to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. This enables your app code to talk to the Redis component instance through the Dapr sidecar.

  1. // Dapr subscription in [Topic] routes orders topic to this route
  2. app.MapPost("/orders", [Topic("orderpubsub", "orders")] (Order order) => {
  3. Console.WriteLine("Subscriber received : " + order);
  4. return Results.Ok(order);
  5. });
  6. public record Order([property: JsonPropertyName("orderId")] int OrderId);

Step 4: Publish a topic

In a new terminal window, from the root of the Quickstarts clone directory, navigate to the checkout directory.

  1. cd pub_sub/csharp/sdk/checkout

Recall NuGet packages:

  1. dotnet restore
  2. dotnet build

Run the checkout publisher service alongside a Dapr sidecar.

  1. dapr run --app-id checkout --resources-path ../../../components -- dotnet run

In the checkout publisher, we’re publishing the orderId message to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. As soon as the service starts, it publishes in a loop:

  1. using var client = new DaprClientBuilder().Build();
  2. await client.PublishEventAsync("orderpubsub", "orders", order);
  3. Console.WriteLine("Published data: " + order);

Step 5: View the Pub/sub outputs

Notice, as specified in the code above, the publisher pushes a random number to the Dapr sidecar while the subscriber receives it.

Publisher output:

  1. == APP == Published data: Order { OrderId = 1 }
  2. == APP == Published data: Order { OrderId = 2 }
  3. == APP == Published data: Order { OrderId = 3 }
  4. == APP == Published data: Order { OrderId = 4 }
  5. == APP == Published data: Order { OrderId = 5 }
  6. == APP == Published data: Order { OrderId = 6 }
  7. == APP == Published data: Order { OrderId = 7 }
  8. == APP == Published data: Order { OrderId = 8 }
  9. == APP == Published data: Order { OrderId = 9 }
  10. == APP == Published data: Order { OrderId = 10 }

Subscriber output:

  1. == APP == Subscriber received: Order { OrderId = 1 }
  2. == APP == Subscriber received: Order { OrderId = 2 }
  3. == APP == Subscriber received: Order { OrderId = 3 }
  4. == APP == Subscriber received: Order { OrderId = 4 }
  5. == APP == Subscriber received: Order { OrderId = 5 }
  6. == APP == Subscriber received: Order { OrderId = 6 }
  7. == APP == Subscriber received: Order { OrderId = 7 }
  8. == APP == Subscriber received: Order { OrderId = 8 }
  9. == APP == Subscriber received: Order { OrderId = 9 }
  10. == APP == Subscriber received: Order { OrderId = 10 }
pubsub.yaml component file

When you run dapr init, Dapr creates a default Redis pubsub.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\pubsub.yaml
  • On Linux/MacOS, under ~/.dapr/components/pubsub.yaml

With the pubsub.yaml component, you can easily swap out underlying components without application code changes.

The Redis pubsub.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: orderpubsub
  5. spec:
  6. type: pubsub.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""

In the YAML file:

  • metadata/name is how your application talks to the component.
  • spec/metadata defines the connection to the instance of the component.
  • scopes specify which application can use the component.

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 3: Subscribe to topics

In a terminal window, from the root of the Quickstarts clone directory navigate to the order-processor directory.

  1. cd pub_sub/java/sdk/order-processor

Install the dependencies:

  1. mvn clean install

Run the order-processor subscriber service alongside a Dapr sidecar.

  1. dapr run --app-port 8080 --app-id order-processor --resources-path ../../../components -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar

In the order-processor subscriber, we’re subscribing to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. This enables your app code to talk to the Redis component instance through the Dapr sidecar.

  1. @Topic(name = "orders", pubsubName = "orderpubsub")
  2. @PostMapping(path = "/orders", consumes = MediaType.ALL_VALUE)
  3. public Mono<ResponseEntity> getCheckout(@RequestBody(required = false) CloudEvent<Order> cloudEvent) {
  4. return Mono.fromSupplier(() -> {
  5. try {
  6. logger.info("Subscriber received: " + cloudEvent.getData().getOrderId());
  7. return ResponseEntity.ok("SUCCESS");
  8. } catch (Exception e) {
  9. throw new RuntimeException(e);
  10. }
  11. });
  12. }

Step 4: Publish a topic

In a new terminal window, from the root of the Quickstarts clone directory, navigate to the checkout directory.

  1. cd pub_sub/java/sdk/checkout

Install the dependencies:

  1. mvn clean install

Run the checkout publisher service alongside a Dapr sidecar.

  1. dapr run --app-id checkout --resources-path ../../../components -- java -jar target/CheckoutService-0.0.1-SNAPSHOT.jar

In the checkout publisher, we’re publishing the orderId message to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. As soon as the service starts, it publishes in a loop:

  1. DaprClient client = new DaprClientBuilder().build();
  2. client.publishEvent(
  3. PUBSUB_NAME,
  4. TOPIC_NAME,
  5. order).block();
  6. logger.info("Published data: " + order.getOrderId());

Step 5: View the Pub/sub outputs

Notice, as specified in the code above, the publisher pushes a random number to the Dapr sidecar while the subscriber receives it.

Publisher output:

  1. == APP == 7194 [main] INFO com.service.CheckoutServiceApplication - Published data: 1
  2. == APP == 12213 [main] INFO com.service.CheckoutServiceApplication - Published data: 2
  3. == APP == 17233 [main] INFO com.service.CheckoutServiceApplication - Published data: 3
  4. == APP == 22252 [main] INFO com.service.CheckoutServiceApplication - Published data: 4
  5. == APP == 27276 [main] INFO com.service.CheckoutServiceApplication - Published data: 5
  6. == APP == 32320 [main] INFO com.service.CheckoutServiceApplication - Published data: 6
  7. == APP == 37340 [main] INFO com.service.CheckoutServiceApplication - Published data: 7
  8. == APP == 42356 [main] INFO com.service.CheckoutServiceApplication - Published data: 8
  9. == APP == 47386 [main] INFO com.service.CheckoutServiceApplication - Published data: 9
  10. == APP == 52410 [main] INFO com.service.CheckoutServiceApplication - Published data: 10

Subscriber output:

  1. == APP == 2022-03-07 13:31:19.551 INFO 43512 --- [nio-8080-exec-5] c.s.c.OrderProcessingServiceController : Subscriber received: 1
  2. == APP == 2022-03-07 13:31:19.552 INFO 43512 --- [nio-8080-exec-9] c.s.c.OrderProcessingServiceController : Subscriber received: 2
  3. == APP == 2022-03-07 13:31:19.551 INFO 43512 --- [nio-8080-exec-6] c.s.c.OrderProcessingServiceController : Subscriber received: 3
  4. == APP == 2022-03-07 13:31:19.552 INFO 43512 --- [nio-8080-exec-2] c.s.c.OrderProcessingServiceController : Subscriber received: 4
  5. == APP == 2022-03-07 13:31:19.553 INFO 43512 --- [nio-8080-exec-2] c.s.c.OrderProcessingServiceController : Subscriber received: 5
  6. == APP == 2022-03-07 13:31:19.553 INFO 43512 --- [nio-8080-exec-9] c.s.c.OrderProcessingServiceController : Subscriber received: 6
  7. == APP == 2022-03-07 13:31:22.849 INFO 43512 --- [nio-8080-exec-3] c.s.c.OrderProcessingServiceController : Subscriber received: 7
  8. == APP == 2022-03-07 13:31:27.866 INFO 43512 --- [nio-8080-exec-6] c.s.c.OrderProcessingServiceController : Subscriber received: 8
  9. == APP == 2022-03-07 13:31:32.895 INFO 43512 --- [nio-8080-exec-6] c.s.c.OrderProcessingServiceController : Subscriber received: 9
  10. == APP == 2022-03-07 13:31:37.919 INFO 43512 --- [nio-8080-exec-2] c.s.c.OrderProcessingServiceController : Subscriber received: 10
pubsub.yaml component file

When you run dapr init, Dapr creates a default Redis pubsub.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\pubsub.yaml
  • On Linux/MacOS, under ~/.dapr/components/pubsub.yaml

With the pubsub.yaml component, you can easily swap out underlying components without application code changes.

The Redis pubsub.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: orderpubsub
  5. spec:
  6. type: pubsub.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""
  13. scopes:
  14. - orderprocessing
  15. - checkout

In the YAML file:

  • metadata/name is how your application talks to the component.
  • spec/metadata defines the connection to the instance of the component.
  • scopes specify which application can use the component.

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

  1. git clone https://github.com/dapr/quickstarts.git

Step 3: Subscribe to topics

In a terminal window, from the root of the Quickstarts clone directory navigate to the order-processor directory.

  1. cd pub_sub/go/sdk/order-processor

Install the dependencies and build the application:

  1. go build .

Run the order-processor subscriber service alongside a Dapr sidecar.

  1. dapr run --app-port 6005 --app-id order-processor-sdk --app-protocol http --dapr-http-port 3501 --resources-path ../../../components -- go run .

In the order-processor subscriber, we’re subscribing to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. This enables your app code to talk to the Redis component instance through the Dapr sidecar.

  1. func eventHandler(ctx context.Context, e *common.TopicEvent) (retry bool, err error) {
  2. fmt.Println("Subscriber received: ", e.Data)
  3. return false, nil
  4. }

Step 4: Publish a topic

In a new terminal window, from the root of the Quickstarts clone directory, navigate to the checkout directory.

  1. cd pub_sub/go/sdk/checkout

Install the dependencies and build the application:

  1. go build .

Run the checkout publisher service alongside a Dapr sidecar.

  1. dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 --resources-path ../../../components -- go run .

In the checkout publisher, we’re publishing the orderId message to the Redis instance called orderpubsub (as defined in the pubsub.yaml component) and topic orders. As soon as the service starts, it publishes in a loop:

  1. client, err := dapr.NewClient()
  2. if err := client.PublishEvent(ctx, PUBSUB_NAME, PUBSUB_TOPIC, []byte(order)); err != nil {
  3. panic(err)
  4. }
  5. fmt.Println("Published data: ", order)

Step 5: View the Pub/sub outputs

Notice, as specified in the code above, the publisher pushes a numbered message to the Dapr sidecar while the subscriber receives it.

Publisher output:

  1. == APP == dapr client initializing for: 127.0.0.1:63293
  2. == APP == Published data: {"orderId":1}
  3. == APP == Published data: {"orderId":2}
  4. == APP == Published data: {"orderId":3}
  5. == APP == Published data: {"orderId":4}
  6. == APP == Published data: {"orderId":5}
  7. == APP == Published data: {"orderId":6}
  8. == APP == Published data: {"orderId":7}
  9. == APP == Published data: {"orderId":8}
  10. == APP == Published data: {"orderId":9}
  11. == APP == Published data: {"orderId":10}

Subscriber output:

  1. == APP == Subscriber received: {"orderId":1}
  2. == APP == Subscriber received: {"orderId":2}
  3. == APP == Subscriber received: {"orderId":3}
  4. == APP == Subscriber received: {"orderId":4}
  5. == APP == Subscriber received: {"orderId":5}
  6. == APP == Subscriber received: {"orderId":6}
  7. == APP == Subscriber received: {"orderId":7}
  8. == APP == Subscriber received: {"orderId":8}
  9. == APP == Subscriber received: {"orderId":9}
  10. == APP == Subscriber received: {"orderId":10}

Note: the order in which they are received may vary.

pubsub.yaml component file

When you run dapr init, Dapr creates a default Redis pubsub.yaml and runs a Redis container on your local machine, located:

  • On Windows, under %UserProfile%\.dapr\components\pubsub.yaml
  • On Linux/MacOS, under ~/.dapr/components/pubsub.yaml

With the pubsub.yaml component, you can easily swap out underlying components without application code changes.

The Redis pubsub.yaml file included for this Quickstart contains the following:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: orderpubsub
  5. spec:
  6. type: pubsub.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: localhost:6379
  11. - name: redisPassword
  12. value: ""
  13. scopes:
  14. - orderprocessing
  15. - checkout

In the YAML file:

  • metadata/name is how your application talks to the component.
  • spec/metadata defines the connection to the instance of the component.
  • scopes specify which application can use the component.

Tell us what you think!

We’re continuously working to improve our Quickstart examples and value your feedback. Did you find this Quickstart helpful? Do you have suggestions for improvement?

Join the discussion in our discord channel.

Next steps

Explore Dapr tutorials >>

Last modified March 21, 2024: Merge pull request #4082 from newbe36524/v1.13 (f4b0938)