跨多个RabbitMQ实例的RabbitMQ RPC [英] RabbitMQ RPC across multiple rabbitMQ instances

查看:388
本文介绍了跨多个RabbitMQ实例的RabbitMQ RPC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个客户端,每个客户端都有自己的RabbitMQ实例,并且我有一个具有自己的RabbitMQ实例的应用程序(我们称其为appA),这三个客户端应用程序(app1,app2,app3)希望在其上使用服务appA.

I have three clients each with their own RabbitMQ instances and I have an application (let's call it appA) that has its own RabbitMQ instance, the three client applications (app1, app2, app3) wants to make use of a service on appA.

appA上的服务需要RPC通信,app1,app2和app3分别具有一个booking.request队列和一个booking.response队列.

The service on appA requires RPC communication, app1, app2 and app3 each has a booking.request queue and a booking.response queue.

使用铲子插件,我可以将所有预订请求信息从app1-3转发到appA:

With the shovel plugin, I can forward all booking.request messages from app1-3 to appA:

Shovel1 
virtualHost=appA, 
name=booking-request-shovel, 
sourceURI=amqp://userForApp1:password@app1-server/vhostForApp1
queue=booking.request
destinationURI=amqp://userForAppA:password@appA-server/vhostForAppA
queue=booking.request

setup another shovel to get booking requests from app2 and app3 to appA in the same way as above.

现在,appA将响应booking.response队列上的请求,我需要在rabbitMQ-appA上的预订响应消息返回到app1,app2或app3上的正确booking.response队列,但不是所有他们-如何在rabbitMQ-appA上设置铲子/联合队列,将响应转发回正确的在他们自己的booking.response队列中期望响应的rabbitMQ(app1,app2,app3)?

Now appA will respond to the request on the booking.response queue, I need the booking response message on rabbitMQ-appA to go back to the correct booking.response queue either on app1, app2 or app3, but not to all of them - how do I setup a shovel / federated queue on rabbitMQ-appA that will forward the response back to the correct rabbitMQ (app1, app2, app3) that is expecting a response in their own booking.response queue?

所有这些应用都使用spring-amqp(如果相关) 另外,我可以在Spring中设置RabbitMQ模板,以监听多个RabbitMQ队列并从每个队列中使用.

All these apps are using spring-amqp (in case that's relevant) Alternatively, I could setup a rabbitMQ template in Spring that listens to multiple rabbitMQ queues and consumes from each of them.

从文档中可以看出,典型的消费者是这样的:

From the docs, this what a typical consumer looks like:

<rabbit:listener-container connection-factory="rabbitConnectionFactory">
    <rabbit:listener queues="some.queue" ref="somePojo" method="handle"/>
</rabbit:listener-container>

即使连接工厂是同一RabbitMQ实例,但只是不同的虚拟主机,也可以指定多个连接工厂来做到这一点:

Is it possible to specify multiple connection factories in order to do this even if the connection factories are to the same instance of RabbitMQ, but just different vhosts:

更新:

根据Josh的回答,我将有多个连接工厂:

Based on Josh's answer, I'd have multiple connection factories:

 <rabbit:connection-factory
                id="connectionFactory1"
                port="${rabbit.port1}"
                virtual-host="${rabbit.virtual1}"
                host="${rabbit.host1}"
                username="${rabbit.username1}"
                password="${rabbit.password1}"
                connection-factory="nativeConnectionFactory" />

 <rabbit:connection-factory
                id="connectionFactory2"
                port="${rabbit.port2}"
                virtual-host="${rabbit.virtual2}"
                host="${rabbit.host2}"
                username="${rabbit.username2}"
                password="${rabbit.password2}"
                connection-factory="nativeConnectionFactory" />

然后,我将使用SimpleRoutingConnectionFactory来包装两个连接工厂:

Then I would use the SimpleRoutingConnectionFactory to wrap both connection-factories:

<bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection.SimpleRoutingConnectionFactory">
    <property name="targetConnectionFactories">
        <map>
            <entry key="#{connectionFactory1.virtualHost}" ref="connectionFactory1"/>
            <entry key="#{connectionFactory2.virtualHost}" ref="connectionFactory2"/>
        </map>
    </property>
</bean>

现在,当我声明RabbitMQ模板时,我会将其指向SimpleRoutingConnectionFactory而不是各个连接工厂:

Now when I declare my rabbitMQ template, I would point it to the SimpleRoutingConnectionFactory instead of the individual connection factories:

<rabbit:template id="template" connection-factory="connectionFactory" />

...然后像我通常使用的那样使用模板...

... and then use the template as I would normally use it ...

<rabbit:listener-container
        connection-factory="connectionFactory"
        channel-transacted="true"
        requeue-rejected="true"
        concurrency="${rabbit.consumers}">
        <rabbit:listener queues="${queue.booking}" ref="TransactionMessageListener" method="handle"  />
</rabbit:listener-container>

//,并且从两个RabbitMQ实例中消费了消息

// and messages are consumed from both rabbitMQ instances

...和...

  @Autowired
  private AmqpTemplate template;

  template.send(getExchange(), getQueue(), new Message(gson.toJson(message).getBytes(), properties));

//和消息发布到两个队列

// and message publishes to both queues

我正确吗?

推荐答案

看看org.springframework.amqp.rabbit.connection.AbstractRoutingConnectionFactory.它将允许您创建到不同的虚拟主机或不同的Rabbitmq实例的多个连接工厂.我们正在将其用于多租户rabbitmq应用程序.

Take a look at org.springframework.amqp.rabbit.connection.AbstractRoutingConnectionFactory. It will allow you to create multiple connection factories to different vhosts or different rabbitmq instances. We are using it for a multi tenant rabbitmq application.

这篇关于跨多个RabbitMQ实例的RabbitMQ RPC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