springboot actuator

springboot提供了actuator模块用于线上健康检测以及状态监控

添加actuator maven依赖

<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>springboot-actuator</artifactId>

<name>springboot-actuator</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</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>
</project>

 添加Demo类并运行

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

访问http://localhost:8080/actuator可以看到actuator通过http暴露了哪些接口,默认actuator http形式只暴露health和info接口,内容如下

{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false}}}

如果需要暴露更多接口,可以配置management.endpoints.web.exposure.include参数,如配置

management.endpoints.web.exposure.include=*

即暴露所有enable的接口,访问http://localhost:8080/actuator,可以看到所有enable的接口为:

{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},"caches-cache":{"href":"http://localhost:8080/actuator/caches/{cache}","templated":true},"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"info":{"href":"http://localhost:8080/actuator/info","templated":false},"conditions":{"href":"http://localhost:8080/actuator/conditions","templated":false},"configprops":{"href":"http://localhost:8080/actuator/configprops","templated":false},"env":{"href":"http://localhost:8080/actuator/env","templated":false},"env-toMatch":{"href":"http://localhost:8080/actuator/env/{toMatch}","templated":true},"loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false},"loggers-name":{"href":"http://localhost:8080/actuator/loggers/{name}","templated":true},"heapdump":{"href":"http://localhost:8080/actuator/heapdump","templated":false},"threaddump":{"href":"http://localhost:8080/actuator/threaddump","templated":false},"metrics":{"href":"http://localhost:8080/actuator/metrics","templated":false},"metrics-requiredMetricName":{"href":"http://localhost:8080/actuator/metrics/{requiredMetricName}","templated":true},"scheduledtasks":{"href":"http://localhost:8080/actuator/scheduledtasks","templated":false},"mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false}}}

默认只有shutdown接口是disable的,如果需要启动,可以配置:

management.endpoint.shutdown.enabled=true

配置后,可以POST请求localhost:8080/actuator/shutdown来关闭引用,如使用curl:

curl -X POST localhost:8080/actuator/shutdown

springboot同时支持使用JMX的方式来查看系统运行状况,因为JMX默认暴露所有接口,所以只要启用JMX配置,即可访问相关MBean:

spring.jmx.enabled=true

 springboot支持的接口列表如下,某些接口还需要引入相应的依赖,如micrometer-registry-prometheus,jolokia-core等:

 详细文档可以访问: https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#production-ready

原文地址:https://www.cnblogs.com/yytxdy/p/12806963.html