Sunday 22 September 2019

Spring boot Actuator: Customize health API

By implementing HealthIndicator interface, we can add custom health responses to the health endpoint.

Example
@Component
public class AuthServiceHealth implements HealthIndicator {

  @Override
  public Health health() {
    return Health.down().outOfService().build();
  }

}

Let’s build an application and see how it works in spring boot.

Step 1: Create new maven project ‘actuatorDemo’.

Step 2: Update pom.xml with maven dependencies.
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
   <optional>true</optional>
  </dependency>

  <dependency>
   <groupId>com.h2database</groupId>
   <artifactId>h2</artifactId>
   <scope>runtime</scope>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>

 </dependencies>

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>actuatorDemo</groupId>
 <artifactId>actuatorDemo</artifactId>
 <version>1</version>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
  <relativePath />
 </parent>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
   <optional>true</optional>
  </dependency>

  <dependency>
   <groupId>com.h2database</groupId>
   <artifactId>h2</artifactId>
   <scope>runtime</scope>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>

 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
     <execution>
      <goals>
       <goal>build-info</goal>
      </goals>
     </execution>
    </executions>
   </plugin>

   <plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>

    <executions>
     <execution>
      <id>get-the-git-infos</id>
      <goals>
       <goal>revision</goal>
      </goals>
     </execution>
    </executions>

    <configuration>
     <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
     <prefix>git</prefix>
     <verbose>false</verbose>
     <generateGitPropertiesFile>true</generateGitPropertiesFile>
     <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
     <format>json</format>
     <gitDescribe>
      <skip>false</skip>
      <always>false</always>
      <dirty>-dirty</dirty>
     </gitDescribe>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

Step 3: Create a package ‘com.sample.app’ and define App.java like below.

App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
 public static void main(String[] args) {
  
  SpringApplication.run(App.class, args);
 }
}

Step 4: Create a package com.sample.app.controller and define HelloController like below.

HelloController.java    
package com.sample.app.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

 @RequestMapping("/")
 public String home() {
  return "Welcome to spring boot developement";
 }

}

Step 5: Create application.properties file under src/main/resources folder.

application.properties    
info.team.name=Alaska
info.team.size=23
info.team.dev=krishna, ram, Sunil, Bikash
info.team.test=hari, pani, panth

info.version=1.23
info.product.owner=Ashwani
info.product.architect=Surav

management.endpoint.health.show-details=always

Step 6: Create a package ‘com.sample.app.components’ and define AuthServiceHealth.java.

AuthServiceHealth.java
package com.sample.app.components;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class AuthServiceHealth implements HealthIndicator {

  @Override
  public Health health() {
    return Health.down().outOfService().build();
  }

}

Total project structure looks like below.
Run App.java.

Open the url ‘http://localhost:8080/actuator/health/’ in browser, you will see below response.    
{
  "status": "OUT_OF_SERVICE",
  "details": {
    "authServiceHealth": {
      "status": "OUT_OF_SERVICE"
    },
    "db": {
      "status": "UP",
      "details": {
        "database": "H2",
        "hello": 1
      }
    },
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 499963174912,
        "free": 303654981632,
        "threshold": 10485760
      }
    }
  }
}



As you see overall status of the application is ‘OUT_OF_SERVICE’.

You can download complete working application from this link.
    

Previous                                                    Next                                                    Home

No comments:

Post a Comment