我正在尝试通过xml使用spring boot app连接并使用两个不同的rabbitmq集群。当在应用程序上下文中创建单个rabbit:connection-factory bean时,它可以很好地工作。但是,当添加第二个时,它无法启动应用程序,错误“org.springframework.boot.autoconfigure.amqp.RabbitAnnotationDrivenConfiguration中的方法rabbitListenerContainerFactory的参数1需要一个bean,但是找到了2个:” 即可。如何为每个群集创建不同的工厂?如果这不是正确的方法,请建议另一种方法吗?
以下是xml片段:
<rabbit:connection-factory id="firstConnectionFactory" connection-factory="firstSpringConnectionFactory" />
<rabbit:connection-factory id="secondConnectionFactory" connection-factory="secondSpringConnectionFactory"/>
<bean id="firstSpringConnectionFactory"
class="org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean">
<property name="useSSL" value="${rabbitmq.ssl.enabled}" />
<property name="host" value="${rabbitmq.first.host}"/>
<property name="virtualHost" value="${rabbitmq.vhost}"/>
<property name="port" value="${rabbitmq.cluster.port}"/>
<property name="username" value="${rabbitmq.user}"/>
<property name="password" value="${rabbitmq.first.password}"/>
</bean>
<bean id="secondSpringConnectionFactory"
class="org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean">
<property name="useSSL" value="${rabbitmq.ssl.enabled}" />
<property name="host" value="${rabbitmq.second.host}"/>
<property name="virtualHost" value="${rabbitmq.vhost}"/>
<property name="port" value="${rabbitmq.cluster.port}"/>
<property name="username" value="${rabbitmq.user}"/>
<property name="password" value="${rabbitmq.second.password}"/>
</bean>
监听器容器代码:
ConnectionFactory cf = rabbitConnectionFactory;//One of the connnection factories will be injected here from app context
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(cf);
container.setConcurrentConsumers(count);
container.addQueueNames(queueName);
container.setMessageListener(listener);
container.start();
答案 0 :(得分:3)
由于您不依赖Spring Boot并且不使用Spring AMQP注释支持,我建议您从自动配置中exclude RabbitAnnotationDrivenConfiguration
:
@EnableAutoConfiguration(exclude={RabbitAnnotationDrivenConfiguration.class})
spring.autoconfigure.exclude = org.springframework.boot.autoconfigure.amqp.RabbitAnnotationDrivenConfiguration
如果您仍需要在项目的其他位置某处@RabbitListener
,则只能选择构建所有@EnableRabbit
基础架构manually。