我有一个支持Kafka的Spring Boot应用程序。最近,我试图使其也支持RabbitMQ。我使用@Profile
注释设置了代码,以便只有当我选择spring.profiles.active
为rabbit-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]
我有两个问题:
@Profile
进行的设置无法正常工作?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。有办法吗?
答案 0 :(得分:2)
您要么需要从类路径中排除spring-rabbit jar,要么通过从RabbitAutoConfiguration
中排除@SpringBootApplication
来禁用Rabbitmq自动配置。
更新:禁用健康检查
请参见boot properties documentation。将management.health.rabbit.enabled
设置为false