自动检测出RabbitConnectionFactory的JMX暴露

时间:2019-05-07 13:54:46

标签: spring-boot rabbitmq spring-amqp

我有一个支持Kafka的Spring Boot应用程序。最近,我试图使其也支持RabbitMQ。我使用@Profile注释设置了代码,以便只有当我选择spring.profiles.activerabbit-mq时,RabbitMQ的所有新代码才应处于活动状态。同样,特定于Kafka的代码由kafka的配置文件值标记出来

我惊讶地发现,即使使用上述设置,当我将配置文件设置为kafka时,仍通过 JMX暴露机制包含并激活了某些RabbitMQ。具体来说,首先构造了一个rabbitConnectionFactory bean,然后它尝试与localhost:5672处的RabbitMQ代理进行健康检查,但失败了。

在日志文件中,我看到了以下消息:

... o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
... o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'rabbitConnectionFactory' has been autodetected for JMX exposure
... o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'rabbitConnectionFactory': registering with JMX server as MBean [org.springframework.amqp.rabbit.connection:name=rabbitConnectionFactory,type=CachingConnectionFactory]
... o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483547
... o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
... o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
... c.s.datacomparatorproducer.Application   : Started Application in 5.175 seconds (JVM running for 5.663)
... o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
... o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
... o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms
... o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
... o.s.b.a.amqp.RabbitHealthIndicator       : Rabbit health check failed

org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)
        at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:62) ~[spring-rabbit-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
        at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:476) ~[spring-rabbit-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
        at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:614) ~[spring-rabbit-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
        at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:240) ~[spring-rabbit-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
        at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1797) ~[spring-rabbit-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
        at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1771) ~[spring-rabbit-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]

我有两个问题:

  1. 如何避免包含RabbitMQ代码?为什么我使用@Profile进行的设置无法正常工作?
  2. 如何配置rabbitConnectionFactory?目前,它正在尝试与localhost:5672进行通话。我通常知道如何设置Spring模板以将application-xxx.properties用于spring.rabbitmq.{host,port},但是在这种情况下,由于代码是自动包含的,所以我不知道如何配置{{1 }}

常规配置

rabbitConnectionFactory

更新

尝试1:排除spring.rabbitmq.host=someRabbitBroker spring.rabbitmq.port=5672

谢谢你的建议。我尝试了他的方法,并按如下方式更改了RabbitAutoConfiguration。这里的想法是在未定义@SpringBootApplication时(当RabbitAutoConfiguration的配置文件未激活时)排除spring.rabbitmq.host

rabbit-mq

我不确定这段代码是否是正确的方法,但是没有用。当我的应用启动时,我仍然在消息中看到这些:

@SpringBootApplication
public class Application {

    @ConditionalOnProperty(value="spring.rabbitmq.host")
    @Bean 
    RabbitAutoConfiguration rabbitAutoConfiguration(){
        return new RabbitAutoConfiguration();
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

只要可以停止进行运行状况检查(或至少将其配置为使用我指定的主机和端口),我就可以构造bean。有办法吗?

1 个答案:

答案 0 :(得分:2)

您要么需要从类路径中排除spring-rabbit jar,要么通过从RabbitAutoConfiguration中排除@SpringBootApplication来禁用Rabbitmq自动配置。

更新:禁用健康检查

请参见boot properties documentation。将management.health.rabbit.enabled设置为false