2. SpringBoot监控
SpringBoot自带监控功能Actuator,可以帮助实现堆程序内部运行情况监控,比如监控状况、Bean加载情况、配置属性、日志信息等。
使用步骤非常简单:只有两步
①导入依赖坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
②访问http://localhost:8080/acruator
实战:
创建SpringBoot工程,引入两个依赖
启动项目并访问:得到json字符串
包含三部分:1::actuator;2:健康状况;3:info
{
"_links":{
"self":{
"href":"http://localhost:8080/actuator",
"templated":false
},
"health":{
"href":"http://localhost:8080/actuator/health",
"templated":false
},
"health-component-instance":{
"href":"http://localhost:8080/actuator/health/{component}/{instance}",
"templated":true
},
"health-component":{
"href":"http://localhost:8080/actuator/health/{component}",
"templated":true
},
"info":{
"href":"http://localhost:8080/actuator/info",
"templated":false
}
}
}
点击http://localhost:8080/actuator/info,返回为空字符串,其获得的是配置文件中以info开头的信息。我们写两个。
重新启动,并点击:看到了里面显示的信息。这个比较简单,我们先不管。
看一下健康的信息:http://localhost:8080/actuator/health,只有一个UP,即上线的意思。如果我们添加了actuator的依赖,别人打开这个健康信息网址就能看到(我们上线后),那岂不是不好,因此里面只有一个UP信息。
如果想看到详细信息,需要配置,默认是never,等上线时,我们再关闭即可。
此时返回json信息如下:
details是详细信息:
diskSpace是磁盘空间
status:UP健康
下面的分别是磁盘空间总大小,剩余空闲大小,和阈值。
现在我们没有什么组件,因此只有磁盘信息,这个健康检查还可以检查第三方组件,比如数据库。
我们以redis为例,这里引入redis起步依赖。但是我们不启动redis。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
如果我们启动了reids,则看到都OK了。
那么以后我们就可以用这个健康检查看我们的项目有没有问题:如果有问题,是哪个组件问题,是磁盘空间不够了,还是redis坏了等等。