内容简介:Spring Boot中使用Actuator的/info端点输出Git版本信息
对于Spring Boot的Actuator模块相信大家已经不陌生了,尤其对于其中的 /health 、 /metrics 等强大端点已经不陌生(如您还不了解Actuator模块,建议先阅读 《Spring Boot Actuator监控端点小结》 )。但是,其中还有一个比较特殊的端点 /info 经常被大家所忽视,因为从最初的理解,它主要用来输出 application.properties 配置文件中通过info前缀来定义的一些属性,由于乍看之下可能想不到太多应用场景,只是被用来暴露一些应用的基本信息,而基本信息本身也可以在与Spring Cloud结合时作为服务治理的注册信息统一管理,所以这个端点的用处并不是很大。
然而实际上,该端点除了描述应用信息之外,也还可以用来描述Git版本信息,并且整合方法非常简单,下面我们就来看看如何使用 /info 端点暴露当前应用的Git版本信息。
POM配置
首先,我们可以挑选任意一个Spring Boot项目,修改它的 pom.xml :
- 引入
spring-boot-starter-actuator,提供/info端点
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 添加
git-commit-id-plugin插件,该插件用来产生git的版本信息
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.1.15</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
</configuration>
</plugin>
产生git版本信息
- 在完成了上面的配置之后,执行
git-commit-id-plugin插件
运行完成后,我们可以在控台中看到类似下面的信息:
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - dotGitDirectory E:\git_project\oschina\SpringBoot-Learning\.git
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.build.user.name didi
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.build.user.email dyc87112@qq.com
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.branch master
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - --always = true
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - --dirty = -dirty
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - --abbrev = 7
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - Tag refs [ [Ref[refs/tags/chapter1=ec8713f61cd49569886708a08adea02c8ef0a112]] ]
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - Created map: [ {} ]
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - HEAD is [ e0540b3524378de9b5d938668a0f75ec016fa5e5 ]
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - Repo is in dirty state [ true ]
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.commit.id.describe e0540b3-dirty
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.commit.id e0540b3524378de9b5d938668a0f75ec016fa5e5
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.commit.id.abbrev e0540b3
[INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.dirty true
...
同时,在 target/classes 目录下,我们可以发现产生了一个 git.properties 配置信息:
这个文件就是当前项目的git信息,它的内容如下:
#Generated by Git-Commit-Id-Plugin #Thu Jun 01 17:57:53 CST 2017 git.build.user.email=dyc87112@qq.com git.build.host=Lenovo-zhaiyc git.dirty=true git.remote.origin.url=https\://git.oschina.net/didispace/SpringBoot-Learning.git git.closest.tag.name=chapter1 git.commit.id.describe-short=e0540b3-dirty git.commit.user.email=dyc87112@qq.com git.commit.time=2017-06-01T17\:57\:10+0800 git.commit.message.full=update git.build.version=1.0.0 git.commit.message.short=update git.commit.id.abbrev=e0540b3 git.branch=master git.build.user.name=didi git.closest.tag.commit.count=240 git.commit.id.describe=e0540b3-dirty git.commit.id=e0540b3524378de9b5d938668a0f75ec016fa5e5 git.tags= git.build.time=2017-06-01T17\:57\:53+0800 git.commit.user.name=didi
启动测试
完成了上述配置之后,启动应用并访问端点,比如: curl localhost:8080/info ,我们可以获得如下输出:
{
"git": {
"commit": {
"time": 1496311030000,
"id": "e0540b3"
},
"branch": "master"
}
}
其中包含了关于branch和commit的基础信息。而这个信息格式是最简模式,我们也可以通过配置下面的参数来获取更全面的git信息:
management.info.git.mode=full
重启应用后再访问 /info 端点,可以获得类似下面更为详细的版本信息了。
{
"git": {
"build": {
"host": "Lenovo-zhaiyc",
"version": "1.0.0",
"time": 1496311073000,
"user": {
"name": "didi",
"email": "dyc87112@qq.com"
}
},
"branch": "master",
"commit": {
"message": {
"short": "update",
"full": "update"
},
"id": "e0540b3524378de9b5d938668a0f75ec016fa5e5",
"id.describe-short": "e0540b3-dirty",
"id.abbrev": "e0540b3",
"id.describe": "e0540b3-dirty",
"time": 1496311030000,
"user": {
"email": "dyc87112@qq.com",
"name": "didi"
}
},
"closest": {
"tag": {
"name": "chapter1",
"commit": {
"count": "240"
}
}
},
"dirty": "true",
"remote": {
"origin": {
"url": "https://git.oschina.net/didispace/SpringBoot-Learning.git"
}
},
"tags": ""
}
}
代码示例:Chapter6-2-1
以上所述就是小编给大家介绍的《Spring Boot中使用Actuator的/info端点输出Git版本信息》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 网络安全之端点安全如何实现
- Spring Boot Actuator端点详解
- ASP.NET Core端点路由 作用原理
- Prometheus 2.24 发布,为 HTTP 端点添加 TLS 支持
- FreeFileSync 10.14 发布,使用 TLS 访问所有 Web 端点
- Sophos Intercept X EDR:智能、简单的端点侦测与响应
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python带我起飞
李金洪 / 电子工业出版社 / 2018-6 / 79
《Python带我起飞——入门、进阶、商业实战》针对Python 3.5 以上版本,采用“理论+实践”的形式编写,通过大量的实例(共42 个),全面而深入地讲解“Python 基础语法”和“Python 项目应用”两方面内容。书中的实例具有很强的实用性,如对医疗影像数据进行分析、制作爬虫获取股票信息、自动化实例、从一组看似混乱的数据中找出规律、制作人脸识别系统等。 《Python带我起飞——......一起来看看 《Python带我起飞》 这本书的介绍吧!