我已将 Actuator 添加到现有的 Spring Boot (v1.2.2) 项目中。我的目标是从 /health 端点查看数据库信息。目前该端点返回:
{
"status": "UP",
"diskSpace": {
"status": "UP",
"free": 400667607040,
"threshold": 10485760
}
}
我已将这些属性添加到项目的属性文件中:
management.health.db.enabled=true
endpoints.health.enabled=true
endpoints.health.sensitive=false
该项目已经有一个创建 DataSource bean 的 @Configuration 类,如下所示:
@Import({
HealthIndicatorAutoConfiguration.class,
...
@Configuration
public class FooConfig {
@Bean
public DataSource dataSource() {
final HikariConfig hikariConfig = new HikariConfig();
...
return new HikariDataSource(hikariConfig);
}
@EnableAutoConfiguration 注解没有在项目中的任何地方使用。
我可以从 bean 端点看到“dataSource”bean:
{
"bean": "dataSource",
"scope": "singleton",
"type": "com.zaxxer.hikari.HikariDataSource",
"resource": "com.example.FooConfig",
"dependencies": []
},
/configprops 端点包含以下内容:
"healthEndpoint": {
"prefix": "endpoints.health",
"properties": {
"timeToLive": 1000,
"id": "health",
"sensitive": false
}
},
"diskSpaceHealthIndicatorProperties": {
"prefix": "management.health.diskspace",
"properties": {
"path": "/bar/foo/.",
"threshold": 10485760
}
}
但没有数据源的迹象;^(
该项目使用的是 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 端点按预期返回 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
}
}