spring-boot- starter- actuator模块的实现对于实施微服务的中小团队来说,可以有效地省去或大大减少监控系统在采集应用指标时的开发量。当然,它也并不是万能的,有时候也需要对其做一些简单的扩展来帮助我们实现自身系统个性化的监控需求。
首先在我们的spring boot项目的meven 的POM文件里面添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
然后启动项目
我们会发现在项目启动日志里面出现一对Mapped,到这里说明我们的actuator配置成功
上图显示了一批端点定义,这些端点并非我们自己在程序中创建的,而是由spring-boot- starter- actuator模块根据应用依赖和配置自动创建出来的监控和管理端点。
然后我们打开postman 或者浏览器访问我们的项目地址然后根据上图的端点进行相关访问
例如:访问actuator/health(我这里的项目地址是http://127.0.0.1:8082)
ps 我这里的项目输出的是这个actuator/health,看百度上一些项目是/health 这是应为: 2.0之前默认是/ 2.0默认是 /actuator 可以通过这个属性值修改
这里就可以看到我们项目的health信息了
我们可以在配置文件中这样详细配置actuator
#actuator端口
management.server.port=9001
#修改访问路径 2.0之前默认是/ 2.0默认是 /actuator 可以通过这个属性值修改
management.endpoints.web.base-path=/monitor
#开放所有页面节点 默认只开启了health、info两个节点
management.endpoints.web.exposure.include=*
#显示健康具体信息 默认不会显示详细信息
management.endpoint.health.show-details=always
当我们修改之后就可以通过我们配置后的地址http://127.0.0.1:9001/monitor/env/进行访问了
在这里就能看到我们项目的环境变量了!
详情可以去https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html官方文档进行查看