Spring boot

Spring boot actuator example

What is Spring boot Actuator

Spring Boot Actuator includes a number of additional features to help you monitor and manage your application when you push it to production.
You can manage and monitor your application by using HTTP endpoints given by spring boot or you can create your own endpoints.

Check out the supported endpoints here :
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-endpoints-exposing-endpoints

If you are developing a web application, Spring Boot Actuator auto-configures all enabled endpoints to be exposed over HTTP

Configuring actuator dependency(pom.xml)

Add actuator and spring security starters to your pom.xml or build.gradle if you are using Gradle. Since application monitoring API’s cannot be exposed publicly, its always a good practice to configure spring security.

	<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.candidjava.springboot</groupId>
	<artifactId>SpringBoot_Actuator</artifactId>
	<packaging>jar</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBoot_Actuator Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/>
	</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>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
		<finalName>SpringBoot_Actuator</finalName>
	</build>
</project>

Adding Actuator security (SpringSecurityConfig.java)

Create an in-memory authentication flow with the role actuator, Spring boot will allow this user to login through the management port.

package com.candidjava.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
 @Autowired
 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  auth.inMemoryAuthentication().withUser("admin")
   .password("{noop}admin").roles("ADMIN");
  auth.inMemoryAuthentication().withUser("act")
   .password("{noop}act").roles("ACTUATOR");

 }
}

Simple controller (HelloWorldController.java)

package com.candidjava.springboot.controller;

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

@RestController
public class HelloWorldController {

 @GetMapping(value = "/")
 public String getMessage() {
  return "Hello World";
 }
}

Launch as Spring boot (Application.java)

Run this application and hit localhost:8080/ in the browser and then open new browser and open localhost:8855/ and provide actuator username and password and try actuator endpoint init

Application endpoint http://localhost:8080/

actuator endpoint http://localhost:8855/health

package com.candidjava.springboot;

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

@SpringBootApplication
public class Application {

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

Download

Download source code from my github account Click here