Harshil
Harshil

Reputation: 473

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 ?

Upvotes: 13

Views: 43904

Answers (6)

Mukesh Kumar Gupta
Mukesh Kumar Gupta

Reputation: 1647

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"));
    }
}

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

Upvotes: 3

erdinc
erdinc

Reputation: 61

Add this configuration to your application.properties file

management.endpoint.health.show-details=always 

Upvotes: 5

nekperu15739
nekperu15739

Reputation: 3698

In spring version 2.2.x is:

management.endpoint.health.show-details=always

by default this prop has value never.

Upvotes: 3

fuat
fuat

Reputation: 1842

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"
  }
}

Upvotes: 0

Derrick
Derrick

Reputation: 4417

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.

Upvotes: 1

Stephane Nicoll
Stephane Nicoll

Reputation: 33091

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.

Upvotes: 7

Related Questions