如何在SpringBoot中禁用单个运行状况指示器?

lf5gs5x2  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(299)

我需要为一个数据源禁用springboot运行状况指示器(我们有很多数据源,如果可能的话,我不想全部禁用它们或为其他数据源创建指示器)。
我采用的方法是扩展datasourcehealthcontributorautoconfiguration。我知道在datasourcehealthindicatorautoconfiguration运行时,这个功能曾经起作用,但现在它是一个子接口,不能按预期工作。
预期:将不检查使用雪花驱动程序的数据源的运行状况指示器
事实:数据源在启动时看起来确实被过滤掉了,但是,createindicator仍然在为所有数据源调用。
如果需要更多信息,请告诉我。这是我第一次尝试更改java代码,因此可能会遗漏一些内容。

package com.org.database.snowflake.config;

import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration;
import java.util.Map;
import java.util.stream.Collectors;

@Configuration
public class ExcludeSnowflakeHealthIndicatorConfig extends DataSourceHealthContributorAutoConfiguration {

    public ExcludeSnowflakeHealthIndicatorConfig(Map<String, DataSource> dataSources, ObjectProvider<DataSourcePoolMetadataProvider> metadataProviders) {
        // Filter out data sources that use the Snowflake Driver
        super(filterDataSources(dataSources), metadataProviders);
    }

    private static Map<String, DataSource> filterDataSources(Map<String, DataSource> dataSources) {
        return dataSources.entrySet().stream()
                .filter(dataSourceEntry -> {
                    if (dataSourceEntry.getValue() instanceof HikariDataSource) {
                        HikariDataSource hikariDataSource = (HikariDataSource) dataSourceEntry.getValue();
                        return !hikariDataSource.getDriverClassName().equals("net.snowflake.client.jdbc.SnowflakeDriver");
                    } else {
                        return true;
                    }
                }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }
}
gijlo24d

gijlo24d1#

这似乎奏效了:

package com.org.database.snowflake.config;

import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.health.HealthContributor;
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration;
import java.util.Map;
import java.util.stream.Collectors;

@Configuration
public class ExcludeSnowflakeHealthIndicatorConfig extends DataSourceHealthContributorAutoConfiguration {
    private final Map<String, DataSource> ds;

    public ExcludeSnowflakeHealthIndicatorConfig(Map<String, DataSource> dataSources, ObjectProvider<DataSourcePoolMetadataProvider> metadataProviders) {
        // Filter out data sources that use the Snowflake Driver
        super(filterDataSources(dataSources), metadataProviders);
        this.ds = filterDataSources(dataSources);
    }

    private static Map<String, DataSource> filterDataSources(Map<String, DataSource> dataSources) {
        return dataSources.entrySet().stream()
                .filter(dataSourceEntry -> {
                    if (dataSourceEntry.getValue() instanceof HikariDataSource) {
                        HikariDataSource hikariDataSource = (HikariDataSource) dataSourceEntry.getValue();
                        return !hikariDataSource.getDriverClassName().equals("net.snowflake.client.jdbc.SnowflakeDriver");
                    } else {
                        return true;
                    }
                }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }

    @Bean
    public HealthContributor dbHealthContributor(Map<String, DataSource> dataSources) {
        return super.dbHealthContributor(ds);
    }
}

相关问题