首先引入相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
然后需要编写mq相关的一个配置类
@Configuration
public class RabbitMQConfig {
@Autowired
private RabbitDataSourceService rabbitDataSourceService;
@Bean(name="firstConnectionFactory")
@Primary
public ConnectionFactory firstConnectionFactory(@Value("${spring.rabbitmq.first.param}") Long param){
RabbitDataSource config = getConfig(param);
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(config.getHost());
connectionFactory.setPort(config.getPort());
connectionFactory.setUsername(config.getUsername());
connectionFactory.setPassword(config.getPassword());
return connectionFactory;
}
@Bean(name="firstRabbitTemplate")
@Primary
public RabbitTemplate firstRabbitTemplate(@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory){
return new RabbitTemplate(connectionFactory);
}
@Bean(name="firstFactory")
public SimpleRabbitListenerContainerFactory firstFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
}
@Bean(name="secondConnectionFactory")
public ConnectionFactory secondConnectionFactory(@Value("${spring.rabbitmq.second.param}") Long param){
RabbitDataSource config = getConfig(param);
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(config.getHost());
connectionFactory.setPort(config.getPort());
connectionFactory.setUsername(config.getUsername());
connectionFactory.setPassword(config.getPassword());
return connectionFactory;
}
@Bean(name="secondRabbitTemplate")
public RabbitTemplate secondRabbitTemplate(@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory){
return new RabbitTemplate(connectionFactory);
}
@Bean(name="secondFactory")
public SimpleRabbitListenerContainerFactory secondFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
}
/**
* 获取对应数据源配置信息
* @param id
* @return
*/
private RabbitDataSource getConfig(Long id) {
return rabbitDataSourceService.getById(id);
}
然后再去继承AbstractHealthIndicator,来写对应自己需要的检测信息;
@Component
public class FirstRabbitHealthIndicator extends AbstractHealthIndicator {
@Autowired
@Qualifier("firstRabbitTemplate")
private final RabbitTemplate rabbitTemplate;
public FirstRabbitHealthIndicator(RabbitTemplate rabbitTemplate) {
super("Rabbit health check failed");
Assert.notNull(rabbitTemplate, "RabbitTemplate must not be null");
this.rabbitTemplate = rabbitTemplate;
}
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
builder.up().withDetail("channelMax", getConnection().getChannelMax());
builder.up().withDetail("frameMax", getConnection().getFrameMax());
builder.up().withDetail("heartbeat", getConnection().getHeartbeat());
builder.up().withDetail("address", getConnection().getAddress());
builder.up().withDetail("version", getConnection().getServerProperties().get("version").toString());
builder.up().withDetail("server", (Map<String, Object>) getConnection().getServerProperties().get("capabilities"));
builder.up().withDetail("client", (Map<String, Object>) getConnection().getClientProperties().get("capabilities"));
}
private Connection getConnection() {
return this.rabbitTemplate.execute((channel) -> channel.getConnection());
}
}
@Component
public class SecondRabbitHealthIndicator extends AbstractHealthIndicator {
@Autowired
@Qualifier("secondRabbitTemplate")
private final RabbitTemplate rabbitTemplate;
public SecondRabbitHealthIndicator(RabbitTemplate rabbitTemplate) {
super("Rabbit health check failed");
Assert.notNull(rabbitTemplate, "RabbitTemplate must not be null");
this.rabbitTemplate = rabbitTemplate;
}
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
builder.up().withDetail("channelMax", getConnection().getChannelMax());
builder.up().withDetail("frameMax", getConnection().getFrameMax());
builder.up().withDetail("heartbeat", getConnection().getHeartbeat());
builder.up().withDetail("address", getConnection().getAddress());
builder.up().withDetail("version", getConnection().getServerProperties().get("version").toString());
builder.up().withDetail("server", (Map<String, Object>) getConnection().getServerProperties().get("capabilities"));
builder.up().withDetail("client", (Map<String, Object>) getConnection().getClientProperties().get("capabilities"));
}
private Connection getConnection() {
return this.rabbitTemplate.execute((channel) -> channel.getConnection());
}
}