1
votes

I want to setup a rabbitmq cluster behind a load balancer and connect to it using spring amqp. Questions :

  1. Does spring client need to know the address of each node in the RMQ cluster or it is sufficient for it to know just the address of load balancer.

  2. If Spring client is only aware of the load balancer, how will it maintain connections/connection factory for each node in the cluster.

  3. Is there any code sample, which shows how to make the spring client work with load balancer.

1

1 Answers

1
votes

It only needs the load balancer; however, Spring AMQP maintains a long-lived shared connection, so a load balancer generally doesn't bring much value unless you have multiple applications.

With a single application (with one connection factory), you will only be connected to one broker.

Clarification

See the documentation.

Starting with version 1.3, the CachingConnectionFactory can be configured to cache connections as well as just channels. In this case, each call to createConnection() creates a new connection (or retrieves an idle one from the cache). Closing a connection returns it to the cache (if the cache size has not been reached). Channels created on such connections are cached too. The use of separate connections might be useful in some environments, such as consuming from an HA cluster, in conjunction with a load balancer, to connect to different cluster members. Set the cacheMode to CacheMode.CONNECTION.

By default all components (listener containers, RabbitTemplates) share a single connection to the broker.

Starting with version 2.0.2, RabbitTemplate has a property usePublisherConnection; if this is set to true, publishers will use a separate connection to the listener containers - this is generally recommended to avoid a blocked publisher connection preventing consumers from receiving messages.

As shown in the quote, the use of a single (or 2) connections is controlled by connection factory's cache mode.

Setting the cache mode to CONNECTION, means that each component (listener container consumer, RabbitTemplate) gets its own connection. In practice there will only be one or two publisher connections because publish operations are, generally, short lived and the connection is cached for reuse. You might get one or two more publisher connections if concurrent publish operations are performed.