Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 (e.g. {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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...