How to use Spring Boot Actuator for Management Endpoints
We can say that Spring Boot Actuator is a module or sub project of Spring Boot which provide many production grade services. It provides several management HTTP endpoints like application health, bean details, version details, configurations, logger details, etc.which we can use for interacting and monitoring our applications.
Enabling Spring Boot Actuator
Required Dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>
HAL Browser provides a User Interface for viewing the management endpoints.
Below is the URL for HAL Browser UI
In the HAL Browser UI, There are 2 group buttons: GET and NON-
GET. GET is used to navigate to a resource of the collection.
NON-GET button is used to do a {POST, PUT, DELETE} request on the collection.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.gaurav.boot</groupId>
<artifactId>actuator-hal-browser-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>actuator-hal-browser-demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
ActuatorDemoController.java
package com.gaurav.boot.actuatorhalbrowserdemo;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/rest")
public class ActuatorDemoController {
@GetMapping("/greeting")
public String greetingMessage(@RequestParam String name){
return "Welcome "+name+" in the world of Spring Boot";
}
private static Map<Long, String> employeeMap = new HashMap<Long, String>();
@GetMapping(path = "/list/employee")
public String listEmployees() {
employeeMap.put(new Long(1001), "Gaurav");
employeeMap.put(new Long(1234), "Aaditya");
employeeMap.put(new Long(11), "Shivam");
employeeMap.put(new Long(101), "Kumar");
return employeeMap.toString();
}
}
ActuatorHalBrowserDemoApplication.java
package com.gaurav.boot.actuatorhalbrowserdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ActuatorHalBrowserDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ActuatorHalBrowserDemoApplication.class, args);
}
}
App URL for greeting method:-
http://localhost:8080/rest/greeting?name=Aaditya
Actuator URl - http://localhost:8080/actuator
HAL Browser URL - http://localhost:8080/browser/index.html
No comments:
Post a Comment