1 使用Actuator检查与监控的步骤
1.1 在 pom 文件中添加 Actuator 的坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
1.2 在全局配置文件中设置关闭安全限制
# 在1.5.x版本中通过management.security.enabled=false来暴露所有端点
management.security.enabled=false
# 在2.x 的springboot中的properties文件格式
management.endpoints.web.exposure.include=*
# 在2.x的springboot版本中的yml格式
# 2.0 yml格式
management:
endpoints:
web:
exposure:
include: refresh
但是spring boot2.0
以后actuator
中security
的报错A global security auto-configuration is now provided
,过时问题,主要是:
- 报错原因: 在
spring boot2.0
以后的版本中,actuator
的security
某些方法已经过期了,已有所改动.不过要是不介意继续用也没事(eclispe
不会提示,但是idea
会提示)
springboot
启动成功后,直接127.0.0.1:8080/+以下请求URI
即可
ID | 描述 | 是否需要鉴权 |
---|---|---|
actuator | 为其他端点提供“发现页面”。 要求 Spring HATEOAS 在 classpath 路径上 |
需要 |
auditevents | 陈列当前应用程序的审计事件信息 | 需要 |
autoconfig | 展示自动配置信息并且显示所有自动配置候选人以及他们“被不被”应用的原因 | 需要 |
beans | 显示应用程序中所有 Spring bean 的完整列表 | 需要 |
configprops | 显示所有配置信息 | 需要 |
dump | dump 所有线程 | 需要 |
env | 陈列所有的环境变量 | 需要 |
flyway | Shows any Flyway database migrations that have been applied | 需要 |
health | 显示应用程序运行状况信息 | 不需要 |
info | 显示应用信息 | 不需要 |
loggers | 显示和修改应用程序中的 loggers 配置 | 需要 |
liquibase | 显示已经应用的任何 Liquibase 数据库迁移 | 需要 |
metrics | 显示当前应用程序的“指标”信息 | 需要 |
mappings | 显示所有@RequestMapping 的 url 整理列表。 | 需要 |
shutdown | 关闭应用(默认情况下不启用) | 需要 |
trace | 显示跟踪信息(默认最后 100 个 HTTP 请求) | 需要 |
2 使用可视化的监控报表 Spring Boot Admin
2.1 搭建服务端
2.1.1 引入admin坐标
服务端其实也是一个 SpringBoot
项目,引入admin的坐标
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>1.5.7</version>
</dependency>
2.1.2 修改启动类,添加@EnableAdminServer
@SpringBootApplication
@EnableAdminServer
public class SpringbootServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootServerApplication.class,args);
}
}
2.2 搭建客户端
其实客户端
就是我们需要监控的工程。
2.2.1 修改客户端的pom文件添加依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<!-- 此处的版本号和springboot版本号一致,或者比springboot版本更低也可以 -->
<version>RELEASE</version>
</dependency>
2.2.2 修改客户端的 application.properteis 配置文件
2.2.2.1 关闭安全限制
# 当springboot是1.5.x时使用这个关闭安全限制如下
management.security.enabled=false
# 当springboot是2.X时,就使用如下
management.endpoints.web.exposure.include=*
# 当springboot是2.X时,使用的yml格式:
management:
endpoints:
web:
exposure:
# 此处的星号是特殊字符,必须用引号引起来
include: “*”
2.2.2.2 注册到springbootadmin的服务
# http://localhost:9090 表示是 Spring Boot Admin 服务单的 IP 地址以及端口号
spring.boot.admin.client.url=http://localhost:9090
2.2.2.3 不让springboot admin通过主机名字 获取ip
# 必须在客户端配置 boot.admin.client.instance.service-url属性,
# 让Spring Boot Admin服务端可以通过网络获取客户端的数据(否则默认会通过主机名去获取)
spring.boot.admin.client.instance.service-url=127.0.0.1:9090
# prefer-ip是否使用注册的ip地址来取代上述各个url中hostname的值,默认值是false
spring.boot.admin.client.instance.prefer-ip=true
2.3 监控信息讲解
报错信息:
Calling [asyncError()] is not valid for a request with Async state [MUST_DISPATCH]
这个是没有什么影响的,说是内置tomcat
的问题,把spring-boot-admin-starter
版本降低下,2.1.2和2.1.3
可能不会让application
页面刷新出来,这时候降低到2.1.0
就可以了