Actuator In Spring Boot

Learn Actuator using Spring Boot

In this example we will use same project that we created earlier.To create project you can follow below link.

REST API Example Using Spring Boot


Or you can download above project from given link.

Spring Boot REST Example


Any application uses others application internally.Actuator provides features that can be used to monitor them.It provides many REST endpoint by which features can be used. By actuator we can perform thread dumps,environment variable,bean configuration etc.

To make actuator enable for spring boot application, we need to include following dependency.

 
	   <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
	   </dependency>

To test actuator we are using following class which contains a GET API

 
package com.example.preparationforinterview.FirstSpringBootPreparation;

import javax.websocket.server.PathParam;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestService {	
	
	@RequestMapping(value = "/api", method = RequestMethod.GET)
	public String getMessage(@PathParam("name") String name){
		return "Hello" + " " + name;		
	}
}

If we have RestService on separate package then we need to include @Import({ RestService.class }) in spring boot class , in our example


 
@SpringBootApplication
@Import({ RestService.class })
public class FirstSpringBootPreparationApplication {

After adding dependency Right click on FirstSpringBootPreparationApplication and run.It will start spring boot application.

Now go to below url

Actuator Health Rest EndPoint


It will display as below



If you go to below url

Actuator Beans Rest EndPoint


It will display There was an unexpected error type=Unauthorized, status=401



Explain :

Most endpoints are sensitive.It means they are not fully public.While some are for example /health and /info.

Most of the details exposed by endpoint is sensitive so only authorized user can access it, so we need to include spring security on class path

Add following dependency

	<dependency>
	  <groupId>org.springframework.boot</groupId>
	 <artifactId>spring-boot-starter-security</artifactId>
	</dependency>

Due to above dependency,security will enable for all feature.To make it for basic we need to include following entries in application.properties

 
security.user.name=preparationforinterview
security.user.password=preparationforinterview
management.security.enabled=true
security.basic.enabled=true

After adding above properties

If you go to below url again.It will ask username and password,given whatever you have set.

Actuator Beans Rest EndPoint


It will load all bean configuration.

Similarly we can visit.

Actuator Beans Rest EndPoint

Actuator Health Rest EndPoint

Actuator Metrics Rest EndPoint

Actuator Dump Rest EndPoint

We can customize endpoint as well.Each endpoint has three properties.
  • id: id of the endpoint
  • enabled: false means it can not be accessed
  • sensitive: false means no authorize is required

For example

endpoints.beans.id=springbeans endpoints.beans.sensitive=false endpoints.shutdown.enabled=true

Download source code

Learn Actuator using Spring Boot



Visit Others Links

Spring Boot Introduction
Spring Boot Setup
REST API Example Using Spring Boot
Spring Boot REST API Test with Mock MVC Using standaloneSetup
Spring Boot REST API Test with Mock MVC Using webAppContextSetup
Spring Boot REST API Test with Embedded Server
Actuator In Spring Boot
Deployment Of Spring Boot In External Tomcat
Spring Boot with OAuth
Top Spring Boot Interview Question