我有一个用@RabbitListener
注释的监听器方法,当前将其配置为从1个兔子集群监听。现在我有另一个兔子集群(cluster2),它定义了相同的队列/交换/路由键,并且可能会发布消息。
是否可以使用相同的侦听器方法从两个群集侦听?作为监听者,我不在乎消息来自哪个群集,并且我想对两者进行相同的处理。
// Config
@Bean
@ConfigurationProperties("cluster1.rabbitmq")
public RabbitProperties rabbitProps() {
return new RabbitProperties();
}
// Listener code
@RabbitListener(id = "My App", bindings = @QueueBinding(value = @Queue(value = "Some_Queue_Name"),
exchange = @Exchange(value = "Some_Exchange_Name"),
key = "Some_Routing_Key"))
public void onMessage(final Message message) {
// ....
}
我的application.yml
已定义以下配置
cluster1.rabbitmq.host
cluster1.rabbitmq.username
cluster1.rabbitmq.password
创建两个侦听器容器工厂,并向方法添加第二个@RabbitListener
@RabbitListener(id = "My App 1",
bindings = @QueueBinding(value = @Queue(value = "Some_Queue_Name"),
exchange = @Exchange(value = "Some_Exchange_Name"),
key = "Some_Routing_Key"))
@RabbitListener(id = "My App 2", containerFactory="secondContainerFactory"
bindings = @QueueBinding(value = @Queue(value = "Some_Queue_Name"),
exchange = @Exchange(value = "Some_Exchange_Name"),
key = "Some_Routing_Key"))
public void onMessage(final Message message) {
// ....
}
第一个将使用默认的rabbitListenerContainerFactory
。