在 Spring AMQP 中 CachingConnectionFactory.CacheMode Connection 或 Channel 哪个更好

Which is better CachingConnectionFactory.CacheMode Connection or Channel in Spring AMQP

您好,我一直在使用 Spring AMQP 以及从 CachingConnectionFactory 获得的连接,其属性如下所示

<bean id="connectionFactory"
    class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
    <constructor-arg value="rabbit-server-fqdn" />
    <property name="virtualHost" value="some-vhost" />
    <property name="username" value="username" />
    <property name="password" value="password " />
   <property name="channelCacheSize" value="25" />
</bean>

现在我需要将我的模式更改为连接,因为我需要检查打开的连接

   <bean id="connectionFactory"
    class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
    <constructor-arg value="rabbit-server-fqdn" />
    <property name="virtualHost" value="some-vhost" />
    <property name="username" value="username" />
    <property name="password" value="password " />
    <property name="cacheMode" value="CONNECTION" />
   <property name="channelCacheSize" value="25" />
</bean>

所以 Q1。 channelCacheSize 会起作用吗? Q2。 CachingConnectionFactory 中的默认连接池大小是多少? Q3.我需要设置额外的 属性 吗?

CHANNEL更好,因为你不需要为每次调用创建一个新的连接,而是重复使用一个共享的连接。

Reference Manual 有很多关于此事的事实。其中之一是什么时候你真的需要 CONNECTION 模式:

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.

仍然可以通过 ConnectionListener 注入 CachingConnectionFactory 中的 open/close 状态跟踪共享连接。