Introduction of ACTUATOR

  1. The ACTUATOR is a function module provided by Springboot for the introspect and monitoring of application systems. With the help of the ACTUATOR, developers can easily view and count some monitoring indicators of application systems
  2. IO /spring-boot…

2. Build the ACTUATOR environment

  1. Create a new SpringBoot project and add maven dependencies
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Copy the code
  1. Start project, access address is
    • Visit all: http://ip:port/actuator
    • Access to a single: http://ip:port/actuator/endpoint
    • The access addresses of the supported endpoints (monitoring points) are then returned as JSON
{
	"_links": {
		"self": {
			"href": "http://localhost:8426/actuator"."templated": false
		},
		"health": {
			"href": "http://localhost:8426/actuator/health"."templated": false
		},
		"health-path": {
			"href": "http://localhost:8426/actuator/health/{*path}"."templated": true
		},
		"info": {
			"href": "http://localhost:8426/actuator/info"."templated": false}}}Copy the code
  1. By default, only the health and INFO endpoints are enabled for external access. If all endpoints are enabled for external access, perform the following operations
management:
  endpoint:
    health:
      If you do not enable the health node, only the status information will be displayed
      show-details: ALWAYS
    shutdown:
      Open the post close # : http://ip:port/actuator/shutdown
      enabled: true
  endpoints:
    web:
      exposure:
        # specifies to expose all endpoints. The default is INFO,health
        #include: info,health
        * Expose all endpoints. Single quotes can also be used
        include: "*"
Copy the code
  1. Reaccess displays all endpoints
{
	"_links": {
		"self": {
			"href": "http://localhost:8426/actuator"."templated": false
		},
		"beans": {
			"href": "http://localhost:8426/actuator/beans"."templated": false
		},
		"caches-cache": {
			"href": "http://localhost:8426/actuator/caches/{cache}"."templated": true
		},
		"caches": {
			"href": "http://localhost:8426/actuator/caches"."templated": false
		},
		"health": {
			"href": "http://localhost:8426/actuator/health"."templated": false
		},
		"health-path": {
			"href": "http://localhost:8426/actuator/health/{*path}"."templated": true
		},
		"info": {
			"href": "http://localhost:8426/actuator/info"."templated": false
		},
		"conditions": {
			"href": "http://localhost:8426/actuator/conditions"."templated": false
		},
		"shutdown": {
			"href": "http://localhost:8426/actuator/shutdown"."templated": false
		},
		"configprops": {
			"href": "http://localhost:8426/actuator/configprops"."templated": false
		},
		"env": {
			"href": "http://localhost:8426/actuator/env"."templated": false
		},
		"env-toMatch": {
			"href": "http://localhost:8426/actuator/env/{toMatch}"."templated": true
		},
		"loggers": {
			"href": "http://localhost:8426/actuator/loggers"."templated": false
		},
		"loggers-name": {
			"href": "http://localhost:8426/actuator/loggers/{name}"."templated": true
		},
		"heapdump": {
			"href": "http://localhost:8426/actuator/heapdump"."templated": false
		},
		"threaddump": {
			"href": "http://localhost:8426/actuator/threaddump"."templated": false
		},
		"metrics": {
			"href": "http://localhost:8426/actuator/metrics"."templated": false
		},
		"metrics-requiredMetricName": {
			"href": "http://localhost:8426/actuator/metrics/{requiredMetricName}"."templated": true
		},
		"scheduledtasks": {
			"href": "http://localhost:8426/actuator/scheduledtasks"."templated": false
		},
		"mappings": {
			"href": "http://localhost:8426/actuator/mappings"."templated": false}}}Copy the code
  1. Endpoint information
type API port describe
GET /beans Application context beans and relationships between them
GET /caches Cache information
GET /health Health indicators for the application
GET /info Get information about the application
GET /conditions Details of bean state conditions
POST /shutdown Stopping the service Node
GET /configprops Describes how configuration properties inject beans
GET /env All environment attribute information
GET /metrics Application metrics information
GET /mappings URL Mapping between a path and a controller
GET /autoconfig Automatic configuration reports which conditions are passed and which conditions are not passed
  1. For example, access health information
    • http://localhost:8426/actuator/health
{
    "status": "UP"."components": {
        "diskSpace": {
            "status": "UP"."details": {
                "total": 153276153856."free": 107921981440."threshold": 10485760}},"ping": {
            "status": "UP"}}}Copy the code

Iii. Expand health monitoring endpoints

  1. The default endpoint only has the above endpoint information. If you want to extend the endpoint information, you can customize or extend the existing endpoint information
  2. You need to inherit the AbstractHealthIndicator indicator and add **@Component** to the container
  3. If the name of the inherited class is XxxHealthIndicator, the name of the injected monitor that ends with HealthIndicator will be XXX, with a lowercase first letter; If not, add the endpoint with the first letter of the class name in lowercase
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;

@Component
public class ExtendHealthIndicator extends AbstractHealthIndicator {
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        builder.up().withDetail("website"."www.codecoord.com");
        // builder.down().withDetail("website", "www.codecoord.com");}}Copy the code
  1. Restart, and then access the health endpoint, at which point you can see the new information
    • The class name does not end with HealthIndicator, so the first letter of the class name is lowercase to add the monitoring endpoint, in this case XxxHealthIndicator end
{
	"status": "UP"."components": {
		"diskSpace": {
			"status": "UP"."details": {
				"total": 214750457856."free": 213597241344."threshold": 10485760."exists": true}},"extend": {
			"status": "UP"."details": {
				"website": "www.codecoord.com"}},"ping": {
			"status": "UP"}}}Copy the code
  1. More monitoring indicators

Custom monitoring endpoint

  1. In addition to extending the endpoint, you can also customize the port
  2. The custom Endpoint class needs to be annotated with ** @endpoint (id = “name”) to identify the Endpoint name, and the @Component annotation is also required to add the class to Spring
  3. You need to add ** @readOperation ** to the monitored method, which can be called through the GET method
    • http://localhost:9999/actuator/advanced
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Endpoint(id = "advanced")
public class ExtendsEndPoint {
    @ReadOperation
    public List<Map<String, Object>> advancedHealth() {
        List<Map<String, Object>> detailList = new ArrayList<>();
        Map<String, Object> detailMap = new HashMap<>(10);
        detailMap.put("name"."advanced");
        detailMap.put("desc"."Custom Endpoint Information");
        detailMap.put("Impl - class".@endpoint (id = \" XXX \"));
        detailMap.put("Impl - Method annotations".@endpoint (id = \" XXX \"));
        detailMap.put("Impl - Method return value type".Method return value: @readOperation);
        detailMap.put("Impl - reference"."com.codecoord.springboot.practice.actuator.ExtendsEndPoint");
        detailList.add(detailMap);
        returndetailList; }}Copy the code
  1. Accessing a new endpoint
    • http://localhost:8426/actuator/advanced
[{"Impl - Method annotations": @endpoint (id = \" XXX \")."name": "advanced"."Impl - reference": "com.codecoord.springboot.practice.actuator.ExtendsEndPoint"."Impl - Method return value type": Method return value: @readOperation."desc": "Custom Endpoint Information"."Impl - class": @endpoint (id = \" XXX \")}]Copy the code