@TOC

SpringBoot officially provides the Spring-boot-starter -actuator for system monitoring and management. It operates using HTTP, JMX, and SSH protocols and automatically obtains audit, health, and indicator information

Environment Preparation:

  • JDK 1.8
  • SpringBoot2.2.1
  • Maven 3.2 +
  • The development tools
    • IntelliJ IDEA
    • smartGit

To create a SpringBoot Initialize project, please refer to my previous blog: SpringBoot Series of Quick Project creation tutorials

To add actuators to maven-based projects, check to add the following “Starter” dependencies:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>
Copy the code

After the project is successfully started, if the context-path is not set, the project automatically adds /actuator as its prefix. Most endpoints are started by default, but only health and INFO endpoints can be accessed through web browsers

You can modify the default prefix through configuration

management.endpoints.web.base-path=/actuator
Copy the code

Common endpoints (HTTP, Jms, SSH can be accessed) :

ID describe Enabled by default
auditevents Expose audit event information for the current application. is
beans Displays a complete list of all Spring beans in the application. is
caches Expose available caches. is
conditions Displays the conditions evaluated on the configuration and auto-configuration classes and why they match or don’t match. is
configprops Show all@ConfigurationPropertiesProofread the list. is
env To expose the SpringConfigurableEnvironmentProperty in. is
flyway Displays Flyway database migrations that have been applied. is
health Displays application health information is
httptrace Displays HTTP trace information (by default, the last 100 HTTP request/response exchanges). is
info Displays application information. is
integrationgraph Displays the Spring Integration diagram. is
loggers Displays and modifies the logger configuration in the application. is
liquibase Displays the Liquibase database migration that has been applied. is
metrics Displays metrics for the current application. is
mappings Show all@RequestMappingCollated list of paths. is
scheduledtasks Displays scheduled tasks in the application. is
sessions Allows user sessions to be retrieved and deleted from the Session store supported by Spring Session. Not available when using Spring Session’s responsive Web application support. is
shutdown Close the application normally. POST request mode no
threaddump Perform thread dump. is

GET calls the Health endpoint, returning JSON information

ID describe Enabled by default
heapdump Returns ahprofHeap dump files. is
jolokia Expose JMX beans over HTTP (not for WebFlux when Jolokia is on classpath). is
logfile Returns the contents of the log file, if setlogging.filelogging.pathProperties). Support using HTTPRangeHeader to retrieve the contents of a partial log file. is
prometheus Expose metrics in a format that can be captured by the Prometheus server. is

Enable endpoint, modify configuration, syntax management.endpoint.[endpoint name]. Enabled =true

management.endpoint.shutdown.enabled=true
Copy the code

The following table shows the built-in endpoints and default exposures, compared to JMX and WEB(Http) :

ID JMX Web
auditevents is no
beans is no
caches is no
conditions is no
configprops is no
env is no
flyway is no
health is is
heapdump N/A no
httptrace is no
info is is
integrationgraph is no
jolokia N/A no
logfile N/A no
loggers is no
liquibase is no
metrics is no
mappings is no
prometheus N/A no
scheduledtasks is no
sessions is no
shutdown is no
threaddump is no

To change the exposed endpoints, use the following specific include and exclude attributes:

attribute The default
management.endpoints.jmx.exposure.exclude
management.endpoints.jmx.exposure.include *
management.endpoints.web.exposure.exclude
management.endpoints.web.exposure.include info, health

The include attribute lists the ids of the exposed endpoints. The exclude attribute lists the ids of endpoints that should not be exposed. The exclude attribute takes precedence over the include attribute.

Example: Disable JMX access to all endpoints except health and INFO

management.endpoints.jmx.exposure.include=health,info
Copy the code

Enable Web access to all endpoints except env

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env
Copy the code

Pay attention to

* has special meaning in YAML, so if you want to include (or exclude) all endpoints, be sure to add quotes, as follows:

management:
  endpoints:
    web:
      exposure:
        include: "*"
Copy the code

Custom InfoContributor

package com.example.springboot.actuator.actuate.health;

import java.util.Collections;

import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;

@Component
public class ExampleInfoContributor implements InfoContributor {

    @Override
    public void contribute(Info.Builder builder) {
        builder.withDetail("example",
                Collections.singletonMap("key"."value")); }}Copy the code

This can be called from a browser or postman:

Cross-domain configuration is supported

management.endpoints.web.cors.allowed-origins=http://localhost
management.endpoints.web.cors.allowed-methods=GET,POST
Copy the code

Fixed endpoint:

management.endpoint.info.enabled=true
management.endpoint.info.cache.time-to-live=10s
Copy the code

Ok, the knowledge of the actuator is more, please refer to the official document for details. This blog refers to the official document and makes a brief record, which is only a reference manual for getting started

Code download