| 知乎专栏 |
返回 application.properties 文件中定义的 info 配置信息,如:
# info端点信息配置 info.app.name=spring-boot-example info.app.version=v1.0.0
neo@MacBook-Pro ~ % curl -s http://www.netkiller.cn:8080/actuator/info | jq
{
"app": {
"name": "spring-boot-example",
"version": "v1.0.0"
}
}
package cn.aigcsst.config;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class CustomInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
Map<String, Object> customInfo = new HashMap<>();
customInfo.put("currentTime", System.currentTimeMillis());
customInfo.put("serviceStatus", "running");
customInfo.put("database", "MySQL 8.0");
// 2. 将自定义信息添加到info端点
builder.withDetail("custom", customInfo);
// 也可以直接添加单个属性
builder.withDetail("buildTime", "2025-01-01 10:00:00");
}
}
输出信息
neo@Mac ~ % curl -s http://localhost:8080/actuator/info | jq
{
"custom": {
"currentTime": 1766126152379,
"database": "MySQL 8.0",
"serviceStatus": "running"
},
"buildTime": "2025-01-01 10:00:00"
}