springboot框架四个基础核心三(actuator)

1.引言

springboot框架同样有四个基础核心,分别是:

  • 依赖管理
  • 自动装配
  • 监控Actuator
  • CLI工具

自动装配,与依赖管理只要用过springboot框架,我们都能体会到它的好处;CLI在实际项目中应用较少,不需要很关心;剩下actuator健康检查,我们是不应该忽略它的!

那么actuator到底是个何方神圣呢?这么说吧,在微服务盛行的今天,可观测性是很重要的一个事情,我们需要知道每一个服务的运行状态。

于是,springboot框架给我们提供了actuator模块,通过actuator组件可以非常方便的监控springboot应用状态,提供了全方位的健康检查能力(比如后面我们可以通过actuator实现服务优雅下线)

2.案例

2.1.搭建actuator环境

要搭建actuator监控环境,只需要遵循搭建springboot应用的三板斧

  • 加依赖
  • 加注解
  • 写配置

这么方便,都得益于前面依赖管理,自动装配两个特性,你看这是一套组合拳。而且这个地方只需要两步即可:加依赖、写配置。

  • 加依赖
xml 复制代码
<!--监控健康检查依赖-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 写配置
yaml 复制代码
management:
  endpoints:
    web:
      exposure:
        include: '*'
        exclude: beans,env,metrics
  endpoint:
    health:
      show-details: always

配置说明

  • management.endpoints.web.exposure.include:配置要暴露的端点,*表示暴露所有端点
  • management.endpoints.web.exposure.exclude:配置不要暴露的端点,多个通过逗号隔开
  • management.endpoint.health.show-details:配置是否显示health端点详情

2.2.体验actuator监控

启动应用体验actuator,访问http://ip:port/actuator,查看暴露的端点信息

json 复制代码
// 20210801222532
// http://127.0.0.1:8080/actuator

{
  "_links": {
    "self": {
      "href": "http://127.0.0.1:8080/actuator",
      "templated": false
    },
    "caches-cache": {
      "href": "http://127.0.0.1:8080/actuator/caches/{cache}",
      "templated": true
    },
    "caches": {
      "href": "http://127.0.0.1:8080/actuator/caches",
      "templated": false
    },
    "health": {
      "href": "http://127.0.0.1:8080/actuator/health",
      "templated": false
    },
    "health-path": {
      "href": "http://127.0.0.1:8080/actuator/health/{*path}",
      "templated": true
    },
    "info": {
      "href": "http://127.0.0.1:8080/actuator/info",
      "templated": false
    },
    "conditions": {
      "href": "http://127.0.0.1:8080/actuator/conditions",
      "templated": false
    },
    "configprops": {
      "href": "http://127.0.0.1:8080/actuator/configprops",
      "templated": false
    },
    "loggers": {
      "href": "http://127.0.0.1:8080/actuator/loggers",
      "templated": false
    },
    "loggers-name": {
      "href": "http://127.0.0.1:8080/actuator/loggers/{name}",
      "templated": true
    },
    "heapdump": {
      "href": "http://127.0.0.1:8080/actuator/heapdump",
      "templated": false
    },
    "threaddump": {
      "href": "http://127.0.0.1:8080/actuator/threaddump",
      "templated": false
    },
    "scheduledtasks": {
      "href": "http://127.0.0.1:8080/actuator/scheduledtasks",
      "templated": false
    },
    "mappings": {
      "href": "http://127.0.0.1:8080/actuator/mappings",
      "templated": false
    }
  }
}

访问health端点:http://ip:port/actuator/health

json 复制代码
// 20210801222650
// http://127.0.0.1:8080/actuator/health

{
  "status": "UP",
  "components": {
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 1000203087872,
        "free": 493320859648,
        "threshold": 10485760,
        "exists": true
      }
    },
    "edu": {
      "status": "UP",
      "details": {
        "status": 666
      }
    },
    "ping": {
      "status": "UP"
    }
  }
}

2.3.actuator高级特性

除了基本玩法,actuator还有一些高级的玩法。比如说:

  • 修改暴露端点端口
  • 定义info端点信息
  • 自定义业务监控指标

2.3.1.修改暴露端点端口

yaml 复制代码
management:
  server:
    port: 8888

配置management.server.port后,访问健康检查端点端口需要换成8888,等于是与业务应用独立开了

2.3.2.定义info端点信息

yaml 复制代码
info:
  app: follow-me-springboot-actuator
  author: yanghouhua
  date: 20210731

通过info.xxx的形式,可以给http://ip:port/actuator/info端点增加描述信息,效果

json 复制代码
// 20210801223330
// http://127.0.0.1:8080/actuator/info

{
  "app": "follow-me-springboot-actuator",
  "author": "yanghouhua",
  "date": 20210731
}

2.3.3.自定义业务监控指标

除了springboot内置健康检查的相关端点,我们可以通过扩展HealthIndicator接口,增加业务指标监控,比如说

java 复制代码
/**
 * 自定义健康检查
 *
 * @author ThinkPad
 * @version 1.0
 * @date 2021/7/26 20:43
 */
@Component
public class EduHealthIndicator implements HealthIndicator{

    public Health health() {
       // return  Health.unknown().withDetail("status",666).build();
       // return  Health.unknown().withDetail("status",666).build();
        return  Health.up().withDetail("status",666).build();
    }
}

效果

相关推荐
OpenCSG13 分钟前
对比分析:CSGHub vs. Hugging Face:模型管理平台选型对
人工智能·架构·开源
indexsunny41 分钟前
互联网大厂Java面试实战:从Spring Boot到微服务架构的技术问答解析
java·spring boot·redis·微服务·kafka·jwt·flyway
sheji34161 小时前
【开题答辩全过程】以 基于SpringBoot的疗养院管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
短剑重铸之日1 小时前
《设计模式》第六篇:装饰器模式
java·后端·设计模式·装饰器模式
像少年啦飞驰点、1 小时前
零基础入门 Spring Boot:从‘Hello World’到可上线微服务的完整学习路径
java·spring boot·web开发·编程入门·后端开发
麦兜*1 小时前
深入解析现代分布式事务架构:基于Seata Saga模式与TCC模式实现金融级高可用与数据最终一致性的工程实践全解析
分布式·金融·架构
1104.北光c°2 小时前
【从零开始学Redis | 第一篇】Redis常用数据结构与基础
java·开发语言·spring boot·redis·笔记·spring·nosql
X54先生(人文科技)2 小时前
元创力开源项目介绍
人工智能·架构·零知识证明
码界奇点2 小时前
基于Flask与OpenSSL的自签证书管理系统设计与实现
后端·python·flask·毕业设计·飞书·源代码管理