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 来自定义敏感信息的清理过程。
核心概念
-
SanitizingFunction
:SanitizingFunction
是一个函数式接口,用于定义敏感信息的清理逻辑。你可以创建实现该接口的类,并将其注册为 Spring Bean,Spring Boot 会在需要清理敏感信息时调用这些函数。 -
SanitizableData
:SanitizableData
类包含了需要清理的数据的相关信息,如键(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
:表明操作的影响,可取值为INFO
、ACTION
、ACTION_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