连接数和通道数
Number of connections and channels
我是 Rabbitmq 的新手 Spring。我想知道如何管理连接数和通道数。
在我的架构中有 2 个队列,其中消息是根据直接交换中的路由键从单个生产者发布的。根据我的理解,我需要一个与 2 个通道的单一连接,这将是持久的,并且消息将通过它们发布。我假设这是由 Spring 自动管理的。但是每次发布消息时都会创建一个由单个通道组成的连接。
- 如何管理频道和连接?为连接中的每个队列创建单个通道是否正确?如果队列大小增加到 10,那么应该在单个连接中使用 10 个通道?
配置文件:
<bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<property name="username" value="test"/>
<property name="password" value="test"/>
<property name="host" value="50.16.11.22"/>
<property name="port" value="5672"/>
</bean>
<bean id="publisher" class="com.test.code.Publisher">
<constructor-arg ref="amqpTemplate"></constructor-arg>
</bean>
<bean id="amqpTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="mandatory" value="true"></property>
<property name="exchange" value="x.direct"></property>
</bean>
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="q.queue1" />
<rabbit:queue name="q.queue2" />
<rabbit:direct-exchange name="x.direct">
<rabbit:bindings>
<rabbit:binding queue="q.queue1" key="key1" />
<rabbit:binding queue="q.queue2" key="key2" />
</rabbit:bindings>
</rabbit:direct-exchange>
</beans>
这是我的发布者class
public class Publisher {
public Publisher(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void messageToQueue1(JSONObject message) {
amqpTemplate.convertAndSend("key1", message.toString());
}
public void messageToQueue2(JSONObject message) {
amqpTemplate.convertAndSend("key2", message.toString());
}
}
But a connection, consisting of single channel, is created every time a message is published.
事实并非如此。每个路由键也没有专用通道。
CachingConnectionFactory
维护单个持久连接(默认情况下)并缓存通道。
第一次发布创建一个频道并将其放入缓存中。下一次发布从缓存中获取它。只有当缓存为空时才会创建一个新频道(然后你最终会得到 2 个缓存频道)。
您只能同时获得所需数量的频道。
我是 Rabbitmq 的新手 Spring。我想知道如何管理连接数和通道数。
在我的架构中有 2 个队列,其中消息是根据直接交换中的路由键从单个生产者发布的。根据我的理解,我需要一个与 2 个通道的单一连接,这将是持久的,并且消息将通过它们发布。我假设这是由 Spring 自动管理的。但是每次发布消息时都会创建一个由单个通道组成的连接。 - 如何管理频道和连接?为连接中的每个队列创建单个通道是否正确?如果队列大小增加到 10,那么应该在单个连接中使用 10 个通道?
配置文件:
<bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<property name="username" value="test"/>
<property name="password" value="test"/>
<property name="host" value="50.16.11.22"/>
<property name="port" value="5672"/>
</bean>
<bean id="publisher" class="com.test.code.Publisher">
<constructor-arg ref="amqpTemplate"></constructor-arg>
</bean>
<bean id="amqpTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="mandatory" value="true"></property>
<property name="exchange" value="x.direct"></property>
</bean>
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="q.queue1" />
<rabbit:queue name="q.queue2" />
<rabbit:direct-exchange name="x.direct">
<rabbit:bindings>
<rabbit:binding queue="q.queue1" key="key1" />
<rabbit:binding queue="q.queue2" key="key2" />
</rabbit:bindings>
</rabbit:direct-exchange>
</beans>
这是我的发布者class
public class Publisher {
public Publisher(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void messageToQueue1(JSONObject message) {
amqpTemplate.convertAndSend("key1", message.toString());
}
public void messageToQueue2(JSONObject message) {
amqpTemplate.convertAndSend("key2", message.toString());
}
}
But a connection, consisting of single channel, is created every time a message is published.
事实并非如此。每个路由键也没有专用通道。
CachingConnectionFactory
维护单个持久连接(默认情况下)并缓存通道。
第一次发布创建一个频道并将其放入缓存中。下一次发布从缓存中获取它。只有当缓存为空时才会创建一个新频道(然后你最终会得到 2 个缓存频道)。
您只能同时获得所需数量的频道。