
Spring Boot Actuator 提供了一系列生产就绪型功能,帮助监控和管理应用程序。其中 /info 端点用于显示应用程序的通用信息,例如版本、名称、描述等。默认情况下,如果未进行特定配置,访问此端点可能只会返回一个空的 JSON 对象 {}。为了让它显示有用的信息,我们需要进行相应的配置。
首先,确保您的 Maven 项目中已正确引入 Spring Boot Actuator 的依赖。对于 Spring Boot 3 及以上版本,推荐使用 spring-boot-starter-actuator,它包含了 Actuator 的核心功能。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>注意事项:
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 获取项目版本配置说明:
如果您的应用程序集成了 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 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 端点,使其能够动态展示应用程序的核心元数据。这对于应用程序的监控和管理提供了极大的便利。
以上就是Spring Boot Actuator /info 端点应用版本信息配置指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号