Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

java - How to disable individual health indicator in Spring Boot?

I am in need of disabling the Spring Boot health indicator for a single data source (we have many and I don't want to disable them all or create indicators for the others, if possible).

The path I have taken is to extend the DataSourceHealthContributorAutoConfiguration. I know this used to work when DataSourceHealthIndicatorAutoConfiguration was in play, but now that it is a subinterface it is not working as expected.

Expected: Health indicator for data sources using the Snowflake driver will not be checked

Reality: The data source does appear to be filtered out on startup, however, createIndicator is still being called for all of the data sources.

Please let me know if more information is required. This is my first attempt to change java code so I might be missing some things.

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));
    }
}
question from:https://stackoverflow.com/questions/66052911/how-to-disable-individual-health-indicator-in-spring-boot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This seems to be working:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...