Spring Boot – Actuator | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

Actuator endpoints let you monitor and interact with your application. Spring Boot includes a number of built-in endpoints and lets you add your own. For example, the health endpoint provides basic application health information.

By default, the health endpoint is mapped to /actuator/health.

The following table shows the default exposure for the built-in endpoints :

IDJMXWEB
auditevents YesNo
beans YesNo
caches YesNo
conditions YesNo
configprops YesNo
env YesNo
flyway YesNo
health YesYes
heapdump N/ANo
httptrace YesNo
info YesYes
integrationgraph YesNo
jolokia N/ANo
logfile N/ANo
loggers YesNo
liquibase YesNo
metrics YesNo
mappings YesNo
prometheus N/ANo
scheduledtasks YesNo
sessions YesNo
shutdown YesNo
threaddump YesNo

To change which endpoints are exposed, use the following technology-specific include and exclude properties :

PropertyDefault
management.endpoints.jmx.exposure.exclude
management.endpoints.jmx.exposure.include*
management.endpoints.web.exposure.exclude
management.endpoints.web.exposure.includeinfo, health

To stop exposing all endpoints over JMX and only expose the health and info endpoints, use the following property :

management.endpoints.jmx.exposure.include=health,info

* can be used to select all endpoints. For example, to expose everything over HTTP except the env and beans endpoints, use the following properties :

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans

* has a special meaning in YAML, so be sure to add quotes if you want to include (or exclude) all endpoints, as shown in the following example :

management:
  endpoints:
    web:
      exposure:
        include: "*"

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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.actuator</groupId>
	<artifactId>Actuator</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Actuator</name>
	<description>Actuator Demo project for Spring Boot</description>

	<properties>
		<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.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>

application.yml

management:
  endpoints:
    web:
      exposure:
        include: "*"

ActuatorApplication.java

package com.actuator;

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

@SpringBootApplication
public class ActuatorApplication {

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

}

CustomActuator.java

package com.actuator;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@Endpoint(id = "customPoint")
@Component
public class CustomActuator {

	@ReadOperation
	public String customPoint() {
		return "Hello";
	}
}

ActuatorController.java

package com.actuator.controller;

import java.util.Date;

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

@RestController
public class ActuatorController {

	@GetMapping("/example")
	public String example() {
		return "Hello User !! " + new Date();
	}

}

Reference Links :
1. https://start.spring.io
2. https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s