Spring Boot Actuator Health Indicator

后端 未结 3 875
误落风尘
误落风尘 2021-01-14 02:04

We have been using Spring Boot for several projects now, We are using the latest version 1.2.3. We are incorporating Actuator. So far things are working well except we are f

相关标签:
3条回答
  • 2021-01-14 02:22

    The DataSourceHealthIndicator is used to check availablity. The default query is SELECT 1, but there are some product specific queries, too.
    You can write your own HealthIndicator. Either you implement the interface or extend the AbstractHealthIndicator.
    To disable the default db-health-check put this line into your application properties management.health.db.enabled=false. Hope that helps

    0 讨论(0)
  • 2021-01-14 02:41

    The above comment helpedme to init my research but for me it was not enough :

    @Bean
    @Primary
    public DataSourceHealthIndicator dataSourceHealthIndicator() {
        return new DataSourceHealthIndicator(dataSource, "SELECT 1 FROM DUAL");
    }
    

    Here is the configuration that helped me to make it run: Define HealthIndicator @Bean like the follow and provide the required query :

    @Bean
    @Primary
    public HealthIndicator dbHealthIndicator() {
      return new DataSourceHealthIndicator(dataSource, "SELECT 1 FROM DUMMY");
    }
    

    If no Query is providen the SELECT 1 will be used . As #derFuerst said will be used , Here is the defailt implementation of DataSourceHealthIndicator :

    public DataSourceHealthIndicator(DataSource dataSource, String query) {
        super("DataSource health check failed");
        this.dataSource = dataSource;
        this.query = query;
        this.jdbcTemplate = dataSource != null ? new JdbcTemplate(dataSource) : null;
    }
    ...
    protected String getValidationQuery(String product) {
            String query = this.query;
            if (!StringUtils.hasText(query)) {
                DatabaseDriver specific = DatabaseDriver.fromProductName(product);
                query = specific.getValidationQuery();
            }
    
            if (!StringUtils.hasText(query)) {
                query = "SELECT 1";
            }
    
            return query;
        }
    
    0 讨论(0)
  • 2021-01-14 02:45

    As #derFuerst said the DataSourceHealthIndicator has the default query to check whether the DB is up or not.

    If you want to use this the proper vendor specific query you should write your own health indicator in your configuration class, like this in case of Oracle data source:

    @Autowired(required = false)
    private DataSource dataSource;
    
    @Bean
    @Primary
    public DataSourceHealthIndicator dataSourceHealthIndicator() {
        return new DataSourceHealthIndicator(dataSource, "SELECT 1 FROM DUAL");
    }
    
    0 讨论(0)
提交回复
热议问题