2. SpringBoot监控

SpringBoot自带监控功能Actuator,可以帮助实现堆程序内部运行情况监控,比如监控状况、Bean加载情况、配置属性、日志信息等。
使用步骤非常简单:只有两步
①导入依赖坐标

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>

②访问http://localhost:8080/acruator
实战:
创建SpringBoot工程,引入两个依赖
image.pngimage.png
启动项目并访问:得到json字符串
包含三部分:1::actuator;2:健康状况;3:info

  1. {
  2. "_links":{
  3. "self":{
  4. "href":"http://localhost:8080/actuator",
  5. "templated":false
  6. },
  7. "health":{
  8. "href":"http://localhost:8080/actuator/health",
  9. "templated":false
  10. },
  11. "health-component-instance":{
  12. "href":"http://localhost:8080/actuator/health/{component}/{instance}",
  13. "templated":true
  14. },
  15. "health-component":{
  16. "href":"http://localhost:8080/actuator/health/{component}",
  17. "templated":true
  18. },
  19. "info":{
  20. "href":"http://localhost:8080/actuator/info",
  21. "templated":false
  22. }
  23. }
  24. }

点击http://localhost:8080/actuator/info,返回为空字符串,其获得的是配置文件中以info开头的信息。我们写两个。
image.png
重新启动,并点击:看到了里面显示的信息。这个比较简单,我们先不管。
image.png
看一下健康的信息:http://localhost:8080/actuator/health,只有一个UP,即上线的意思。如果我们添加了actuator的依赖,别人打开这个健康信息网址就能看到(我们上线后),那岂不是不好,因此里面只有一个UP信息。
image.png
如果想看到详细信息,需要配置,默认是never,等上线时,我们再关闭即可。
image.png
此时返回json信息如下:
image.png
details是详细信息:
diskSpace是磁盘空间
status:UP健康
下面的分别是磁盘空间总大小,剩余空闲大小,和阈值。


现在我们没有什么组件,因此只有磁盘信息,这个健康检查还可以检查第三方组件,比如数据库。
我们以redis为例,这里引入redis起步依赖。但是我们不启动redis。

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>

image.png
如果我们启动了reids,则看到都OK了。
image.png

那么以后我们就可以用这个健康检查看我们的项目有没有问题:如果有问题,是哪个组件问题,是磁盘空间不够了,还是redis坏了等等。