Actuator info 端点
一句话定位:Spring Boot Actuator 的
/actuator/info端点用于暴露应用的静态描述信息,包括版本号、构建属性、自定义业务属性等,是运维团队快速识别应用身份和运行上下文的标准入口。
定义与作用
在分布式部署环境中,运维人员经常需要确认:当前运行的 JAR 是哪个版本?Git 提交号是什么?构建时间是什么时候?这些元信息对于问题回滚、版本对齐和审计追踪至关重要。
传统做法是在代码中硬编码一个 VersionController 返回版本字符串,或在配置文件里手动维护版本号。这些方式的问题是:版本号与构建过程脱节,容易忘记更新,且无法自动携带 Git 信息。
Spring Boot Actuator 的 info 端点通过以下三种信息源自动构建响应:
| 信息源 | 来源 | 说明 |
|---|---|---|
build-info.properties | Maven/Gradle 构建插件生成 | 包含版本号、构建时间、ArtifactId |
info.* 配置属性 | application.yml 或外部配置 | 自定义业务属性(如负责人、环境名) |
git.properties | Git 信息插件生成 | 包含分支名、最新提交 ID、提交时间 |
设计意图:
info端点只暴露静态信息(构建时确定),不暴露动态运行时状态(那是health和metrics的职责)。三者组合使用,可完整描述"我是谁(info)"、"我是否健康(health)"、"我运行得怎么样(metrics)"。
适用位置与常用属性
info 端点无需代码注解,完全由构建配置和 application.yml 驱动。但端点本身需要显式暴露。
构建插件配置(pom.xml)
<build>
<plugins>
<!-- 生成 build-info.properties -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 可选:生成 git.properties -->
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>4.9.10</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
application.yml 配置
# application.yml
server:
port: 8080
management:
server:
port: 8081
endpoints:
web:
exposure:
include: "health,info"
info:
env:
enabled: true # 允许暴露 info.* 环境属性
info:
app:
name: 学生成绩管理系统
version: 1.0.0
company: 广州飞翔科技
owner: 白歌
developer: 小崔
contact:
support: support@feixiang.com
oncall: 400-800-1234
| 属性 | 说明 | 典型值 |
|---|---|---|
management.info.env.enabled | 是否允许 info.* 属性通过端点暴露 | true |
management.info.build.enabled | 是否暴露 build-info | true |
management.info.git.enabled | 是否暴露 git-info | true |
info.* | 自定义业务属性前缀 | info.app.name=xxx |
注意:Spring Boot 2.6+ 出于安全考虑,默认
management.info.env.enabled=false。若不显式开启,自定义info.*属性不会出现在端点响应中。
核心原理
info 端点信息聚合流程
流程解读:
- 构建阶段,
spring-boot-maven-plugin的build-info目标生成META-INF/build-info.properties,记录版本、时间等。 - 可选的
git-commit-id-plugin生成git.properties,记录分支和提交信息。 - 这两个文件被 Maven 打包进
BOOT-INF/classes,随应用一同发布。 - 运行期访问
/actuator/info时,InfoEndpoint遍历所有InfoContributor实现类。 - 每个贡献者从自己的信息源读取数据,最终合并为一个 JSON 对象返回。
信息源层次结构
完整示例
场景说明
飞翔科技在测试环境部署了多个版本的微服务。架构师白歌要求小崔确保每个 JAR 运行时都能通过 info 端点回答三个问题:这是什么系统?版本是多少?谁在负责?小崔需要配置 info 端点,并验证构建信息自动注入。
操作前:未配置 info 信息
# 操作前:application.yml
server:
port: 8080
management:
server:
port: 8081
endpoints:
web:
exposure:
include: "health,info"
此时访问 info 端点:
$ curl http://localhost:8081/actuator/info
返回结果:
{}
或仅包含空的 {},因为没有启用 env 信息源,也没有生成 build-info.properties。运维团队无法从端点获取任何有用的身份标识。
使用该特性的完整代码
步骤一:在 pom.xml 中配置 build-info 插件:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.18</version>
<executions>
<execution>
<goals>
<goal>build-info</goal>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
步骤二:在 application.yml 中开启 info 信息源并配置自定义属性:
spring:
application:
name: student-app
server:
port: 8080
management:
server:
port: 8081
endpoints:
web:
exposure:
include: "health,info"
info:
env:
enabled: true
build:
enabled: true
info:
app:
name: 学生成绩管理系统
description: 广州飞翔科技内部学生成绩查询与管理平台
version: 1.0.0
company: 广州飞翔科技
owner: 白歌
developer: 小崔
system:
env: 测试环境
region: 华南区
db: student_db
步骤三:执行 Maven 打包,确保 build-info 生成:
$ mvn clean package
操作后运行结果及分析
访问 info 端点:
$ curl http://localhost:8081/actuator/info
返回结果:
{
"build": {
"artifact": "student-app",
"name": "student-app",
"time": "2024-05-20T10:30:00.000+0800",
"version": "1.0.0",
"group": "com.feixiang"
},
"app": {
"name": "学生成绩管理系统",
"description": "广州飞翔科技内部学生成绩查询与管理平台",
"version": "1.0.0",
"company": "广州飞翔科技",
"owner": "白歌",
"developer": "小崔"
},
"system": {
"env": "测试环境",
"region": "华南区",
"db": "student_db"
}
}
分析:
build信息来自build-info.properties:由spring-boot-maven-plugin的build-info目标自动生成,包含artifact、version、group等构建坐标。版本号与 POM 保持一致,无需手动维护。app和system信息来自info.*配置属性:由EnvironmentInfoContributor读取application.yml中的info前缀属性并暴露。这些属性适合描述业务维度的元信息。management.info.env.enabled=true是关键开关:Spring Boot 2.6+ 默认关闭环境信息源,若不显式开启,自定义info.*属性不会出现在响应中。- 信息分层清晰:构建信息(build)客观不可变,自定义信息(app/system)灵活可配置,两者互补。
易错场景与面试考点
易错场景一:忘记开启 management.info.env.enabled
# 错误示范:Spring Boot 2.6+ 默认不暴露 info.* 环境属性
management:
endpoints:
web:
exposure:
include: "info"
info:
app:
name: 学生成绩管理系统
后果:访问 /actuator/info 只返回 build 信息(如果生成了),而 app 信息完全缺失。小崔排查半天,以为配置写错了,最后发现是安全开关默认关闭。
正确做法:
management:
info:
env:
enabled: true
易错场景二:未执行 build-info 目标导致 build 信息缺失
<!-- 错误示范:pom.xml 中缺少 build-info 目标 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
后果:META-INF/build-info.properties 没有被生成,端点响应中 build 字段为空。运维无法通过 API 获取版本号,需手动解压 JAR 查看 POM。
正确做法:确保 build-info 和 repackage 目标同时配置在 executions 中。使用 spring-boot-starter-parent 时,默认已包含 build-info。
面试考点
Q:info 端点和 health 端点有什么区别?
info暴露静态元信息(版本、构建时间、自定义属性),回答"我是谁";health暴露动态健康状态(数据库连通、磁盘空间),回答"我是否正常"。两者互补,不应互相替代。
Q:为什么 Spring Boot 2.6+ 默认关闭 management.info.env.enabled?
出于安全考虑。
info.*属性可能包含敏感业务信息(如内部联系人、数据库名),默认关闭可防止无意泄露。开发者需显式开启,意味着已审查过信息安全性。
Q:build-info.properties 是如何生成的?
由
spring-boot-maven-plugin的build-info目标在构建阶段生成,写入META-INF/build-info.properties。内容包括build.group、build.artifact、build.version、build.time等。BuildInfoContributor在运行时读取该文件并注入info响应。
Q:如何确保 info 端点中的版本号与 POM 完全一致?
使用
${project.version}让 Maven 统一管理,但build-info插件会自动从 POM 读取版本号,无需在application.yml中重复写死版本字符串。如果业务需要单独维护应用版本,可在info.app.version中引用 Spring Boot 的build.version。