Spring Boot Actuators – Customizing Actuator Endpoint
SpringBoot – Actuators (Example here)
Spring Boot Actuator brings in several production grade services to your application. Spring Boot Actuators has various number of built-in endpoints.
Actuators helps us monitor and interact with your application. Spring Boot also lets us to create our own endpoints too. These end points can be both enabled or disabled.
We have already seen in-build actuator endpoints here – Spring Boot Actuators – in-built EndPoint.
Let us see how to customize the already existing endpoint. Let us override the /actuator/health endpoint.
To override the health endpoint we will be extending a class – AbstractHealthIndicator
AbstractHealthIndicator:
Class which has base health indicator implementations and encapsulates creation of health and error handling. This will override the method – protected abstract void doHealthCheck(Health.Builder builder) throws Exception.
Let us see an example,
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SpringBootHealth</groupId> <artifactId>SpringBootHealth</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringBootHealth</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties
management.endpoints.web.exposure.include=*
ControllerSample.java
package com.javainfinite.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ControllerSample { @GetMapping(value = "/") public String showStatus() { return "Lets check "; } }
App.java
package com.javainfinite.App; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan("com.javainfinite.*") public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
Now without customizing endpoint, if we run the application
Now let us override the health endpoint,
package com.javainfinite.Health; import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.Health.Builder; import org.springframework.stereotype.Component; @Component public class HealthIndicator extends AbstractHealthIndicator { @Override protected void doHealthCheck(Builder builder) throws Exception { int i = 0; // implement the required logic, here i am using sample check to make it down if (i != 0) { builder.up(); } else { builder.down(); } } }
Output after overriding the healthIndicator,
Download this example here