Spring Boot Actuator – Production Ready Features

You may also like to read:

Introduction

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 we will see how to work with Spring Boot Actuator by creating examples.

Prerequisites

Knowledge on Spring Boot, web starter and actuator starter dependencies

Java 8, Eclipse IDE

Creating Gradle Project

Project Structure

Create gradle based project in Eclipse IDE.

spring boot actuator

Gradle build file

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

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

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
    
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
	mavenLocal()
    mavenCentral()
}

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

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

Creating Main Class

Create below main class to start up the Spring Boot application.

package com.jeejava.springboot.actuator.main;

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);
	}

}

Testing the 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 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 here:

spring boot actuator

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

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

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

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

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.

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.

Source Code Download

You can download source code.

Thanks for reading.

Spring Boot Actuator – Production Ready Features
I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger and writing blogs is my passion. Connect me on Roy Tutorials | Twitter | Facebook | Linkedin | Reddit | Ello | Tumblr

Leave a Reply

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

Scroll to top