JobPlus知识库 IT 大数据 文章
SpringBoot之旅 -- SpringBoot 项目健康检查与监控

You build it,You run it, 当我们编写的项目上线后,为了能第一时间知晓该项目是否出现问题,常常对项目进行健康检查及一些指标进行监控。
Spring Boot-Actuator 就是帮助我们监控我们的Spring Boot 项目的。

使用

Spring Boot 最主要的特性就是AutoConfig(自动配置),而对于我们这些使用者来说也就是各种starter,
Spring Boot-Actuator 也提供了starter,为我们自动配置,在使用上我们只需要添加starter到我们的依赖中,然后启动项目即可。

  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-actuator</artifactId>

  4. </dependency>

常用Endpoint

Spring Boot-actuator,提供了许多有用的EndPoint,对Spring Boot应用提供各种监控,下面说一下我常用的EndPoint:

/health 应用的健康状态
/configprops 获取应用的配置信息,因为Spring Boot 可能发布时是单独的Jar包,配置文件可能包含其中, 当我们需要检查配置文件时可以使用 ConfigpropsEndPoint 进行查看一些配置是否正确。
/trace 最近几次的http请求信息

HealthEndPoint

当我们访问 http://localhost:8088/health 时,可以看到 HealthEndPoint 给我们提供默认的监控结果,包含 磁盘检测和数据库检测。

  1. {

  2. "status": "UP",

  3. "diskSpace": {

  4. "status": "UP",

  5. "total": 398458875904,

  6. "free": 315106918400,

  7. "threshold": 10485760

  8. },

  9. "db": {

  10. "status": "UP",

  11. "database": "MySQL",

  12. "hello": 1

  13. }

  14. }

其实看 Spring Boot-actuator 源码,你会发现 HealthEndPoint 提供的信息不仅限于此,org.springframework.boot.actuate.health 包下 你会发现 ElasticsearchHealthIndicator、RedisHealthIndicator、RabbitHealthIndicator 等
也就是 HealthEndPoint 也提供 ES, Redis 等组件的健康信息。

自定义Indicator 扩展 HealthEndPoint

看源码 其实 磁盘和数据库健康信息就是 DiskSpaceHealthIndicator、DataSourceHealthIndicator 来实现的,当我们对一些我们自定义的组件进行监控时, 我们也可以实现个Indicator :

  1. @Component

  2. public class User implements HealthIndicator {

  3. /**

  4.     * user监控 访问: http://localhost:8088/health

  5.     *

  6.     * @return 自定义Health监控

  7.     */

  8. @Override

  9. public Health health() {

  10. return new Health.Builder().withDetail("usercount", 10) //自定义监控内容

  11. .withDetail("userstatus", "up").up().build();

  12. }

  13. }

这时我们再次访问: http://localhost:8088/health 这时返回的结果如下,包含了我们自定义的 User 健康信息。

  1. {

  2. "status": "UP",

  3. "user": {

  4. "status": "UP",

  5. "usercount": 10,

  6. "userstatus": "up"

  7. },

  8. "diskSpace": {

  9. "status": "UP",

  10. "total": 398458875904,

  11. "free": 315097989120,

  12. "threshold": 10485760

  13. },

  14. "db": {

  15. "status": "UP",

  16. "database": "MySQL",

  17. "hello": 1

  18. }

  19. }

自定义EndPoint

其实除了扩展 HealthEndPoint 来添加一些健康检查, 我们也可以自定定义一些EndPoint 来提供程序运行时一些信息的展示:

  1. @Configuration

  2. public class EndPointAutoConfig {

  3. @Bean

  4. public Endpoint<Map<String, Object>> customEndPoint() {

  5. return new SystemEndPoint();

  6. }

  7. }

  8. @ConfigurationProperties(prefix="endpoints.customsystem")

  9. public class SystemEndPoint extends AbstractEndpoint<Map<String, Object>> {

  10. public SystemEndPoint(){

  11. super("customsystem");

  12. }

  13. @Override

  14. public Map<String, Object> invoke() {

  15. Map<String,Object> result= new HashMap<>();

  16. Map<String, String> map = System.getenv();

  17. result.put("username",map.get("USERNAME"));

  18. result.put("computername",map.get("COMPUTERNAME"));

  19. result.put("userdomain",map.get("USERDOMAIN"));

  20. return result;

  21. }

  22. }

访问 http://localhost:8088/customsystem 来查看我们自定义的EndPoint ,返回结果如下:

  1. {

  2. "username": "xxx",

  3. "userdomain": "DESKTOP-6EAN1H4",

  4. "computername": "DESKTOP-6EAN1H4"

  5. }


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持
319人赞 举报
分享到
用户评价(0)

暂无评价,你也可以发布评价哦:)

扫码APP

扫描使用APP

扫码使用

扫描使用小程序