首页 文章

Spring Boot Actuator的DataSourcesHealthIndicatorConfiguration没有找到dataSource bean

提问于
浏览
1

我已将Actuator添加到现有的Spring Boot(v1.2.2)项目中 . 我的目标是从/ health endpoints 查看数据库信息 . 目前该 endpoints 返回:

{
    "status": "UP",
    "diskSpace": {
        "status": "UP",
        "free": 400667607040,
        "threshold": 10485760
    }
}

我已将这些属性添加到项目的属性文件中:

management.health.db.enabled=true
endpoints.health.enabled=true
endpoints.health.sensitive=false

该项目已经有一个@Configuration类,可以创建一个DataSource bean,如下所示:

@Import({
    HealthIndicatorAutoConfiguration.class,
    ...
@Configuration
public class FooConfig {

    @Bean
    public DataSource dataSource() {
        final HikariConfig hikariConfig = new HikariConfig();
        ...
        return new HikariDataSource(hikariConfig);
    }

@EnableAutoConfiguration注释未在项目中的任何位置使用 .

我可以从beans endpoints 看到'dataSource'bean:

{
    "bean": "dataSource",
    "scope": "singleton",
    "type": "com.zaxxer.hikari.HikariDataSource",
    "resource": "com.example.FooConfig",
    "dependencies": []
},

/ configprops endpoints 包含以下内容:

"healthEndpoint": {
    "prefix": "endpoints.health",
    "properties": {
        "timeToLive": 1000,
        "id": "health",
        "sensitive": false
    }
},

 "diskSpaceHealthIndicatorProperties": {
    "prefix": "management.health.diskspace",
    "properties": {
        "path": "/bar/foo/.",
        "threshold": 10485760
    }
}

但没有dataSource的迹象; ^(

该项目使用的是MySql DB,因此它可以正确响应“SELECT 1”查询 .

我想知道在构建HealthIndicators之后是否正在创建dataSource bean但是到目前为止我的Spring foo已经失败了 .

我宁愿不要写一个自定义的HealthIndicator,但如果必须的话 .

我还将Actuator添加到一个示例项目中,不同之处在于它没有显式创建DataSource bean,而是使用了许多@ Repository标记的接口,这些接口使用H2 DB扩展了CrudRepository,并且它只是工作,例如

{
    "status": "UP",
    "diskSpace": {
        "status": "UP",
        "free": 400610689024,
        "threshold": 10485760
    },
    "db": {
        "status": "UP",
        "database": "H2",
        "hello": 1
    }
}

正如预期的那样,示例项目的/ configprops endpoints 返回dataSource和diskSpace属性:

"dataSource": {
    "prefix": "spring.datasource",
    "properties": {
        "connectionProperties": null,
        "propagateInterruptState": false,
        "validator": null,
        "useDisposableConnectionFacade": true,
        "defaultCatalog": null,
        "validationInterval": 30000,
        "jmxEnabled": true,
        "ignoreExceptionOnPreLoad": false,
        "logAbandoned": false,
        "commitOnReturn": false,
        "password": "******",
        "maxIdle": 100,
        "testWhileIdle": false,
        "removeAbandoned": false,
        "poolProperties": {
            "dbProperties": {
                "user": "sa",
                "password": "******"
            },
            "url": "jdbc:h2:~/test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE",
            "driverClassName": "org.h2.Driver",
            "defaultAutoCommit": null,
            "defaultReadOnly": null,
            "defaultTransactionIsolation": -1,
            "defaultCatalog": null,
            "connectionProperties": null,
            "initialSize": 10,
            "maxActive": 100,
            "maxIdle": 100,
            "minIdle": 10,
            "maxWait": 30000,
            "validationQuery": null,
            "validationQueryTimeout": -1,
            "validatorClassName": null,
            "validator": null,
            "testOnBorrow": false,
            "testOnReturn": false,
            "testWhileIdle": false,
            "timeBetweenEvictionRunsMillis": 5000,
            "numTestsPerEvictionRun": 0,
            "minEvictableIdleTimeMillis": 60000,
            "accessToUnderlyingConnectionAllowed": true,
            "removeAbandoned": false,
            "removeAbandonedTimeout": 60,
            "logAbandoned": false,
            "name": "Tomcat Connection Pool[1-1043530337]",
            "password": "******",
            "username": "sa",
            "validationInterval": 30000,
            "jmxEnabled": true,
            "initSQL": null,
            "testOnConnect": false,
            "jdbcInterceptors": null,
            "fairQueue": true,
            "useEquals": true,
            "abandonWhenPercentageFull": 0,
            "maxAge": 0,
            "useLock": false,
            "suspectTimeout": 0,
            "dataSource": null,
            "dataSourceJNDI": null,
            "alternateUsernameAllowed": false,
            "commitOnReturn": false,
            "rollbackOnReturn": false,
            "useDisposableConnectionFacade": true,
            "logValidationErrors": false,
            "propagateInterruptState": false,
            "ignoreExceptionOnPreLoad": false
        },
        "minIdle": 10,
        "abandonWhenPercentageFull": 0,
        "defaultReadOnly": null,
        "pool": {
            "poolProperties": {
                "dbProperties": {
                    "user": "sa",
                    "password": "******"
                },
                "url": "jdbc:h2:~/test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE",
                "driverClassName": "org.h2.Driver",
                "defaultAutoCommit": null,
                "defaultReadOnly": null,
                "defaultTransactionIsolation": -1,
                "defaultCatalog": null,
                "connectionProperties": null,
                "initialSize": 10,
                "maxActive": 100,
                "maxIdle": 100,
                "minIdle": 10,
                "maxWait": 30000,
                "validationQuery": null,
                "validationQueryTimeout": -1,
                "validatorClassName": null,
                "validator": null,
                "testOnBorrow": false,
                "testOnReturn": false,
                "testWhileIdle": false,
                "timeBetweenEvictionRunsMillis": 5000,
                "numTestsPerEvictionRun": 0,
                "minEvictableIdleTimeMillis": 60000,
                "accessToUnderlyingConnectionAllowed": true,
                "removeAbandoned": false,
                "removeAbandonedTimeout": 60,
                "logAbandoned": false,
                "name": "Tomcat Connection Pool[1-1043530337]",
                "password": "******",
                "username": "sa",
                "validationInterval": 30000,
                "jmxEnabled": true,
                "initSQL": null,
                "testOnConnect": false,
                "jdbcInterceptors": null,
                "fairQueue": true,
                "useEquals": true,
                "abandonWhenPercentageFull": 0,
                "maxAge": 0,
                "useLock": false,
                "suspectTimeout": 0,
                "dataSource": null,
                "dataSourceJNDI": null,
                "alternateUsernameAllowed": false,
                "commitOnReturn": false,
                "rollbackOnReturn": false,
                "useDisposableConnectionFacade": true,
                "logValidationErrors": false,
                "propagateInterruptState": false,
                "ignoreExceptionOnPreLoad": false
            }
        },
        "maxWait": 30000,
        "logValidationErrors": false,
        "name": "Tomcat Connection Pool[1-1043530337]",
        "driverClassName": "org.h2.Driver",
        "dataSource": null,
        "initSQL": null,
        "validationQueryTimeout": -1,
        "dbProperties": {
            "user": "sa",
            "password": "******"
        },
        "validationQuery": null,
        "rollbackOnReturn": false,
        "logWriter": null,
        "validatorClassName": null,
        "dataSourceJNDI": null,
        "alternateUsernameAllowed": false,
        "suspectTimeout": 0,
        "useEquals": true,
        "removeAbandonedTimeout": 60,
        "defaultAutoCommit": null,
        "loginTimeout": 30,
        "testOnConnect": false,
        "jdbcInterceptors": null,
        "initialSize": 10,
        "defaultTransactionIsolation": -1,
        "url": "jdbc:h2:~/test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE",
        "numTestsPerEvictionRun": 0,
        "testOnBorrow": false,
        "fairQueue": true,
        "minEvictableIdleTimeMillis": 60000,
        "timeBetweenEvictionRunsMillis": 5000,
        "maxAge": 0,
        "accessToUnderlyingConnectionAllowed": true,
        "testOnReturn": false,
        "useLock": false,
        "username": "sa",
        "maxActive": 100
    }
},
"diskSpaceHealthIndicatorProperties": {
    "prefix": "management.health.diskspace",
    "properties": {
        "path": "/foo/bar/.",
        "threshold": 10485760
    }
}

1 回答

  • 1

    感谢一位出色的同事,将以下内容添加到FooConfig解决了我的问题:

    @Bean
    public DataSourceHealthIndicator dataSourceHealthIndicator() {
        return new DataSourceHealthIndicator(dataSource());
    }
    

    即/ health endpoints 现在返回:

    {
        "status": "UP",
        "diskSpace": {
            "status": "UP",
            "free": 385221242880,
            "threshold": 10485760
        },
        "dataSource": {
            "status": "UP",
            "database": "MySQL",
            "hello": 1
        }
    }
    

相关问题