简体   繁体   中英

Spring boot actuator MySQL database health check

I have designed one demo spring boot application CRUD operation with MySQL database. My application.properties file is as follow.

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.

<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.

Can anyone please help ?

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.

"management.endpoint.health.show-details=always" 添加您的 application.properties

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 .

In spring version 2.2.x is:

management.endpoint.health.show-details=always

by default this prop has value never.

If you are using spring security, then security is by default enabled for actuator.

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.

When call http://localhost:8080/actuator endpoint, there must be a endpoint like below.

"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/mongo endpoint it returns mongodb health.

{
  "status": "UP",
  "details": {
     "version": "2.6.0"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM