指标监控
在日常开发中,如果需要监控,查看SpringBoot程序内的URL,有哪些Bean,配置项,健康度,运行指标,日志信息,线程状态...,SpringBoot为我们提供了SpringBootActuator为我们进行监控
1.端点介绍
1.端点分类
1.信息类端点
| 端点名称 | 默认是否开启HTTP | 端点说明 |
|---|---|---|
| auditevents | 否 | 提供系统的审计信息 |
| beans | 否 | 提供系统的Bean列表 |
| caches | 否 | 提供系统的缓存信息 |
| conditions | 否 | 提供配置类的匹配信息以及条件运算结果 |
| configprops | 否 | 提供@ConfigurationProperties的列表 |
| env | 否 | 提供ConfigurableEnvironment中的属性信息 |
| flyway | 否 | 提供已执行的Flyway数据库迁移信息 |
| httptrace | 否 | 提供HTTP跟踪信息,默认最近的100条 |
| info | 是 | 显示事先设置好的系统信息 |
| integrationgraph | 否 | 提供Spring Integration图信息 |
| liquibase | 否 | 提供已执行的Liquibase数据库迁移信息 |
| logfile | 否 | 如果设置了Logging.file.name/logging.file.path属性,则显示日志文件内容 |
| mappings | 否 | 提供@RequestMapping的映射列表 |
| scheduledtasks | 否 | 提供系统中的调度任务列表 |
2.监控类端点
| 端点 | 默认是否开启HTTP | 端点说明 |
|---|---|---|
| health | 是 | 提供系统运行的健康状态 |
| metrices | 否 | 提供系统的度量信息 |
| prometheus | 否 | 提供Prometheus系统可解析的度量信息 |
3.操作类端点
| 端点 | 默认是否开启HTTP | 端点说明 |
|---|---|---|
| heapdump | 否 | 执行Heap Dump操作 |
| loggers | 否 | 查看并修改日志信息 |
| sessions | 否 | 针对使用了SpringSession的系统,可以获取/删除用户的Session |
| shutdown | 否 | 关闭系统 |
| threaddump | 否 | 执行Thread Dump操作 |
4.集成类端点
| 端点 | 默认是否开启HTTP | 端点说明 |
|---|---|---|
| jolokia | 否 | 通过HTTP来发布JMX Bean |
2.使用
1.依赖
<!--指标监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.访问端点
http://localhost:8083/actuator
3.配置监控端点
1.开启/禁用端点
默认情况下,除了shutdown端点以外,其余所有的端点都是处于开启状态,只是访问的状态不同,或者保护状态不同,如果要开启/禁用某个端点,可以如下配置
management:
endpoint:
"端点名称"
enabled: false/true
#例如
management:
endpoint:
shutdown:
enabled: false
禁用所有端点
management:
endpoints:
enabled-by-default: false
禁用所有端点后开启某个端点
#禁用所有端点后开启health端点
management:
endpoints:
enabled-by-default: false
endpoint:
health:
enabled: true
templated:表示有无占位符,可以使用具体的值去代替{}里面的内容
2.通过HTTP访问端点
可以使用如下配置来控制哪些端点是可以通过HTTP的方式发布,哪些端点是不行的
management:
endpoints:
web:
exposure:
#放开所有的端点
include: "*"
management:
endpoints:
enabled-by-default: false
web:
exposure:
#排除所有的端点
exclude: "*"
注:如果同一个端点同时出现在include和exclude中,前者的优先级高于后者
如果需要使用HTTP端点去访问,需要有Web支持,因此需要引入
<!--web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.图形界面使用
1.使用SpringBoot Admin图形界面
1.显示模块
-
创建SpringBoot Admin Server应用
可以在项目中添加模块
引入Spirng-boot-admin-starter-server依赖
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.0</version>
</dependency>
```
在application.yaml中配置端口号
```yaml
server:
port: 8888
```
在主启动类上添加@EnableAdminServer
```java
package com.jiasen.admin;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableAdminServer
@SpringBootApplication
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
```
2.控制模块
-
配置SpringBoot Admin client
添加依赖
<!--springboot-admin--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.4.0</version> </dependency>在application.yaml中配置SpringBoot Admin Server的地址
spring: boot: admin: client: #SpringBoot Admin Server的地址 url: http://localhost:8888配置完成后就可以在http://localhost:8888端口中访问了
这样就不用直接看json数据,可以使用图形界面来使用了