厌伴老儒烹瓠叶,强随举子踏槐花。这篇文章主要讲述Spring Boot指标监控与健康检查相关的知识,希望能为你提供帮助。
Spring Boot指标监控与健康检查ActuatorSpring Boot Actuator 可以帮助你监控和管理 Spring Boot 应用,比如健康检查、审计、统计和HTTP追踪等。所有的这些特性可以通过 JMX 或者 HTTP endpoints 来获得。
创建项目
文章图片
文章图片
文章图片
文章图片
创建 Spring Boot 项目,选择 Spring Boot Actuator 组件。
文章图片
POM
<
?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 https://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.2.2.RELEASE<
/version>
<
relativePath/>
<
!-- lookup parent from repository -->
<
/parent>
<
groupId>
com.example<
/groupId>
<
artifactId>
demo-actuator<
/artifactId>
<
version>
0.0.1-SNAPSHOT<
/version>
<
name>
demo-actuator<
/name>
<
description>
Demo project for Spring Boot<
/description>
<
properties>
<
java.version>
1.8<
/java.version>
<
/properties>
<
dependencies>
<
!-- spring boot actuator 依赖 -->
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-actuator<
/artifactId>
<
/dependency>
<
!-- spring boot web 依赖 -->
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-web<
/artifactId>
<
/dependency>
<
!-- spring boot test 依赖 -->
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-test<
/artifactId>
<
scope>
test<
/scope>
<
exclusions>
<
exclusion>
<
groupId>
org.junit.vintage<
/groupId>
<
artifactId>
junit-vintage-engine<
/artifactId>
<
/exclusion>
<
/exclusions>
<
/dependency>
<
/dependencies>
<
build>
<
plugins>
<
plugin>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-maven-plugin<
/artifactId>
<
/plugin>
<
/plugins>
<
/build>
<
/project>
监控设置
下列端点与具体技术无关:
ID | Description |
---|---|
auditevents |
Exposes audit events information for the current application. Requires an AuditEventRepository bean. |
beans |
Displays a complete list of all the Spring beans in your application.< br /> 显示应用程序中所有Spring Bean的完整列表。 |
caches |
Exposes available caches.< br /> 公开可用的缓存。 |
conditions |
Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match. |
configprops |
Displays a collated list of all @ConfigurationProperties .<
br />
显示所有配置信息。 |
env |
Exposes properties from Spring’s ConfigurableEnvironment .<
br />
陈列所有的环境变量。 |
flyway |
Shows any Flyway database migrations that have been applied. Requires one or more Flyway beans. |
health |
Shows application health information.< br /> 显示应用程序的健康信息。 |
httptrace |
Displays HTTP trace information (by default, the last 100 HTTP request-response exchanges). Requires an HttpTraceRepository bean.<
br />
显示HTTP跟踪信息(默认情况下,最后100个HTTP请求-响应交换)。需要一个HttpTraceRepository bean。 |
info |
Displays arbitrary application info.< br /> 显示应用程序信息。 |
integrationgraph |
Shows the Spring Integration graph. Requires a dependency on spring-integration-core . |
loggers |
Shows and modifies the configuration of loggers in the application.< br /> 显示和修改应用程序中的 loggers 配置。 |
liquibase |
Shows any Liquibase database migrations that have been applied. Requires one or more Liquibase beans. |
metrics |
Shows ‘metrics’ information for the current application.< br /> 显示当前应用程序的“指标”信息。 |
mappings |
Displays a collated list of all @RequestMapping paths.<
br />
显示所有 @RequestMapping 的 url 整理列表。 |
scheduledtasks |
Displays the scheduled tasks in your application. |
sessions |
Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Requires a Servlet-based web application using Spring Session. |
shutdown |
Lets the application be gracefully shutdown. Disabled by default.< br /> 关闭应用程序(默认情况下不启用)。 |
threaddump |
Performs a thread dump. |
ID | Description |
---|---|
heapdump |
Returns an hprof heap dump file. |
jolokia |
Exposes JMX beans over HTTP (when Jolokia is on the classpath, not available for WebFlux). Requires a dependency on jolokia-core . |
logfile |
Returns the contents of the logfile (if logging.file.name or logging.file.path properties have been set). Supports the use of the HTTP Range header to retrieve part of the log file’s content. |
prometheus |
Exposes metrics in a format that can be scraped by a Prometheus server. Requires a dependency on micrometer-registry-prometheus . |
# 度量指标监控与健康检查
management:
endpoints:
web:
base-path: /actuator# 访问端点根路径,默认为 /actuator
exposure:
include: \'*\'# 需要开启的端点,值得注意的是即使配置了 \'*\',shutdown 端点也不会开启还需要额外配置
exclude: env,caches# 不需要开启的端点
endpoint:
shutdown:
enabled: true# 开启 shutdown
启动类
package com.example.demoactuator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoActuatorApplication {public static void main(String[] args) {
SpringApplication.run(DemoActuatorApplication.class, args);
}}
启动信息
通过控制台打印信息可以得知开启了多少访问端点。
文章图片
访问
文章图片
POM
<
?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 https://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.2.2.RELEASE<
/version>
<
relativePath/>
<
!-- lookup parent from repository -->
<
/parent>
<
groupId>
com.example<
/groupId>
<
artifactId>
demo-admin-server<
/artifactId>
<
version>
0.0.1-SNAPSHOT<
/version>
<
name>
demo-admin-server<
/name>
<
description>
Demo project for Spring Boot<
/description>
<
properties>
<
java.version>
1.8<
/java.version>
<
spring-boot-admin.version>
2.2.1<
/spring-boot-admin.version>
<
/properties>
<
dependencies>
<
!-- spring boot admin server 依赖 -->
<
dependency>
<
groupId>
de.codecentric<
/groupId>
<
artifactId>
spring-boot-admin-starter-server<
/artifactId>
<
/dependency>
<
!-- spring boot web 依赖 -->
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-web<
/artifactId>
<
/dependency>
<
!-- spring boot test 依赖 -->
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-test<
/artifactId>
<
scope>
test<
/scope>
<
exclusions>
<
exclusion>
<
groupId>
org.junit.vintage<
/groupId>
<
artifactId>
junit-vintage-engine<
/artifactId>
<
/exclusion>
<
/exclusions>
<
/dependency>
<
/dependencies>
<
dependencyManagement>
<
dependencies>
<
dependency>
<
groupId>
de.codecentric<
/groupId>
<
artifactId>
spring-boot-admin-dependencies<
/artifactId>
<
version>
${spring-boot-admin.version}<
/version>
<
type>
pom<
/type>
<
scope>
import<
/scope>
<
/dependency>
<
/dependencies>
<
/dependencyManagement>
<
build>
<
plugins>
<
plugin>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-maven-plugin<
/artifactId>
<
/plugin>
<
/plugins>
<
/build>
<
/project>
配置文件
server:
port: 8769
启动类【Spring Boot指标监控与健康检查】启动类需要添加
@EnableAdminServer
注解。package com.example.demoadminserver;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAdminServer
public class DemoAdminServerApplication {public static void main(String[] args) {
SpringApplication.run(DemoAdminServerApplication.class, args);
}}
搭建客户端
创建项目创建 Spring Boot 项目,选择 Spring Boot Admin Client 组件。
本文中不再创建,使用刚才的 Actuator 项目当作客户端使用。
POM
<
?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 https://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.2.2.RELEASE<
/version>
<
relativePath/>
<
!-- lookup parent from repository -->
<
/parent>
<
groupId>
com.example<
/groupId>
<
artifactId>
demo-actuator<
/artifactId>
<
version>
0.0.1-SNAPSHOT<
/version>
<
name>
demo-actuator<
/name>
<
description>
Demo project for Spring Boot<
/description>
<
properties>
<
java.version>
1.8<
/java.version>
<
spring-boot-admin.version>
2.2.1<
/spring-boot-admin.version>
<
/properties>
<
dependencies>
<
!-- spring boot admin client 依赖 -->
<
dependency>
<
groupId>
de.codecentric<
/groupId>
<
artifactId>
spring-boot-admin-starter-client<
/artifactId>
<
/dependency>
<
!-- spring boot actuator 依赖 -->
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-actuator<
/artifactId>
<
/dependency>
<
!-- spring boot web 依赖 -->
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-web<
/artifactId>
<
/dependency>
<
!-- spring boot test 依赖 -->
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-test<
/artifactId>
<
scope>
test<
/scope>
<
exclusions>
<
exclusion>
<
groupId>
org.junit.vintage<
/groupId>
<
artifactId>
junit-vintage-engine<
/artifactId>
<
/exclusion>
<
/exclusions>
<
/dependency>
<
/dependencies>
<
dependencyManagement>
<
dependencies>
<
dependency>
<
groupId>
de.codecentric<
/groupId>
<
artifactId>
spring-boot-admin-dependencies<
/artifactId>
<
version>
${spring-boot-admin.version}<
/version>
<
type>
pom<
/type>
<
scope>
import<
/scope>
<
/dependency>
<
/dependencies>
<
/dependencyManagement>
<
build>
<
plugins>
<
plugin>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-maven-plugin<
/artifactId>
<
/plugin>
<
/plugins>
<
/build>
<
/project>
配置文件
# 度量指标监控与健康检查
management:
endpoints:
web:
base-path: /actuator# 访问端点根路径,默认为 /actuator
exposure:
include: \'*\'# 需要开启的端点,值得注意的是即使配置了 \'*\',shutdown 端点也不会开启还需要额外配置
exclude: env,caches# 不需要开启的端点
endpoint:
shutdown:
enabled: true# 开启 shutdown# 指定服务端的访问地址
spring:
boot:
admin:
client:
url: http://localhost:8769
博主交流君羊;723749901【暗号67】
推荐阅读
- 容器缩放比例不小于576px
- k8s安全
- [设计模式系列] 建造者
- 源码解读Dubbo分层设计思想
- 华为三面(说说ListMap和Set有什么区别())
- 你的域名是如何变成 IP 地址的()
- Java开发工程师进阶篇-深入浅出Redis
- 面试官(如何从10亿数据中快速判断是否存在某一个元素())
- Tensorflow保存神经网络参数有妙招(Saver和Restore)