
Spring Boot Actuator提供了一系列生产就绪的端点,用于监控和管理应用。其中,/info 端点是一个非常有用的接口,它可以展示应用的通用信息,如版本、名称、描述、构建信息等。默认情况下,如果未正确配置,访问此端点可能只会返回一个空的JSON对象 {}。本教程将指导您如何填充这些信息。
首先,确保您的pom.xml(Maven项目)或build.gradle(Gradle项目)中包含了Spring Boot Actuator的启动器依赖。这是启用Actuator功能的基础。
Maven (pom.xml):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>spring-boot-starter-actuator 包含了所有Actuator的核心功能,是推荐的依赖方式。
正确配置 application.yml 是在 /info 端点展示应用信息的关键。这里我们将配置Actuator端点的暴露方式,以及如何动态获取项目构建信息。
management:
info:
env:
enabled: true # 启用env info,这对于获取构建信息至关重要
endpoints:
web:
exposure:
include: info # 明确暴露info端点
base-path: /public/actuator # (可选) 自定义Actuator的基础路径,这里设置为/public/actuator
info:
application:
name: '@project.name@' # 从Maven/Gradle获取项目名称
description: '@project.description@' # 从Maven/Gradle获取项目描述
version: '@project.version@' # 从Maven/Gradle获取项目版本配置详解:
management.info.env.enabled: true: 这一行非常重要。它允许Actuator在/info端点中包含环境信息,其中就包括由构建工具(如Maven或Gradle)生成的项目元数据。如果此项为false或未设置,即使info.application部分配置了@project.version@等占位符,它们也可能不会被正确解析。
management.endpoints.web.exposure.include: info: Spring Boot 2.x及更高版本默认只暴露health和info端点。但为了明确起见,建议总是显式地包含您希望暴露的端点。
management.endpoints.web.base-path: /public/actuator: 这是一个可选配置,用于自定义Actuator端点的基础路径。如果不设置,默认为/actuator。在示例中,我们将其更改为/public/actuator。
info.application.name: '@project.name@': 这里使用了Maven或Gradle的占位符。在项目构建时,这些占位符会被实际的项目名称、描述和版本替换。这使得您无需手动更新这些信息,它们会与项目的pom.xml或build.gradle保持同步。
如果您的应用集成了Spring Security,并且自定义了Actuator的基础路径(如 /public/actuator),那么您需要明确允许对该路径的访问。否则,您可能会遇到认证错误。
以下是一个示例,展示如何在Spring Security配置中允许对 /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()
);
// ... 其他安全配置 ...
return httpSecurity.build();
}
}关键点:
完成上述配置后,您可以启动Spring Boot应用。
启动成功后,打开浏览器或使用API测试工具(如Postman),访问以下URL:
http://localhost:8080/public/actuator/info
(请根据您的实际端口和自定义的base-path调整URL。如果未自定义base-path,则访问 http://localhost:8080/actuator/info)。
您应该会看到一个JSON响应,其中包含了您在application.yml中配置的应用名称、描述和版本信息,例如:
{
"application": {
"name": "My Spring Sample Application",
"description": "A spring application example",
"version": "1.0.0-SNAPSHOT"
}
}如果仍然只看到 {},请仔细检查以下几点:
通过遵循以上步骤,您可以轻松地在Spring Boot 3应用中配置Actuator的/info端点,以展示动态获取的项目版本、名称和描述等详细信息。这不仅提升了应用的可观察性,也为运维和监控提供了便利。正确利用构建工具的占位符,可以确保这些信息始终与项目的最新状态保持同步,减少手动维护的成本。
以上就是Spring Boot Actuator:在/info端点展示应用版本与详细信息的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号