In this quickstart, you'll create a publisher microservice and a subscriber microservice to demonstrate how Dapr enables a publish-subcribe pattern. The publisher will generate messages of a specific topic, while subscribers will listen for messages of specific topics. See Why Pub-Sub to understand when this pattern might be a good choice for your software architecture.
Visit this link for more information about Dapr and Pub-Sub.
Note: This example leverages HTTP
requestsonly. If you are looking for the example using the Dapr Client SDK (recommended) click here.
This quickstart includes one publisher:
- Node client message generator
checkout
And one subscriber:
- Node subscriber
order-processor
This section shows how to run both applications at once using multi-app run template files with dapr run -f .. This enables to you test the interactions between multiple applications.
- Install dependencies:
cd ./order-processor
npm install
cd ..
cd ./checkout
npm install- Open a new terminal window and run the multi app run template:
dapr run -f .The terminal console output should look similar to this:
Published data: {"orderId":1}
Subscriber received: { orderId: 1 }
Published data: {"orderId":2}
Subscriber received: { orderId: 2 }
Published data: {"orderId":3}
Subscriber received: { orderId: 3 }
Published data: {"orderId":4}
Subscriber received: { orderId: 4 }
Published data: {"orderId":5}
Subscriber received: { orderId: 5 }
Published data: {"orderId":6}
Subscriber received: { orderId: 6 }
Published data: {"orderId":7}
Subscriber received: { orderId: 7 }
Published data: {"orderId":8}
Subscriber received: { orderId: 8 }
Published data: {"orderId":9}
Subscriber received: { orderId: 9 }
Published data: {"orderId":10}
Subscriber received: { orderId: 10 }
- Stop and clean up application processes
dapr stop -f .An alternative to running all or multiple applications at once is to run single apps one-at-a-time using multiple dapr run .. -- npm run start commands. This next section covers how to do this.
- Install dependencies:
cd ./order-processor
npm install- Run the Node publisher app with Dapr:
dapr run --app-port 5003 --app-id order-processing-http --app-protocol http --dapr-http-port 3501 --resources-path ../../../components -- npm run start- Install dependencies:
cd ./checkout
npm install- Run the Node publisher app with Dapr:
dapr run --app-id checkout-http --app-protocol http --dapr-http-port 3500 --resources-path ../../../components -- npm run startdapr stop --app-id checkout-http
dapr stop --app-id order-processor-http