【问题标题】:Spring boot 2 connection to rabbitmq via Apache CamelSpring boot 2 通过 Apache Camel 连接到 rabbitmq
【发布时间】:2019-03-06 12:00:23
【问题描述】:

我在 Spring Boot 2 上通过 Apache Camel 连接到 rabbitmq 时遇到问题。

我做了以下步骤:

我的依赖:

implementation "org.apache.camel:camel-spring-boot-starter:${camelVersion}"
implementation "org.apache.camel:camel-jackson-starter:${camelVersion}"
implementation "org.apache.camel:camel-core:${camelVersion}"
implementation "org.apache.camel:camel-rabbitmq-starter:${camelVersion}"
implementation "org.springframework.boot:spring-boot-starter-amqp"

Application.yaml

spring:
      rabbitmq:
      dynamic: true
      host: 192.168.1.1
      port: 5672
      username: X
      password: Y

我有以下路线:

@Component
public class BasicRoute extends RouteBuilder {

@Override
public void configure() throws Exception {

    from("direct:loggerQueue")
            .id("loggerQueue")
            .to("rabbitmq://TEST-QUEUE.exchange?queue=TEST-QUEUE.queue&autoDelete=false&connectionFactory=#rabbitConnectionFactory")
            .end();
}

}

最后我还有以下问题:

2019-03-06 12:46:05.766 WARN 19464 --- [restartedMain] o.a.c.c.rabbitmq.RabbitMQProducer:无法创建连接。发布消息时它将尝试再次连接。 java.net.ConnectException:连接被拒绝:连接

连接似乎没问题,我测试了它。 rabbitConnectionFactory 出了点问题。

我不知道自己有什么不好。

【问题讨论】:

  • 您是否尝试将属性前缀从spring.rabbit 更改为camel.component.rabbitmq
  • 还是同样的错误。

标签: java spring spring-boot rabbitmq apache-camel


【解决方案1】:

问题似乎是 RabbitMQComponent 期望找到 com.rabbitmq.client.ConnectionFactory 类型的连接工厂。

但是,springboot 自动配置正在创建一个 org.springframework.amqp.rabbit.connection.CachingConnectionFactory 类型的连接工厂。

因此,每当 RabbitMQComponent 尝试查找适当的连接工厂时,因为它正在寻找特定类型,并且因为它没有子类化 rabbitmq ConnectionFactory,它返回一个空值,并且无法使用适当的主机名和在 application.yml 中指定的配置参数。

You should also see the following in your log if you have debug level set:
2019-12-15 17:58:53.631 DEBUG 48710 --- [           main] o.a.c.c.rabbitmq.RabbitMQComponent       : Creating RabbitMQEndpoint with host null:0 and exchangeName: asterix
2019-12-15 17:58:55.927 DEBUG 48710 --- [           main] o.a.c.c.rabbitmq.RabbitMQComponent       : Creating RabbitMQEndpoint with host null:0 and exchangeName: asterix-sink

编辑: CachingConnectionFactory 配置了所需的 Rabbit 连接工厂作为自动配置的一部分。但是,您需要提供指向正确工厂的链接。

因此,您需要添加一个@Bean 来消除歧义。

@Configuration
@RequiredArgsConstructor
public class CamelConfig {

  private final CachingConnectionFactory rabbitConnectionFactory;

  @Bean
  com.rabbitmq.client.ConnectionFactory rabbitSourceConnectionFactory() {
    return rabbitConnectionFactory.getRabbitConnectionFactory();
  }
}

在您的端点配置中:

rabbitmq:asterix?connectionFactory=#rabbitSourceConnectionFactory

请注意,# 是可选的,因为它在尝试查找 rabbit 连接工厂 bean 时会在代码中被删除。

在您的 application.yml 中,配置连接参数(该 url 不再包含在端点 URI 中)。

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

【讨论】:

    猜你喜欢
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2017-09-06
    相关资源
    最近更新 更多