SpringBoot 整合 Actuator 实现健康检查

描述

Spring Boot 包含许多附加功能,可帮助您在将应用程序推送到生产环境时监控和管理应用程序。您可以选择使用 HTTP 端点或 JMX 来管理和监控您的应用程序。审计、健康和指标收集也可以自动应用于您的应用程序。

官方文档 : SpringBootActuator文档

启用

在pom中加入 依赖,那么就默认启用了actuator,进行健康检查的功能。

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

启动项目,如果看一下日志那么就表示配置成功了,接下来我们就来深入的了解 actuator模块

Root WebApplicationContext: initialization completed in 1767 ms
Exposing 13 endpoint(s) beneath base path '/actuator'

端点

Actuator 端点可让您监控应用程序并与之交互。Spring Boot 包含许多内置端点,并允许您添加自己的端点。例如,health端点提供基本的应用程序健康信息。

如下重要端点:

命令描述
beans显示应用程序中所有 Spring bean 的完整列表
caches公开可用的缓存
conditions显示对配置和自动配置类评估的条件以及它们匹配或不匹配的原因。
configprops显示所有 的整理列表@ConfigurationProperties
health显示应用程序运行状况信息。
info显示任意应用程序信息。
loggers显示和修改应用程序中记录器的配置。
mappings显示所有@RequestMapping路径的整理列表。
threaddump执行线程转储。
heapdump返回hprof堆转储文件。需要一个 HotSpot JVM。

启用端点

management:
  endpoint:
    shutdown:
      enabled: true

使用 include 暴露断点 exclude 排除断点。

开发所有web 端点 排除 env,beans。

management:
  endpoints:
    web:
      exposure:
        include: "*"
        exclude: "env,beans"

由于开放了所有端点,导致谁都可以访问,这是很不安全的,所有要加上安全来保护端点。

安全保护

加入 spring security 依赖做安全登录。

	<!-- 增加安全保护 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

yml文件配置:

spring:
  security:
    user:
      name: root
      password: root

登录的用户名和密码都是root,这里大家可以自行设置。

启动项目,再次访问: http://localhost:8080/actuator

在这里插入图片描述

输入配置的 用户名和密码即可访问端点。

CORS 跨域支持

management:
  endpoints:
    web:
      cors:
        allowed-origins: "https://example.com"
        allowed-methods: "GET,POST"

自定义端点

    /**
     * 自定义端点:
     * @return
     */
    @ReadOperation
    public CustomData getData() {
        return new CustomData("test", 5);
    }

自定义健康指标

/**
 * @Author qrn
 * @Title
 * @Date 2021/6/4 11:55
 * @time 11:55
 * 编写自定义健康指标
 *
 * DOWN 503
 * OUT_OF_SERVICE 503
 * UP 默认没有映射,所以 HTTP 状态是 200
 * UNKNOWN 默认没有映射,所以 HTTP 状态是 200
 *
 */
@Component
public class MyHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        int errorCode = check();
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

    private int check() {
        // perform some specific health check
        return 1;
    }

}

自定义服务端口

management:
  server:
    port: 8081

配置指定的SSL

server:
  port: 8443
  ssl:
    enabled: true
    key-store: "classpath:store.jks"
    key-password: secret
management:
  server:
    port: 8080
    ssl:
      enabled: false

以上就是完整的健康检查,项目的代码已经上传到 github上面。

实例代码在github上面需要的请自行拉取:spring-boot-integrate 然后后续会集成更多的模块进去,需要请点个star。后续会集成更多的接口实现,有需要的请保存。

如果这篇文章,有帮助到大家的,请给作者一个一键三连,谢谢

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值