入门使用:Actuator插件
Actuator插件是SpringBoot原生提供的一个服务,可以通过暴露端点路由,用来输出应用中的诸多 端点信息。
pom.xml中添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
启动Spring Boot应用程序之后,只要在浏览器中输入端点信息就能获得应用的一些状态信息。
访问路径拼接下面的请求路径:例如:localhost:8080/info
常用端点列举如下,可以一个个详细试一下:
/info 应用基本信息
/health 健康度信息
/metrics 运行指标
/env 环境变量信息
/loggers 日志相关
/dump 线程相关信息
/trace 请求调用轨迹
此时只能使用/health 和 /info端点,其他因为权限问题无法访问。想访问指定端点的话可以在yml配置中添加相关的配置项,比如/metrics端点则需要配置:
endpoints:
metrics:
sensitive: false
此时浏览器访问/metrics端点就能得到诸如下面所示的信息:
{
"mem": 71529,
"mem.free": 15073,
"processors": 4,
"instance.uptime": 6376,
"uptime": 9447,
"systemload.average": -1.0,
"heap.committed": 48024,
"heap.init": 16384,
"heap.used": 32950,
"heap": 506816,
"nonheap.committed": 23840,
"nonheap.init": 160,
"nonheap.used": 23506,
"nonheap": 0,
"threads.peak": 25,
"threads.daemon": 23,
"threads.totalStarted": 28,
"threads": 25,
"classes": 6129,
"classes.loaded": 6129,
"classes.unloaded": 0,
"gc.copy.count": 74,
"gc.copy.time": 173,
"gc.marksweepcompact.count": 3,
"gc.marksweepcompact.time": 88,
"httpsessions.max": -1,
"httpsessions.active": 0
}
当然也可以开启全部端点权限,只需如下配置即可:
endpoints:
sensitive: false
由于Actuator插件提供的监控能力毕竟有限,而且UI比较简陋,因此需要一个更加成熟一点的工具
Spring Boot Admin监控系统
添加Spring Boot Admin监控依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.7</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.7</version>
</dependency>
然后在应用主类上通过加注解来启用Spring Boot Admin,在application类中添加@EnableAdminServer注解
@EnableAdminServer
@SpringBootApplication
public class SpringbtAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbtAdminServerApplication.class, args);
}
}
Application.properties添加端口修改
server.port:8081
启动程序,浏览器打开 localhost:8081 查看Spring Boot Admin主页面:

此时完成了监控软件部署和启动。
下面进行被监控的软件进行配置:
pom.xml中加入以下依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.7</version>
</dependency>
然后在yml配置中添加如下配置,将应用注册到Admin服务端去:
被注册的应用名称叫:AdminTest
spring:
boot:
admin:
url: http://localhost:8081
client:
name: AdminTest
Client应用一启动,Admin服务立马推送来了消息,告诉你AdminTest上线了,上线之后就可以在主页上面看到消息。
注意一点,被监控应用,如果启动异常,或者有错误,那么就会出现注册失败情况。
网友评论