带有 rabbitmq 集群和 spring amqp 的负载均衡器

load balancer with rabbitmq cluster and spring amqp

我想在负载均衡器后面设置一个 rabbitmq 集群,并使用 spring amqp 连接到它。问题:

  1. spring客户端是否需要知道RMQ集群中每个节点的地址,或者只知道负载均衡器的地址就足够了。

  2. 如果Spring客户端只知道负载均衡器,它将如何为集群中的每个节点维护connections/connection工厂。

  3. 是否有任何代码示例,显示如何使 spring 客户端与负载平衡器一起工作。

只需要负载均衡器;但是,Spring AMQP 维护长期共享连接,因此负载均衡器通常不会带来太多价值,除非您有多个应用程序。

对于单个应用程序(具有一个连接工厂),您将只连接到一个代理。

澄清

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.

默认情况下,所有组件(侦听器容器、RabbitTemplates)共享一个到代理的连接。

从版本 2.0.2 开始,RabbitTemplate 有 属性 usePublisherConnection;如果将其设置为 true,发布者将使用与侦听器容器的单独连接 - 通常建议这样做以避免阻止发布者连接阻止消费者接收消息。

如引用中所示,单个(或 2 个)连接的使用由连接工厂的缓存模式控制。

将缓存模式设置为CONNECTION,意味着每个组件(监听器容器消费者,RabbitTemplate)都有自己的连接。实际上,只有一个或两个发布者连接,因为发布操作通常是短暂的,并且连接被缓存以供重用。如果执行并发发布操作,您可能会获得一个或两个以上的发布者连接。