Sunday, 22 September 2019

Spring boot Actuator: health endpoint


‘health’ endpoint is used to check the liveliness of the application. One advantage of health API is, if you dependent on other services like database, jms queues, it automatically checks the liveliness of the dependent systems and give us the status.

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


Total project structure looks like below.

Run App.java.


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

As you see the response ‘health’ API is giving information about H2 database, disk space.

You can download complete working application from this link.



Previous                                                    Next                                                    Home

No comments:

Post a Comment