Provide Best Programming Tutorials

Spring Boot Actuator

The characteristics of microservices determine that the deployment of functional modules is distributed. Most of the functional modules are running on different machines and interact with each other through service calls. The front and back office traffic flows through many microservices, there is an abnormality how to quickly locate which link has a problem? In this framework, the monitoring of microservices is particularly important.

This article is mainly used in conjunction with the Spring Boot Actuator to share with you the common usage of the micro-service Spring Boot Actuator so that we can monitor and manage our micro-services on a daily basis.

Actuator Monitoring

Actuator is an integrated feature of Spring Boot’s introspection and monitoring of applications that allows you to view application configuration details such as automated configuration information, created Spring beans, and some environmental properties.

Actuator Interface

Actuator monitoring is divided into two categories:

  • native endpoints
  • user-defined endpoint scustom endpoints are mainly extensibility. Users can define some indicators of concern based on their actual application and monitor them at runtime.

Native endpoints provide a number of Web interfaces in your application that let you know the internals of your application’s runtime. Native endpoints can be divided into three categories:

  • Application configuration class: You can view the static information of the application at runtime: for example, automatic configuration information, loaded springbean information, yml file configuration information, environment information, request mapping information;
  • Metric class: mainly dynamic information of the runtime, such as stack, request connection, some health indicators, metrics information, etc.
  • Operation control class: mainly refers to shutdown, the user can send a request to close the monitoring function of the application.

Actuator provides 13 interfaces as below picture shows you:

HTTP Method path Describe
GET /auditevents Display audit events exposed by the application (such as authentication entry, order failure)
GET /beans Describe all the beans in the application context and their relationships
GET /conditions Is 1.0 /autoconfig , which provides a condition for automatic configuration to take effect, recording which automatic configuration conditions passed and which failed.
GET /configprops Describe how configuration properties (including default values) are injected into the bean
GET /env get all environment attributes
GET /env/{name} get specific environment attribute value based on the given name
GET /flyway Provide a Flyway database migration information
GET /liquidbase Display detailed information about Liquibase database migration
GET /health Report the application’s health metrics, which are provided by the HealthIndicator implementation class
GET /heapdump Dump an application’s JVM heap information
GET /httptrace Show HTTP footprint, the last 100 HTTP requests/repsponse
GET /info Get custom information for the application, which is provided by the property at the beginning of info
GET /logfile Returns the contents of the log file (if logging.file or logging.path is set)
GET /loggers Display and modify configured loggers
GET /metrics Report various application metrics such as memory usage and HTTP request counts
GET /metrics/{name} Report application metrics with the specified name
GET /scheduledtasks Show timed task information in the app
GET /sessions If we use Spring Session to display HTTP sessions information in the app
POST /shutdown Close the application and ask for endpoints.shutdown.enabled to be set to true
GET /mappings Describe all URI paths and their mapping to controllers (including Actuator endpoints)
GET /threaddump Get a snapshot of thread activity

Demo Project

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.andrewprogramming</groupId>
    <artifactId>acutatortest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>acutatortest</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

application.properties

endpoints.default.web.enabled=true
management.server.port=8081
management.endpoints.web.base-path=/manage
management.endpoints.web.exposure.include=*

Controller Class

package com.andrewprogramming.acutatortest.controller;

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

@RestController
public class HelloAcutator {

    @RequestMapping("/sayHi")
    public void sayHi() {
        System.out.printf("say hi!");
    }

    @RequestMapping("/sayNo")
    public void sayNo() {
        System.out.printf("say no!");
    }
}

Explanation

Management.endpoints.web.base-path=/monitor means that a separate url address is enabled to monitor the Spring Boot application. For security reasons, a separate port is enabled to access the backend monitoring information.

Management.endpoint.shutdown.enabled=true Enable interface shutdown Spring Boot

In Spring Boot 2.x, Actuator only opened two endpoints /actuator/health and /actuator/info for security reasons. You can set the open in the configuration file. Can open all monitoring points

management.endpoints.web.exposure.include=*

You can also choose to open the section

management.endpoints.web.exposure.exclude=beans,trace

Actuator defaults to all monitoring point paths in /actuator/*, although customization is also required if needed.

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

After setting the reboot, the access address will become /manage/* Actuator monitors almost every aspect of the application, and we focus on some of the commands that are often used in projects.

Health

Health is mainly used to check the running status of the application, which is the monitoring point we use the highest frequency. This interface is usually used to remind us of the running state of the application instance, and why the application is not “healthy”, such as database connections, insufficient disk space, and so on.

By default, the status of health is open. After adding dependencies, start the project. You can see the status of the application by visiting: http://localhost:8081/manage/health

{
    "status" : "UP"
}

Health Check the health of your app by combining several health indicators. The Spring Boot Actuator has several predefined health indicators such as DataSourceHealthIndicator, DiskSpaceHealthIndicator, MongoHealthIndicator, RedisHealthIndicator, etc., which use these health indicators as part of a health check.

For example, if your app uses Redis, the RedisHealthindicator will be considered part of the check; if MongoDB is used, the MongoHealthIndicator will be considered part of the check.

Specific health check metrics can be turned off in the configuration file, such as turning off redis health checks:

management.health.redis.enabled=false

By default, all of these health indicators are considered part of the health check.

info

Info is our own configuration information that starts with info in the configuration file. For example, our configuration in the sample project is:

info.app.name=spring-boot-actuator
info.app.version= 1.0.0
info.app.test= test

Start the sample project, visit: http://localhost:8080/manage/info to return some information as follows:

{
  "app": {
    "name": "spring-boot-actuator",
    "version": "1.0.0",
    "test":"test"
  }
}

 

Source Code

download address:https://github.com/AndrewProgramming/springbootTutorialCode/tree/master/acutatortest

 

Leave a Reply

Close Menu