Spring Boot Actuator Health Indicator

后端 未结 3 904
误落风尘
误落风尘 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条回答
  •  萌比男神i
    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;
        }
    

提交回复
热议问题