'Spring boot - health endpoint - can I have more than one URI, one simple, other more detailed?
I wanted to have two health-related endpoints in my spring boot application one simple and other one in more detailed, for example -
the simple API -
GET http://localhost:8080/actuator/health
{
"status": "UP"
}
the detailed API -
GET http://localhost:8080/actuator/health-detailed
{
"status": "UP",
"components": {
"custom": {
"status": "UP"
},
"diskSpace": {
"status": "UP",
"details": {
"total": 254971625472,
"free": 60132696064,
"threshold": 10485760,
"exists": true
}
},
"ping": {
"status": "UP"
}
}
}
But I am not able to figure out how to achieve this with Spring boot actuator.
Solution 1:[1]
Yes you can have.
Health Indicators in Spring Boot
Out of the box, Spring Boot registers many HealthIndicators to report the healthiness of a particular application aspect.
Some of those indicators are almost always registered, such as DiskSpaceHealthIndicator or PingHealthIndicator. The former reports the current state of the disk and the latter serves as a ping endpoint for the application.
On the other hand, Spring Boot registers some indicators conditionally. That is if some dependencies are on the classpath or some other conditions are met, Spring Boot might register a few other HealthIndicators, too. For instance, if we're using relational databases, then Spring Boot registers DataSourceHealthIndicator. Similarly, it'll register CassandraHealthIndicator if we happen to use Cassandra as our data store.
In order to inspect the health status of a Spring Boot application, we can call the /actuator/health endpoint. This endpoint will report an aggregated result of all registered HealthIndicators.
Custom HealthIndicators
In addition to the built-in ones, we can register custom HealthIndicators to report the health of a component or subsystem. In order to that, all we have to do is to register an implementation of the HealthIndicator interface as a Spring bean.
@Component
public class RandomHealthIndicator implements HealthIndicator {
@Override
public Health health() {}
}
Solution 2:[2]
add this to your pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
and then create this class to add your custom health checks:
@Component
public class CustomHealthCheck extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder bldr) throws Exception {
// TODO implement some check
boolean running = true;
if (running) {
bldr.up();
} else {
bldr.down();
}
}
}
and first status in response of /actuator/health indicates brief status that you want.
{
"status": "UP",
"components": {
"db": {
"status": "UP",
"details": {
"database": "Oracle",
"validationQuery": "isValid()"
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": 268238319616,
"free": 40618704896,
"threshold": 10485760,
"exists": true
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Opri |
| Solution 2 | Bashir Zamani |
