如何设置主兔模板和兔子连接工厂

时间:2018-01-07 13:05:24

标签: spring-boot spring-integration spring-cloud spring-rabbit zipkin

我在弹簧启动项目中遇到一些错误,我使用spring集成连接到RabbitMQ。我在XML文件中进行RabbitMQ的配置,如下所示:

<!-- RabbitMQ configuration -->
<rabbit:connection-factory
        id="rabbitConnectionFactory_2"
        host="${queuing.events.host}"
        port="${queuing.events.port}"
        username="${queuing.events.username}"
        password="${queuing.events.password}"
        virtual-host="${queuing.events.virtual-host}"
        publisher-returns="true"/>

<rabbit:template id="amqpTemplate_2" connection-factory="rabbitConnectionFactory_2" />
<rabbit:admin id="rabbitAdmin_2" connection-factory="rabbitConnectionFactory_2"/>
<rabbit:listener-container connection-factory="rabbitConnectionFactory_2" auto-startup="true" requeue-rejected="false" />

<bean id="rabbitListenerContainerFactory_2" class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
    <property name="connectionFactory" ref="rabbitConnectionFactory_2"/>
</bean>
<!--  -->

但我正在创建每个组件中的两个。 如何设置初选?

现在问题出现了,我正在使用这个版本的spring cloud:

<spring-cloud.version>Dalston.SR2</spring-cloud.version>

一切正常,但如果我将版本更新为:

<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>

此错误即将发生:

Description:

Parameter 0 of method rabbitSender in org.springframework.cloud.sleuth.zipkin2.sender.ZipkinRabbitSenderConfiguration required a single bean, but 2 were found:
- rabbitConnectionFactory: defined in null
- rabbitConnectionFactory_2: defined in null


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

错误来自于这种依赖性:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-sleuth-zipkin</artifactId>
    </dependency>

如果删除此依赖项,则错误不会发生。

您可以找到重现此方案的示例项目。在pom文件中你会看到:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Dalston.SR2</spring-cloud.version> <!-- it works with this version-->
    <!-- <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> -->  <!-- doesn't work with this version-->
</properties>

<dependencies>
    <!-- SPRING BOOT -->

    <!-- it fails because of this dependency in that we are using Edgware.RELEASE version -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-sleuth-zipkin</artifactId>
    </dependency>
    ...

https://github.com/fjmpaez911/spring-integration-zipkin-cloud

所以我需要知道如何为RabbitMQ设置主要配置,此外我认为这可能是一个问题,因为只有在我使用此版本时才出现此错误 Edgware.RELEASE

我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

fran,在Edgware.RELEASE <artifactId>spring-cloud-sleuth-zipkin</artifactId>将解决Zipkin 2依赖关系尝试使用<artifactId> spring-cloud-starter-zipkin-legacy</artifactId>而不是

答案 1 :(得分:0)

要在XML文件中定义RabbitMQ的主要连接工厂,您可以执行以下操作:

<!-- Here the primary connection -->
<bean id="rabbitConnectionFactory" primary="true" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
    <constructor-arg value="${spring.rabbitmq.host}"/>
    <property name="username" value="${spring.rabbitmq.username}"/>
    <property name="password" value="${spring.rabbitmq.password}"/>
    <property name="virtualHost" value="${spring.rabbitmq.virtual-host}"/>
</bean>

<!-- RabbitMQ configuration -->
<rabbit:connection-factory
        id="rabbitConnectionFactory_2"
        host="${queuing.events.host}"
        port="${queuing.events.port}"
        username="${queuing.events.username}"
        password="${queuing.events.password}"
        virtual-host="${queuing.events.virtual-host}"
        publisher-returns="true"/>

<rabbit:template id="amqpTemplate_2" connection-factory="rabbitConnectionFactory_2" />
<rabbit:admin id="rabbitAdmin_2" connection-factory="rabbitConnectionFactory_2"/>
<rabbit:listener-container connection-factory="rabbitConnectionFactory_2" auto-startup="true" requeue-rejected="false" />

<bean id="rabbitListenerContainerFactory_2" class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
    <property name="connectionFactory" ref="rabbitConnectionFactory_2"/>
</bean>