Spring Boot无法创建多个兔子连接工厂
作者:互联网
我正在尝试通过xml使用spring boot应用程序连接到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();
解决方法:
由于您在这里不依赖于Spring Boot,也不使用Spring AMQP注释支持,因此建议您从自动配置中选择exclude RabbitAnnotationDrivenConfiguration:
@EnableAutoConfiguration(exclude={RabbitAnnotationDrivenConfiguration.class})
spring.autoconfigure.exclude = org.springframework.boot.autoconfigure.amqp.RabbitAnnotationDrivenConfiguration
如果在项目其他位置仍需要@RabbitListener,则只能选择构建所有@EnableRabbit基础结构manually.
标签:spring-amqp,spring-boot,spring 来源: https://codeday.me/bug/20191111/2017241.html