首页 > Java > java教程 > 正文

Spring Boot Actuator /info 端点应用版本信息配置指南

DDD
发布: 2025-10-01 10:43:15
原创
557人浏览过

spring boot actuator /info 端点应用版本信息配置指南

本教程详细阐述如何在 Spring Boot 3 应用中配置 Actuator 的 /info 端点,以展示应用程序的版本、名称和描述等核心信息。通过正确配置 pom.xml 依赖、application.yml 文件以及可选的 Spring Security 规则,开发者可以轻松地将 Maven 项目元数据动态集成到 /info 响应中,从而实现应用信息的便捷监控。

1. 理解 Spring Boot Actuator 的 /info 端点

Spring Boot Actuator 提供了一系列生产就绪型功能,帮助监控和管理应用程序。其中 /info 端点用于显示应用程序的通用信息,例如版本、名称、描述等。默认情况下,如果未进行特定配置,访问此端点可能只会返回一个空的 JSON 对象 {}。为了让它显示有用的信息,我们需要进行相应的配置。

2. 添加 Actuator 依赖

首先,确保您的 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>
登录后复制

注意事项:

  • 避免同时引入 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 中定义的 <name>、<description> 和 <version> 字段的值自动替换。

4. Spring Security 配置(如果使用)

如果您的应用程序集成了 Spring Security,并且您自定义了 Actuator 的基础路径(如 /public/actuator),那么您需要明确允许对这些公共 Actuator 端点的访问,否则它们可能会被安全拦截。

微信 WeLM
微信 WeLM

WeLM不是一个直接的对话机器人,而是一个补全用户输入信息的生成模型。

微信 WeLM 33
查看详情 微信 WeLM

以下是一个示例 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 端点,使其能够动态展示应用程序的核心元数据。这对于应用程序的监控和管理提供了极大的便利。

以上就是Spring Boot Actuator /info 端点应用版本信息配置指南的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号