
1. 理解 Spring Boot Actuator 的 /info 端点
Spring Boot Actuator 提供了一系列生产就绪型功能,帮助监控和管理应用程序。其中 /info 端点用于显示应用程序的通用信息,例如版本、名称、描述等。默认情况下,如果未进行特定配置,访问此端点可能只会返回一个空的 JSON 对象 {}。为了让它显示有用的信息,我们需要进行相应的配置。
2. 添加 Actuator 依赖
首先,确保您的 Maven 项目中已正确引入 Spring Boot Actuator 的依赖。对于 Spring Boot 3 及以上版本,推荐使用 spring-boot-starter-actuator,它包含了 Actuator 的核心功能。
org.springframework.boot spring-boot-starter-actuator
注意事项:
- 避免同时引入 spring-boot-actuator 和 spring-boot-starter-actuator,后者是前者的一个启动器,包含了所有必要依赖。
- 确保您的 pom.xml 中配置了正确的 Spring Boot 父项目,以便管理依赖版本。
3. 配置 application.yml 文件
application.yml 是配置 Actuator /info 端点显示内容的关键。我们需要启用 info 端点,并指定要显示的应用元数据。通过使用 Maven 属性占位符,我们可以动态地从 pom.xml 中获取项目信息。
management:
info:
env:
enabled: true # 启用环境信息,通常包含构建和Git信息(如果配置)
endpoints:
web:
exposure:
include: info # 明确暴露 'info' 端点
base-path: /public/actuator # (可选) 自定义 Actuator 端点的基础路径
info:
application:
name: '@project.name@' # 从 pom.xml 获取项目名称
description: '@project.description@' # 从 pom.xml 获取项目描述
version: '@project.version@' # 从 pom.xml 获取项目版本配置说明:
- management.info.env.enabled: true:这个配置允许 Actuator 收集并显示一些环境相关的信息,例如构建信息、Git 信息等。
- management.endpoints.web.exposure.include: info:Spring Boot 默认情况下不暴露所有 Actuator 端点。此配置明确指定要暴露 info 端点。
- management.endpoints.web.base-path: /public/actuator:这是一个可选配置,用于更改 Actuator 端点的默认基础路径 /actuator。如果您选择更改,请确保后续访问路径与此一致。
- info.application.*:这是自定义信息部分。@project.name@、@project.description@ 和 @project.version@ 是 Maven 过滤属性的占位符。在 Maven 构建过程中,这些占位符会被 pom.xml 中定义的
、 和 字段的值自动替换。
4. Spring Security 配置(如果使用)
如果您的应用程序集成了 Spring Security,并且您自定义了 Actuator 的基础路径(如 /public/actuator),那么您需要明确允许对这些公共 Actuator 端点的访问,否则它们可能会被安全拦截。
以下是一个示例 SecurityFilterChain 配置,允许对 /public/** 路径的 GET 请求进行未认证访问:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity
// ... 其他安全配置
.authorizeHttpRequests(customizer -> customizer
// 允许对 /public/** 路径的 GET 请求进行未认证访问
.requestMatchers(HttpMethod.GET, "/public/**").permitAll()
// ... 其他授权规则
.anyRequest().authenticated() // 其他所有请求需要认证
);
// ... 其他安全配置,例如 CSRF 禁用等
return httpSecurity.build();
}
}注意事项:
- 如果您没有使用 Spring Security,或者您的 Actuator 端点仍使用默认路径 /actuator 且未被其他安全机制保护,则可以跳过此步骤。
- 请根据您的实际安全策略调整 requestMatchers 规则。
5. 测试应用程序
完成上述配置后,重新启动您的 Spring Boot 应用程序。然后,在浏览器或使用 API 工具(如 Postman)访问 Actuator 的 /info 端点。
如果您的 base-path 配置为 /public/actuator 且应用程序运行在 http://localhost:8080,则访问 URL 为: http://localhost:8080/public/actuator/info
您应该会看到一个 JSON 响应,其中包含 pom.xml 中定义的应用程序名称、描述和版本信息,例如:
{
"application": {
"name": "My Spring Sample Application",
"description": "A sample Spring Boot application",
"version": "1.0.0-SNAPSHOT"
},
"env": {
// ... 其他环境信息,如构建时间、Git commit ID等
}
}通过以上步骤,您已经成功配置了 Spring Boot Actuator 的 /info 端点,使其能够动态展示应用程序的核心元数据。这对于应用程序的监控和管理提供了极大的便利。










