当前位置: 首页 > article >正文

Spring Boot Actuator(官网文档解读)

定义        

        Spring Boot Actuator 是 Spring Boot 提供的一个用于监控和管理应用程序的模块。它能够提供各种生产级别的功能,如健康检查、度量指标收集、配置属性查看等,帮助开发者了解应用的内部状态并进行故障排查。

Actuator 引入

        要启用 Actuator 的功能,只需在项目的依赖中加入 spring-boot-starter-actuator 即可:

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

自定义Actuator的地址和端口

        默认情况下,Actuator 的 HTTP 端口和应用的主 HTTP 端口是一样的。不过,在实际的生产环境中,你可能需要让 Actuator 端点监听不同的端口或者网络地址,以此增强系统的安全性与可管理性。

自定义端口

  • 我们可以通过如下配置进行对actuator模块访问端口的自定义:

server:
  port: 8091

# 开启所有默认Web端点

management:
  server:
    port: 8891
  • 我们的项目的端口是8091,但是访问/actuator 端点的端口是8891

自定义访问地址

  • 我们可以通过如下配置自定义actuator 模块的访问地址:

management:
  server:
    port: 8891
    address: 127.2.2.1
  • 现在我们访问/actuator端点的地址改为:http://127.2.2.1:8891/actuator

自定义敏感信息处理

        在 Spring Boot 应用里,对敏感信息(如密码、密钥等)进行清理(Sanitization)是保障数据安全的重要手段。默认情况下,Spring Boot 会对一些常见的敏感信息进行清理,但在实际项目中,你可能需要根据具体需求对清理逻辑进行自定义。下文将详细介绍如何通过定义 SanitizingFunction Bean 来自定义敏感信息的清理过程。

核心概念

  • SanitizingFunctionSanitizingFunction 是一个函数式接口,用于定义敏感信息的清理逻辑。你可以创建实现该接口的类,并将其注册为 Spring Bean,Spring Boot 会在需要清理敏感信息时调用这些函数。

  • SanitizableDataSanitizableData 类包含了需要清理的数据的相关信息,如键(key)、值(value)以及数据来源的 PropertySource。通过这些信息,你可以实现更精细的清理策略,例如针对特定 PropertySource 中的所有值进行清理。

  • 执行顺序:多个 SanitizingFunction 会按照注册顺序依次执行,直到其中一个函数修改了 SanitizableData 中的值,后续的函数将不再执行。

代码示例

1. 创建自定义的 SanitizingFunction 实现类

import org.springframework.boot.actuate.endpoint.SanitizableData;
import org.springframework.boot.actuate.endpoint.SanitizingFunction;
import org.springframework.core.env.PropertySource;

public class CustomSanitizingFunction implements SanitizingFunction {

    @Override
    public SanitizableData apply(SanitizableData data) {
        String key = data.getKey();
        String value = data.getValue();
        PropertySource<?> propertySource = data.getPropertySource();

        // 示例 1:对包含 "password" 或 "secret" 的键对应的值进行清理
        if (key != null && (key.contains("password") || key.contains("secret"))) {
            return data.withValue("******");
        }

        // 示例 2:对特定 PropertySource 中的所有值进行清理
        if (propertySource != null && "MySpecialPropertySource".equals(propertySource.getName())) {
            return data.withValue("CUSTOM_SANITIZED");
        }

        // 如果不需要清理,返回原始数据
        return data;
    }
}

        在上述代码中,CustomSanitizingFunction 实现了 SanitizingFunction 接口的 apply 方法。在该方法中,首先获取 SanitizableData 中的键、值和 PropertySource。然后根据不同的条件对值进行清理:

  • 若键包含 "password" 或 "secret",将值替换为 "******"。

  • PropertySource 的名称为 "MySpecialPropertySource",将值替换为 "CUSTOM_SANITIZED"。

  • 若不满足上述条件,则返回原始数据。

 2.将自定义的 SanitizingFunction 注册为 Spring Bean

        为了让 Spring Boot 能够使用自定义的 SanitizingFunction,需要将其注册为 Spring Bean。可以通过创建一个配置类来实现:

import org.springframework.boot.actuate.endpoint.SanitizingFunction;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SanitizationConfig {

    @Bean
    public SanitizingFunction customSanitizingFunction() {
        return new CustomSanitizingFunction();
    }
}

        在上述代码中,SanitizationConfig 是一个配置类,使用 @Configuration 注解标记。在该类中,通过 @Bean 注解将 CustomSanitizingFunction 实例注册为 Spring Bean。

3. 测试自定义的清理功能

        在完成上述步骤后,你可以启动 Spring Boot 应用,并访问相关的 Actuator 端点(如 /actuator/env)来验证自定义的清理功能是否生效。

Actuator 集成 JMX

  • Java管理扩展(JMX)提供了监控和管理应用程序的标准机制。默认情况下,Spring Boot 并不会启用此功能。您可以通过将spring.jmx.enabled配置属性设置为true来启用它。SpringBoot将使用SpringJMX注释标记合适的MBeanServer,公开ID为MBeanServer的bean。

spring:
  jmx:
    enabled: true
  • spring.jmx.enabled仅影响 Spring 提供的管理 bean。启用其他库(例如Log4J2或Quartz)提供的管理 bean 是独立的。

Spring Boot-JMX 相关注解

