Springboot健康检查多数据源mq相关配置

首先引入相关依赖

        <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());
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值