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:
- Python client message generator
checkout
And one subscriber:
- Python 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:
uv sync --all-packages- Open a new terminal window and run the multi app run template:
uv run dapr run -f .The terminal console output should look similar to this:
Subscriber received : 1
127.0.0.1 - - [04/Sep/2023 11:33:21] "POST /orders HTTP/1.1" 200 -
Published data: {"orderId": 2}
Subscriber received : 2
127.0.0.1 - - [04/Sep/2023 11:33:22] "POST /orders HTTP/1.1" 200 -
Published data: {"orderId": 3}
Subscriber received : 3
127.0.0.1 - - [04/Sep/2023 11:33:23] "POST /orders HTTP/1.1" 200 -
Published data: {"orderId": 4}
Subscriber received : 4
127.0.0.1 - - [04/Sep/2023 11:33:24] "POST /orders HTTP/1.1" 200 -
Published data: {"orderId": 5}
Subscriber received : 5
127.0.0.1 - - [04/Sep/2023 11:33:25] "POST /orders HTTP/1.1" 200 -
127.0.0.1 - - [04/Sep/2023 11:33:26] "POST /orders HTTP/1.1" 200 -
Subscriber received : 6
Published data: {"orderId": 6}
Published data: {"orderId": 7}
Subscriber received : 7
127.0.0.1 - - [04/Sep/2023 11:33:27] "POST /orders HTTP/1.1" 200 -
Published data: {"orderId": 8}
Subscriber received : 8
127.0.0.1 - - [04/Sep/2023 11:33:28] "POST /orders HTTP/1.1" 200 -
Published data: {"orderId": 9}
Subscriber received : 9
127.0.0.1 - - [04/Sep/2023 11:33:29] "POST /orders HTTP/1.1" 200 -
- 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 .. -- uv run python app.py commands. This next section covers how to do this.
- Install dependencies:
uv sync --all-packages- Run the Python subscriber app with Dapr:
cd ./order-processor
dapr run --app-id order-processor-http --resources-path ../../../components/ --app-port 6021 -- uv run uvicorn app:app --port 6002- Install dependencies:
uv sync --all-packages- Run the Python publisher app with Dapr:
cd ./checkout
dapr run --app-id checkout-http --resources-path ../../../components/ -- uv run python app.pydapr stop --app-id checkout-http
dapr stop --app-id order-processor-http