Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I connect to multiple Rabbitmq nodes with Spring Rabbitmq?

I am writing a service with Spring and I am using Spring AMQP in order to connect to Rabbitmq.

I have two rabbitmq clusters, one is only for publishing messages(the messages are sent to the other cluster via the federation plugin) and the other cluster is for declaring queues that end users will consume from.

The nodes sit behind aws lb, each cluster has a lb.

I am using CachingConnectionFactory and RabbitTemplate,RabbitAdmin in my code and I want to have connections to all the nodes so I can use them. For the cluster that will contain the queues I added to the config the queue-master-locator=random so new queues will be declared in all the nodes in the cluster even if my service does not have a connection to them.

With the cluster that publishes messages I have more of a problem because I need a direct connection in my service to each of the nodes so I will be able to separate the load between the nodes.

So my problem is, how do I create connections in my service to all the nodes in the cluster so they will all be used for declaring queues and sending messages?

Now, after I will have some sort of solution to this issue, the next issue will be what happens when a new node is added to the cluster? How can I create a connection to it and start using it as well?

I am using Rabbitmq - 3.7.9, Spring - 2.0.5, Spring AMQP - 2.0.5

Thanks alot!

like image 519
Ori Shofman Avatar asked Dec 02 '25 13:12

Ori Shofman


1 Answers

There is currently no mechanism to do anything like that.

By default, Spring AMQP opens only one connection (optionally two, one for publishing, one for consuming).

Even when using CacheMode.CONNECTION, you'll get a new connection for each consumer (and connections will be created and cached on demand for producers), you won't get any control as to which node it connects to; that's a function of the LB.

The framework does provide the LocalizedQueueConnectionFactory which will try to consume from the node that hosts a queue, but it won't work with a load balancer in place.

In general, however, such optimization is rarely needed.

Are you trying to solve an actual problem you are experiencing now, or something that you perceive that might be a problem?

It is generally best not to perform premature optimization.

like image 187
Gary Russell Avatar answered Dec 04 '25 07:12

Gary Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!