Spring Boot之IntelliJ IDEA RabbitMQ

问题描述

在使用spring boot中使用spring-boot-starter-amqp 处理RabbitMQ操作时,使用以下代码创建DirectExchange

@Bean("directExchange")
public DirectExchange directExchange() {
  return new DirectExchange("test", true, false);
}
  • 目的:当服务启动时自动创建相应DirectExchange
  • 结果:本地使用IntelliJ IDEA启动服务可以创建,使用maven package打包后,用java -jar启动服务后,没有创建

原因分析

相关依赖

去掉spring-boot-starter-actuator依赖后就都无法创建

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

IntelliJ IDEA 文档

具体说明:https://www.jetbrains.com/help/idea/spring-boot.html#spring-boot-endpoints

image

Spring Boot文档

具体说明: https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html

image

结论

因为Spring Boot actuator默认暴露JMX所有节点,IntelliJ IDEA启动默认开启JMX,而用命令java -jar 默认不会开启JMX.这样在IntelliJ IDEA启动Spring Boot 后,触发了org.springframework.boot.actuate.health.HealthEndpoint 从而使用了默认的org.springframework.amqp.rabbit.connection.CachingConnectionFactory#createConnection 获取RabbitMQ连接,并创建相关DirectExchange

posted @ 2022-01-12 22:03  Ken-LK  阅读(235)  评论(0编辑  收藏  举报