SpringBoot 2 Actuator Endpoint
创建Maven 项目:
POM 中加入 actuator 依赖,来获取应用程序的实时运行数据。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
POM全文如下:
<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.study</groupId>
<artifactId>springboot-monitor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath />
</parent>
<properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建启动类Application
package com.actuator.simple; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
默认情况下,只有 如下 3 个 actuator 端口暴露出来:




在application.yml 中,开启 shutdown 端点,并暴露 shutdown 、health 和 info端口:
server: port: 8090 management: endpoint: shutdown: enabled: true endpoints: web: exposure: include: shutdown,health,info

调用 shutdown 端点:

在yaml 中配置暴露所有端点:
server: port: 8090 management: endpoint: shutdown: enabled: true endpoints: web: exposure: include: "*"

在pom.xml 中加入如下security依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
并配置保护Actuator Endpoint,开启HttpBasic, 并在application.yml 中创建role为ACTUATOR 的 User。
package com.actuator.simple; import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class ActuatorSecurity extends WebSecurityConfigurerAdapter{ /* * need to add http.csrf().disable(); to call actuator/shutdown successfully * https://stackoverflow.com/questions/50689032/spring-boot-actuator-shutdown-endpoint-forbidden */ @Override protected void configure(HttpSecurity http) throws Exception { //protect all Actuator endpoints,such as actuator/health, actuator/info http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests().anyRequest().hasRole("ACTUATOR").and().httpBasic().and().csrf().disable(); } }
注意此处我们关闭了 CSRF protection,以使 POST /actuator/shutdown 方法得以执行,否则,调用
POST /actuator/shutdown 会得到403 Forbidden Error。
Cross Site Request Forgery Protection Since SpringBoot relies on Spring security's defaults, CSRF protection is turned by default. This means that the actuator endpoints that require a POST( SHUTDOWN and LOGGERS endpoints) PUT or DELETE will get a 403 forbidden error when the default security configuration is in use. [Note] It's recommended that disabling CSRF protection completely ONLY IF you are creating a service that is used by non-browser clients.
management.endpoint.health.show-details 的值默认是never,即不显示 health 端点的 details 信息。
当将 management.endpoint.health.show-details 设为 when_authorized 或 always 时, 可看到health 端点的 details 信息。
此时,application.yml 如下:
server: port: 8090 management: endpoint: health: # show-details: never(default) / when-authorized / always show-details: when-authorized shutdown: enabled: true endpoints: web: exposure: include: shutdown,health,info spring: security: user: name: xluffy password: 123 roles: ACTUATOR
调用 /actuator/health ,并提供application.yml 中设定的用户名 、密码,此时可看到health 端点的 details 信息

SpringBoot会根据classpath 中的依赖自动配置HealthIndicator: 例,如果在 pom.xml 中加入了如下Redis 依赖,并启动Redis Server,此时的 health 端点信息如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>

此处,有3 个 HealthIndicator,分别是 HealthIndicator 、 DiskSpaceHealthIndicator 、RedisHealthIndicator
对于HealthIndicator 如果如下继承该类,
package com.actuator.simple; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; @Component public class CustomHealth implements HealthIndicator { @Override public Health health() { return Health.outOfService().withDetail("msg", "服务中断...").build(); } }
则 Health 端点的信息如下:

当在application.yml 中加入
info:
author:
name: xluffy
以及配置build-info goal 时,
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
可以看到如下信息:

git-commit-id-plugin 可以将Git 信息暴露到 actuator/info 端口,前提是需要build project(maven clean package)
Add this to plugins section of pom.xml. maven will generate this file during build./target/classes/git.properties.
Spring will read contents of this file and include it in the response of/actuator/info此时的application.yml 是:
server: port: 8090 management: info: git: # mode: full mode: simple endpoint: health: # show-details: never(default) / when-authorized / always show-details: when-authorized shutdown: enabled: true endpoints: web: exposure: include: shutdown,health,info spring: security: user: name: xluffy password: 123 roles: ACTUATOR info: author: name: xluffy
<plugin> <groupId>pl.project13.maven</groupId> <artifactId>git-commit-id-plugin</artifactId> </plugin>

Author:LuffyStory
Origin:http://www.cnblogs.com/luffystory/

浙公网安备 33010602011771号