Spring Boot Actuator – Production Ready Features

Actuator

Spring Boot Actuator that includes additional features to help you monitor and manage your application in production. You can monitor your application’s auditing, health, and metrics by using HTTP endpoints or with JMX.

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

Endpoints

Spring Boot provides built-in endpoints as well as lets you add your own. You can check a list of endpoints at https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

Enabling Endpoints

By default all end points except for shutdown are enabled. You can enable a particular endpoint using management.endpoint.enabled property in your properties file. For example, if you want to enable the shutdown endpoint then you can use management.endpoint.shutdown.enabled=true.

If you want to disable all default endpoints then you can use management.endpoints.enabled-by-default=false and you can enable individual endpoint as per your requirements.

Endpoints – to expose or not

Since Endpoints may contain sensitive information, careful consideration should be given about when to expose them. The default exposure for the built-in endpoints can be found at https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html.

Now you will see how to work with Spring Boot Actuator by creating examples.

Prerequisites

Java 12/19, Spring Boot 2.2.5/3.1.4, Gradle 6.1.1, Maven 3.6.3/3.8.5

Project Setup

Create a gradle or maven based project in your favorite IDE or tool. The name of the project is spring-boot-actuator.

For spring boot 3.1.4, the following pom.xml file can be used for your project:

<?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.roytuts</groupId>
	<artifactId>spring-boot-actuator</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>19</maven.compiler.source>
		<maven.compiler.target>19</maven.compiler.target>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.1.4</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>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

Once your project creation is done then update the build.gradle script with the following code:

buildscript {
	ext {
		springBootVersion = '2.2.5.RELEASE'
	}
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
    implementation("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}")
}

In the above build script I have added the required dependencies – web and actuator starters.

If you are creating maven based project then you can use below pom.xml file:

<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.roytuts</groupId>
	<artifactId>spring-boot-actuator</artifactId>
	<version>0.0.1-SNAPSHOT</version>

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

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

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

    <build>
        <plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>at least 8</source>
					<target>at least 8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Spring Boot Main Class

Create below main class to start up the Spring Boot application. A class is having main method with @SpringBootApplication is enough to deploy the application into embedded Tomcat server.

@SpringBootApplication
public class ActuatorApplication {

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

}

Testing the Actuator Application

Run the above main class that will deploy the application and start the server at default Tomcat server’s port 8080.

Now when you hit the URL http://localhost:8080/actuator/health, then you will see the following output on the browser:

spring boot actuator - production ready features

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

From the above output, it is obvious that the application is up and running.

If you hit /actuator/info that displays arbitrary application’s info but here it won’t show anything because the application does not have any information available to to be shown.

Note that in Spring Boot 3.x, the /actuator/info is not enabled by default, you need to enable it explicitly. You can enable all endpoints by putting the following config into the application.properties file:

management.endpoints.web.exposure.include=*
spring boot actuator

Now if you add below information into classpath file application.properties:

info.app.name=Spring Boot Actuator Example
info.app.java.version=12
info.app.type=Spring Boot 2.2.5

Hit the same URL in the browser, you will see below output on the browser:

spring boot actuator

Note for Spring Boot 3.x, you need to enable the InfoContributor bean to show the additional or rather custom information added to the endpoint /info by putting the following line into the application.properties file:

management.info.env.enabled=true

Now put the following information to the application.properties file:

info.app.name=Spring Boot Actuator Example
info.app.encoding=@project.build.sourceEncoding@
info.app.java.version=19.0.1
info.app.type=Spring Boot 3.1.4

Get the above information displayed at the endpoint /actuator/info:

spring boot actuator

Discovery Page

A “discovery page” is added with links to all the endpoints. The “discovery page” is available on /actuator by default. This endpoint shows you all endpoint links for your Spring Boot application.

So you will get the below output for our application:

spring boot actuator

Note that for spring boot 3.x, the /info endpoint is not enabled by default so you won’t see the information for /info endpoint (it is assumed that you have not enabled explicitly the /info endpoint in application.properties file):

spring boot actuator

Customizing Base Path of Endpoints

By default the base path of the Actuator is /actuator but you can change it to other. For example, if you want to point Actuator’s base path to /management then you can add below into application.properties file.

management.endpoints.web.base-path=/management

Now you can access URL as http://localhost:8080/management/info.

Exposing Other Endpoints

By default for security reasons only /info and /health endpoints are enabled over HTTP. If you want to expose other endpoints then you can add below properties to application.properties file.

If you want to expose all endpoints then add:

management.endpoints.web.exposure.include=*

Now you would be able to access all endpoints over HTTP.

spring boot actuator

If you want specific endpoints to be enabled then you can add specific endpoints:

management.endpoints.web.exposure.include=health,info,beans,env

Now you would be able to access above endpoints over HTTP.

spring boot actuator

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *