Skip to content

BarbieCue/dapr-pub-sub

Repository files navigation

dapr example - publish-subscribe

dapr

The following scenario is implemented here.

scenario

A client posts a new item to the inventory service. Thereupon, the inventory service creates and publishes an item data record on the RabbitMQ topic called "items". The other two services, catalog and stock are subscribers of that topic. Both receive the data record and react individually.

However, all three services do not communicate directly with RabbitMQ. Instead, they each delegate Pub/Sub operations to their dapr sidecars. The dapr sidecars take over the direct communication with RabbitMQ.

Pub/Sub Component

dapr uses yaml files to specify both, the Pub/Sub component (RabbitMQ in our case) and the Pub/Sub subscription settings. These files are located in components/local. Using these yaml files enables us to exchange the underlying message broker without making any code changes.

Prerequisites

  • dapr CLI
  • Java (Version >=17)
  • A running RabbitMQ instance
    docker run --rm -it -p 15672:15672 -p 5672:5672 rabbitmq:3

Get started

1. Build all services

./gradlew buildAllServices

2. Run each service alongside a dapr sidecar

dapr run --app-id inventory-service --app-port 8080 --dapr-http-port 3500 --components-path components/local -- java -cp inventory/build/libs/fat.jar org.example.InventoryKt
dapr run --app-id stock-service --app-port 8081 --dapr-http-port 3501 --components-path components/local -- java -cp stock/build/libs/fat.jar org.example.StockKt
dapr run --app-id catalog-service --app-port 8082 --dapr-http-port 3502 --components-path components/local -- java -cp catalog/build/libs/fat.jar org.example.CatalogKt

As you can see, the dapr CLI is used to start up both, the dapr sidecar and the service.

3. Be the client - post an item

curl -X POST localhost:8080/pencil

References

Releases

No releases published

Packages

No packages published

Languages