@ManagedResource

        该注解的作用是把一个类标记为 JMX MBean。借助这个注解,能让该类成为可被 JMX 代理管理的资源。

  • 属性:

    • objectName:指定 MBean 的对象名,该名称在 JMX 代理中需保持唯一。

    • description:为 MBean 提供简要描述。

  • 代码示例:

mport org.springframework.jmx.export.annotation.ManagedResource;

@ManagedResource(objectName = "com.example:type=MyManagedResource", description = "This is a managed resource.")
public class MyManagedResource {
    // 类的具体实现
}

        上述代码中,MyManagedResource 类被标记成了 JMX MBean,其对象名是 com.example:type=MyManagedResource,并且有相应的描述信息。

@ManagedAttribute

        该注解用于把类的某个属性(通常是 getter 和 setter 方法)暴露成 JMX MBean 的属性,这样就能在 JMX 代理中对该属性进行读取和修改操作。

  • 属性:

    • description:为属性提供简要描述。

    • currencyTimeLimit:设定属性值的有效时间,单位为秒。

  • 代码示例

import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;

@ManagedResource(objectName = "com.example:type=MyManagedResource")
public class MyManagedResource {
    private String myAttribute;

    @ManagedAttribute(description = "This is a managed attribute.")
    public String getMyAttribute() {
        return myAttribute;
    }

    @ManagedAttribute
    public void setMyAttribute(String myAttribute) {
        this.myAttribute = myAttribute;
    }
}

        在上述代码里,myAttribute 属性借助 @ManagedAttribute 注解被暴露成了 JMX MBean 的属性,可在 JMX 代理中对其进行读取和设置操作。

@ManagedOperation

        这个注解用于把类的某个方法暴露成 JMX MBean 的操作,从而可以在 JMX 代理中调用该方法。

  • 属性:

    • description:为操作提供简要描述。

    • impact:表明操作的影响,可取值为 INFOACTIONACTION_INFO 或 UNKNOWN

  • 代码示例

import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;

@ManagedResource(objectName = "com.example:type=MyManagedResource")
public class MyManagedResource {
    @ManagedOperation(description = "This is a managed operation.", impact = javax.management.MBeanOperationInfo.ACTION)
    public void doSomething() {
        System.out.println("Doing something...");
    }
}

        在上述代码中,doSomething 方法通过 @ManagedOperation 注解被暴露成了 JMX MBean 的操作,能够在 JMX 代理中调用该方法。

自定义MBean 名称

  • MBean 的名称通常由id端点的 生成。如果您的应用程序包含多个 Spring ApplicationContext,您可能会发现名称冲突。为了解决这个问题,您可以将spring.jmx.unique-names属性设置为true,以便 MBean 名称始终唯一。

  • 您还可以自定义端点所暴露的 JMX 域。以下是 application.yml的配置示例:

spring:
  jmx:
    unique-names: true
management:
  endpoints:
    jmx:
      domain: "com.example.myapp"

禁用JMX 相关端点

  • 如果您不想通过 JMX 公开端点,则可以将management.endpoints.jmx.exposure.exclude属性设置为*,如以下示例所示:

management:
  endpoints:
    jmx:
      exposure:
        exclude: "*"

Java-JMX 学习相关

        关于Java-JMX 的详细学习内容,可以参考下列文章:

Java-JMX (官网文档解读)_java jmx-CSDN博客
Java-JMX 组件架构即详解-CSDN博客Java-JMX (官网文档解读)_java jmx-CSDN博客

Spring Boot Actuator 集成 Micrometer

        Spring Boot Actuator 集成 Micrometer(官网文档解读)-CSDN博客

Spring Boot  Actuator EndPoints

        Spring Boot Actuator EndPoints(官网文档解读)-CSDN博客

参考文献

Actuator :: Spring Boot

Monitoring and Management over JMX :: Spring Boot


http://www.kler.cn/a/541309.html

相关文章:

  • C# Winform 使用委托实现C++中回调函数的功能
  • DeepSeek本地部署详细指南
  • 手写一个C++ Android Binder服务及源码分析
  • autMan奥特曼机器人-对接deepseek教程
  • 【JavaScript】this 指向由入门到精通
  • TCP服务器与客户端搭建
  • QT:QWidget
  • 采用分步式无线控制架构实现水池液位自动化管理
  • LLM Note
  • 图论——并查集
  • TCP队头阻塞问题以及QUIC解决方案
  • 本地搭建springboot服务并实现公网远程调试本地接口
  • 【QT】 Qt经典面试题
  • 嵌入式硬件---OpenMV文件系统基本图像处理
  • 《量化绿皮书》Chapter 3 Calculus and Linear Algebra 微积分与线性代数(一)
  • 字节跳动大模型应用 Go 开发框架 —— Eino 实践
  • 结合实际讲NR系列2—— SIB1
  • 解锁 CSS Grid 高级技巧:提升网页布局灵活性的秘诀
  • PyTorch torch.sign函数介绍
  • MySQL 8版本认证问题
  • DeepSeek模型场景应用:基于腾讯云HAI搭建IDEA开发助手
  • Redis双写一致性(数据库与redis数据一致性)
  • Cisco ISE升级
  • ModuleJS 与 CommonJS 混用的两种解决方案
  • Day62_补20250210_图论part6_108冗余连接|109.冗余连接II
  • LINUX嵌入式交叉编译:cJSON