简体   繁体   中英

Dapr binding for RabbitMQ not working using Dapr pub/sub sample

I've taken the Dapr pub/sub How-to sample and tried to update it to use RabbitMQ

I've downloaded the Docker rabbitmq:3 image from DockerHub and it should be listening on amqp://localhost:5672.

I have created a new component file for RabbitMQ called rabbitmq.yaml and placed it in the.dapr/components directory. My component configuration for RabbitMQ is:

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: my-rabbitmq
spec:
  type: pubsub.rabbitmq
version: v1
metadata:
  - name: host
    value: amqp://localhost:5672
  - name: durable
    value: true # Optional. Default: "false"
  - name: deletedWhenUnused
   value: false # Optional. Default: "false"
  - name: ttlInSeconds
    value: 60
  - name: prefetchCount
    value: 0

My subscription is defined in subscription-rabbitmq.yaml located in the same.dapr/components directory. and it looks as follows:

apiVersion: dapr.io/v1alpha1
kind: Subscription
metadata:
  name: rabbitmq-subscription
spec:
  topic: deathStarStatus
  route: /dsstatus
  pubsubname: my-rabbitmq
scopes:
- app2

When I run the sample using the Redis Streams component and subscription, the node application receives the message and displays in within the output of the dapr run command.

dapr publish --topic deathStarStatus --pubsub pubsub --data '{ "message": "This is a test" }'

but when I publish a message to RabbitMQ, it states the "Event Published Successfully", but the node app doesn't receive the message.

dapr publish --topic deathStarStatus --pubsub my-rabbitmq --data '{ "message": "This is a test" }'

Here is the node app and the command to run it:

dapr run --app-id app2 --app-port 3000 node app2.js

const express = require('express')
const bodyParser = require('body-parser')
const app = express()
app.use(bodyParser.json({ type: 'application/*+json' }));

const port = 3000

// app.get('/dapr/subscribe', (req, res) => {
//     res.json([
//         {
//             pubsubname: "my-rabbitmq",
//             topic: "deathStarStatus",
//             route: "dsstatus"        
//         }
//     ]);
// })

app.post('/dsstatus', (req, res) => {
    console.log(req.body);
    res.sendStatus(200);
});

app.listen(port, () => console.log(`consumer app listening on port ${port}!`))

What am I doing wrong?

It turns out that a "topic" in RabbitMQ (Dapr world) is really an Exchange and not a Queue.

When running "app2" with RabbitMQ subscription, a queue is created with a prepended appid (eg {appid}-{queueName}), but the Exchange was not created. Not sure if this is by design or my specific configuration.

I ended up creating an Exchange called "deathStarStatus" and mapped that Exchange to my queue called "app2-deathStarStatus" and everything worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM