接着上一章:https://blog.csdn.net/romulusla01/article/details/93469258
本章项目源码:https://download.csdn.net/download/romulusla01/11256054
参考:https://www.jianshu.com/p/d5943e303a1f
Spring Boot Actuator可以帮助你监控和管理Spring Boot应用,比如健康检查、审计、统计和HTTP追踪等。所有的这些特性可以通过JMX或者HTTP endpoints来获得。
Actuator同时还可以与外部应用监控系统整合,比如 Prometheus, Graphite, DataDog, Influx, Wavefront, New Relic等。这些系统提供了非常好的仪表盘、图标、分析和告警等功能,使得你可以通过统一的接口轻松的监控和管理你的应用。
Actuator使用Micrometer来整合上面提到的外部应用监控系统。这使得只要通过非常小的配置就可以集成任何应用监控系统。
1、修改pom.xml
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL3IjN0UDNwUTM0IjNwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2、修改配置文件application.properties
3、项目启动
访问http://localhost:8085/getUsersByName?name=rome,发现程序正常运行
访问http://localhost:8086/actuator,查看所有actuator
访问http://localhost:8086/actuator/httptrace,跟踪请求
访问http://localhost:8086/actuator/health,这里status为UP,就是健康的
这里就成功了。
4、常用的actuator endpoints列表
5、展示详细的health,增加一个配置文件application.yaml并输入如下内容
management:
endpoint:
health:
show-details: always
启动后访问:http://localhost:8086/actuator/health,界面变成了
6、创建自定义的健康指标,新建一个package,创建RomeHealthIndicator类
7、启动项目,访问http://localhost:8086/actuator/health
已经把自定义内容展示了。
8、访问http://localhost:8086/actuator/metrics,追踪所有的度量
想要获得每个度量的详细信息,你需要传递度量的名称到URL中,像
http://localhost:8086/actuator/metrics/{MetricName}
如访问:http://localhost:8086/actuator/metrics/system.cpu.count
9、http://localhost:8086/actuator/loggers/{name}
10、http://localhost:8086/actuator/info
info endpoint展示了应用的基本信息。它通过META-INF/build-info.properties来获得编译信息,通过git.properties来获得Git信息。它同时可以展示任何其他信息,只要这个环境property中含有info key。
修改application.yaml
再次访问:http://localhost:8086/actuator/info
11、使用springSecurity保证Actuator的安全,修改pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
新建package
新建RomeSecurityConfig
修改application.yaml
访问:http://localhost:8086/actuator,会跳转到login页面
输入账号密码后,进入http://localhost:8086/actuator
HttpSecurity方法介绍