java Spring 启动执行器 MySQL 数据库健康检查
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39631355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot actuator MySQL database health check
提问by Harshil
I have designed one demo spring boot application CRUD operation with MySQL database. My application.properties file is as follow.
我设计了一个使用 MySQL 数据库的演示 Spring Boot 应用程序 CRUD 操作。我的 application.properties 文件如下。
spring.boot.admin.url=http://localhost:8081
spring.datasource.url= jdbc:mysql://localhost:3306/springbootdb
spring.datasource.username=root
spring.datasource.password=admin
endpoints.health.sensitive=false
management.health.db.enabled=true
management.health.defaults.enabled=true
management.health.diskspace.enabled=true
spring.jpa.hibernate.ddl-auto=create-drop
POM.xml for the spring actuator is as follow.
弹簧执行器的 POM.xml 如下。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.3.4</version>
</dependency>
When I try to hit the url "http://localhost:8080/health", I am getting {"status":"UP"} as response. I want to monitor my database (MySQL) with spring boot actuator. I want see my database status.
当我尝试点击 url " http://localhost:8080/health" 时,我收到 {"status":"UP"} 作为响应。我想用弹簧启动执行器监控我的数据库(MySQL)。我想查看我的数据库状态。
Can anyone please help ?
任何人都可以帮忙吗?
回答by Stephane Nicoll
I would check the documentation- Exposing the full details anonymously requires to disable the security of the actuator.
我会检查文档- 匿名公开完整细节需要禁用执行器的安全性。
If that's not what you want, you can take full control of the security and write your own rules using Spring Security.
如果这不是您想要的,您可以完全控制安全性并使用 Spring Security 编写自己的规则。
回答by erdinc
"management.endpoint.health.show-details=always" add your application.properties
“management.endpoint.health.show-details=always” 添加您的 application.properties
回答by Freelancer
If you are using spring security, then security is by default enabled for actuator.
如果您使用的是 spring 安全性,那么执行器默认启用安全性。
Add this in your properties file -
将此添加到您的属性文件中 -
management.security.enabled= false
OR
或者
Add username and password in properties file -
在属性文件中添加用户名和密码 -
security.user.name= username
security.user.password = password
and use these credentials to access actuator endpoints.
并使用这些凭据访问执行器端点。
回答by Mukesh Kumar Gupta
I have used my own way of checking a database connection. Below solutions are very simple you can modify as according to your need. I know this is not specific to actuators, though it is the kinda same approach to solve when you don't wanna use the actuators.
我使用了我自己的方式来检查数据库连接。以下解决方案非常简单,您可以根据需要进行修改。我知道这不是执行器特有的,尽管当您不想使用执行器时,这是一种解决方法。
@RestController
@RequestMapping("/api/db/")
public class DbHealthCheckController {
@Autowired
JdbcTemplate template;
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
@GetMapping("/health")
public ResponseEntity<?> dbHealthCheck() {
LOGGER.info("checking db health");
try {
int errorCode = check(); // perform some specific health check
if (errorCode != 1)
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ApiResponse(false, "down"));
return ResponseEntity.ok(new ApiResponse(true, "Up"));
} catch (Exception ex) {
ex.printStackTrace();
LOGGER.error("Error Occured" + ex.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ApiResponse(false, "Down"));
}
}
public int check() {
List<Object> results = template.query("select 1 from dual", new
SingleColumnRowMapper<>());
return results.size();
}
}
ApiResponse
is a simple POJO class with 2 attributes Boolean success
and String message
.
ApiResponse
是一个简单的 POJO 类,具有 2 个属性Boolean success
and String message
。
回答by nekperu15739
In spring version 2.2.x is:
在 spring 版本 2.2.x 中是:
management.endpoint.health.show-details=always
by default this prop has value never.
默认情况下,此道具的值从不。
回答by fuat
When call http://localhost:8080/actuatorendpoint, there must be a endpoint like below.
当调用http://localhost:8080/actuator端点时,必须有一个如下所示的端点。
"health-component": {
"href": "http://localhost:8080/actuator/health/{component}",
"templated": true
}
In my case i use mongodb in my application. And i call http://localhost:8080/actuator/health/mongoendpoint it returns mongodb health.
就我而言,我在我的应用程序中使用 mongodb。我调用http://localhost:8080/actuator/health/mongo端点,它返回 mongodb 健康状况。
{
"status": "UP",
"details": {
"version": "2.6.0"
}
}