系统的监控
在分布式的设计中显得尤为重要,因为分开部署的缘故,并不能及时的了解到程序运行的实时状况
,之所以重要所以SpringBoot
也给我提供了一套自动监控的API
,可以无缝整合spring-boot-admin
来完成图形化的展示,我们本章先来介绍下actuator
系统监控相关内容。
通过spring-boot-actuator
完成系统运行监控,实时了解程序运行的环境是否健康。
使用idea
开发工具创建SpringBoot
项目并添加spring-boot-starter-actuator
以及spring-boot-starter-web
(如果缺少web
依赖会导致本章项目无法启动(没有内部集成的web容器
无法运行.),pom.xml
文件部分内容如下所示:
.....
<description>
SpringBoot核心技术:使用Actuator监控你的应用程序
博客文章地址:http://blog.yuqiyu.com/spring-boot-actuator.html
</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
.....
有关本章开放节点的配置都被映射到属性配置类
org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties
中,通过@ConfigurationProperties
注解自动映射application.yml
配置文件内以前缀management.endpoints.web
的属性配置信息。
Actuator
默认开放了两个节点信息,分别是:
health
:健康监测节点
健康节点我们在访问时默认
只可以查看当前系统的运行状态
,如下所示:{
"status": "UP"
}
如果不开放相关的配置无法查看详细的运行健康信息
,比如:硬盘等,具体的开放方法在本章查看详细健康状态
info
:基本信息查看节点我们在属性类
WebEndpointProperties
内也并没有看到health
、info
作为初始化的值赋值给exposure.include
,那么是怎么进行赋值的呢?
spring-configuration-metadata.json
(元数据配置文件)位于spring-boot-actuator-autoconfigure-2.0.6.jar
依赖的META-INF
目录下,主要作用是对应WebEndpointProperties
等属性配置类的字段类型
、描述
、默认值
、对应目标字段
的定义,具体的实现我在自定义starter
的文章内有讲到,我们找到name
为management.endpoints.web.exposure.include
的配置如下:
.....
{
"sourceType": "org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties$Exposure",
"defaultValue": [
"health",
"info"
],
"name": "management.endpoints.web.exposure.include",
"description": "Endpoint IDs that should be included or '*' for all.",
"type": "java.util.Set<java.lang.String>"
},
.....
通过配置的defaultValue
来自动映射到WebEndpointProperties
属性配置类的Exposure#include
字段,下面简单介绍上面的字段:
sourceType
:该配置字段所关联配置类
的类型(全限定名)defaultValue
:默认值,根据type
来设置,如上java.util.Set<java.lang.String>
类型的默认值就可以通过["health","info"]
设置name
:字段的名称,对应配置类
内的field
名称description
:该配置字段的描述信息,可以是中文,填写后idea
工具会自动识别并提示type
:该字段的类型的全限定名,如:java.lang.String
开启查看详细健康状态比较简单,通过配置参数management.endpoint.health.show-details
来进行修改,该参数的值由org.springframework.boot.actuate.health.ShowDetails
枚举提供配置值,ShowDetails
源码如下所示:
/**
* Options for showing details in responses from the {@link HealthEndpoint} web
* extensions.
*
* @author Andy Wilkinson
* @since 2.0.0
*/
public enum ShowDetails {
/**
* Never show details in the response.
*/
NEVER,
/**
* Show details in the response when accessed by an authorized user.
*/
WHEN_AUTHORIZED,
/**
* Always show details in the response.
*/
ALWAYS
}
在spring-configuration-metadata.json
元数据文件内,配置的showDetails
的默认值为never
,也就是不显示详细信息,配置如下所示:
.....
{
"sourceType": "org.springframework.boot.actuate.autoconfigure.health.HealthEndpointProperties",
"defaultValue": "never",
"name": "management.endpoint.health.show-details",
"description": "When to show full health details.",
"type": "org.springframework.boot.actuate.health.ShowDetails"
},
.....
我们修改management.endpoint.health.show-details
参数为always
后重新项目再次访问/actuator/health
就可以获取如下的信息:
{
"status": "UP",
"details": {
"diskSpace": {
"status": "UP",
"details": {
"total": 250790436864,
"free": 77088698368,
"threshold": 10485760
}
}
}
}
actuator
默认的所有节点的访问前缀都是/actuator
,在application.yml
配置文件内设置management.endpoints.web.basePath
参数进行修改,如下所示:
# 管理节点配置
management:
endpoints:
web:
# actuator的前缀地址
base-path: /
basePath
字段位于WebEndpointProperties
属性配置类内,修改完成重启项目就可以使用修改后的路径进行访问,我们上述直接映射到了/
下。
通过本章的讲解我们明白了spring-boot-actuator
默认开放了health
、info
两个节点,通过配置健康检查详细信息可以查看硬盘相关的运行健康状态。
本章源码项目名称:hengboy-spring-boot-actuator
):访问码云查看源码、访问GitHub查看源码