Spring Boot Actuator Example

By | 2018-11-11T11:17:18+00:00 August 12th, 2018|Spring Boot|0 Comments

In this tutorials, we are going to see what is Spring Boot Actuator and how it is useful to our production release applications.

Spring Boot Actuator:

Actuator is a sub-project of Spring Boot. It brings production-ready features of our application. It is mainly used to expose operational information about the running application such as :

The health of the application, metrics, loggings, dumps, env and etc. It typically uses HTTP endpoints to enable us to interact with it.

Advantages of Spring Boot Actuator:

  • It allows us to monitor our application
  • To gather the application’s metrics
  • Understand the real traffic
  • We can get the production grade tools via the HTTP end pints, by making use of these we can get the above information and we are also free to configure and extend these features in many ways.

Setting Spring Boot Actuator:

The simplest way to enable the actuator feature in our application is to add the spring-boot-starter-actuator ‘Starter’. This dependency provides all of the Spring Boot’s production-ready features.

To add the actuator to a Maven based project, add the following ‘Starter’ dependency:

Actuator Maven Dependency
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
</dependencies>

Technologies Used:

  • spring-boot-starter-parent-2.0.4 RELEASE
  • spring-boot-starter-actuator
  • Java8
  • IntelliJ IDEA

Spring Boot Actuator 2.x version:

  • If you are familiar with actuator 1.x versions, most of the actuator endpoints were available.
  • Comming to the actuator 2.x versions, Actuator comes with most of the endpoints disabled.
  • Would we want to enable all of them at a time, we should set management.endpoints.web.exposure.include=* property in application.properties file.

You can find the further details about the 2.x version in the official document

Java Config to enable all actuator endpoints
@Bean
public SecurityWebFilterChain securityWebFilterChain(
  ServerHttpSecurity http) {
    return http.authorizeExchange()
      .pathMatchers("/actuator/**").permitAll()
      .anyExchange().authenticated()
      .and().build();
}

Default endpoints:

Actuator Default Endpoints
/auditevents – Exposes audit events information for the current application.
/beans – returns all available beans in your application
/conditions – Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match
/configprops – Displays a collated list of all @ConfigurationProperties beans.
/env – returns the current environment properties.Properties from Spring’s ConfigurableEnvironment.
/flyway – provides details about our Flyway database migrations
/health – shows the health status of our application
/heapdump – returns a GZip compressed hprof heap dump file of yor application.
/info – returns general information about the application. It might be custom data, build information or details about the latest commit
/liquibase – Shows any Liquibase database migrations that have been applied.
/logfile – Returns the contents of the logfile (if logging.file or logging.path properties have been set).
/loggers – enables us to query and modify the logging level of our application
/metrics – details metrics of our current application. This might include generic metrics as well as custom ones
/prometheus – exposes metrics in a format that can be scraped by a Prometheus server.
/scheduledtasks – provides details about every scheduled task within our application
/sessions – Allows retrieval and deletion of user sessions from a Spring Session-backed session store. It may not available when using Spring Session’s support for reactive web applications
/shutdown – performs a graceful shutdown of the application
/threaddump – dumps the thread information of the underlying JVM

Spring Boot Actuator Example:

Spring Boot Actuator Example
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>

  <groupId>com.onlinetutorialspoint</groupId>
  <artifactId>SpringBoot-Actuator-Example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringBoot-Actuator-Example</name>
  <description>Demo project for Spring Boot</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </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.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>
Confugure server port, actuator port and enabling all default actuator endpoints.
application.properties
server.port: 9000 
management.server.port: 9001 
management.server.address: 127.0.0.1 
management.endpoints.web.exposure.include=*
Note: Our actuator is running under 9001 port.
HelloController.java
package com.onlinetutorialspoint;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @GetMapping("hello")
    @ResponseBody
    public String hello(@RequestParam(name="name",required = false,defaultValue = "user") String name){
        return "hello "+name+" :)";
    }
}
SpringBootActuatorApplication.java
package com.onlinetutorialspoint;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication
@PropertySource("classpath:application.properties")
public class SpringBootActuatorApplication {

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

Run Application:

Console
E:\work\SpringBoot-Actuator-Example>mvn spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SpringBoot-Actuator-Example 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.0.4.RELEASE:run (default-cli) > test-compile @ SpringBoot-Actuator-Example >>>
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ SpringBoot-Actuator-Example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ SpringBoot-Actuator-Example ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to E:\work\SpringBoot-Actuator-Example\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ SpringBoot-Actuator-Example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\work\SpringBoot-Actuator-Example\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ SpringBoot-Actuator-Example ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.0.4.RELEASE:run (default-cli) < test-compile @ SpringBoot-Actuator-Example <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.4.RELEASE:run (default-cli) @ SpringBoot-Actuator-Example ---

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.4.RELEASE)

2018-08-12 08:42:59.223  INFO 6280 --- [           main] c.o.SpringBootActuatorApplication        : Starting SpringBootActuatorApplication on DESKTOP-RN4SMHT with PID 6280 (E:\work\S
pringBoot-Actuator-Example\target\classes started by Lenovo in E:\work\SpringBoot-Actuator-Example)
2018-08-12 08:42:59.239  INFO 6280 --- [           main] c.o.SpringBootActuatorApplication        : No active profile set, falling back to default profiles: default
2018-08-12 08:42:59.444  INFO 6280 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWeb
ServerApplicationContext@4f1d523f: startup date [Sun Aug 12 08:42:59 IST 2018]; root of context hierarchy
2018-08-12 08:43:04.229  INFO 6280 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9000 (http)
2018-08-12 08:43:04.338  INFO 6280 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-08-12 08:43:04.338  INFO 6280 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.32
2018-08-12 08:43:04.354  INFO 6280 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in pro
duction environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_161\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java
\javapath;C:\oraclexe\app\oracle\product\11.2.0\server\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\
MySQL\MySQL Server 5.5\bin;C:\php;C:\Apache24;C:\Apache24\bin;C:\Program Files\Java\jdk1.8.0_161\bin;D:\Softwares\apache-maven-3.5.2\bin;C:\Program Files\Git\cmd;C:\Program Files\Git
\mingw64\bin;C:\Program Files\Git\usr\bin;D:\Softwares\apache-ant-1.10.2\bin;C:\ProgramData\chocolatey\bin;;C:\WINDOWS\System32\OpenSSH\;C:\Users\Lenovo\AppData\Local\Programs\Python
\Python36\Scripts\;C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\;C:\Users\Lenovo\AppData\Local\Microsoft\WindowsApps;C:\Users\Lenovo\AppData\Local\atom\bin;C:\Users\Lenovo\
AppData\Local\Microsoft\WindowsApps;;C:\Program Files\Microsoft VS Code\bin;C:\Program Files\Docker Toolbox;.]
2018-08-12 08:43:04.620  INFO 6280 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-08-12 08:43:04.635  INFO 6280 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5222 ms
.........
.........

Output:

Accessing Spring Boot Actuator endpoints:

http://localhost:9001/actuator/metrics

Spring Boot Actuator Metrics

http://localhost:9001/actuator/health

Spring Boot Actuator Health

http://localhost:9001/actuator/heapdump

Spring Boot Actuator heapdump

Happy Learning 🙂

Download Example

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment