Spring Boot中使用Actuator的/info端點(diǎn)輸出Git版本信息
對(duì)于Spring Boot的Actuator模塊相信大家已經(jīng)不陌生了,尤其對(duì)于其中的/health、/metrics等強(qiáng)大端點(diǎn)已經(jīng)不陌生(如您還不了解Actuator模塊,建議先閱讀《Spring Boot Actuator監(jiān)控端點(diǎn)小結(jié)》)。但是,其中還有一個(gè)比較特殊的端點(diǎn)/info經(jīng)常被大家所忽視,因?yàn)閺淖畛醯睦斫猓饕脕?lái)輸出application.properties配置文件中通過(guò)info前綴來(lái)定義的一些屬性,由于乍看之下可能想不到太多應(yīng)用場(chǎng)景,只是被用來(lái)暴露一些應(yīng)用的基本信息,而基本信息本身也可以在與Spring Cloud結(jié)合時(shí)作為服務(wù)治理的注冊(cè)信息統(tǒng)一管理,所以這個(gè)端點(diǎn)的用處并不是很大。
然而實(shí)際上,該端點(diǎn)除了描述應(yīng)用信息之外,也還可以用來(lái)描述Git版本信息,并且整合方法非常簡(jiǎn)單,下面我們就來(lái)看看如何使用/info端點(diǎn)暴露當(dāng)前應(yīng)用的Git版本信息。
POM配置
首先,我們可以挑選任意一個(gè)Spring Boot項(xiàng)目,修改它的pom.xml:
- 引入spring-boot-starter-actuator,提供/info端點(diǎn)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
- 添加git-commit-id-plugin插件,該插件用來(lái)產(chǎn)生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>
產(chǎn)生git版本信息
- 在完成了上面的配置之后,執(zhí)行g(shù)it-commit-id-plugin插件

運(yùn)行完成后,我們可以在控臺(tái)中看到類似下面的信息:
[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
...
同時(shí),在target/classes目錄下,我們可以發(fā)現(xiàn)產(chǎn)生了一個(gè)git.properties配置信息:

這個(gè)文件就是當(dāng)前項(xiàng)目的git信息,它的內(nèi)容如下:
#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
啟動(dòng)測(cè)試
完成了上述配置之后,啟動(dòng)應(yīng)用并訪問(wèn)端點(diǎn),比如:curl localhost:8080/info,我們可以獲得如下輸出:
{
"git": {
"commit": {
"time": 1496311030000,
"id": "e0540b3"
},
"branch": "master"
}
}
其中包含了關(guān)于branch和commit的基礎(chǔ)信息。而這個(gè)信息格式是最簡(jiǎn)模式,我們也可以通過(guò)配置下面的參數(shù)來(lái)獲取更全面的git信息:
management.info.git.mode=full
重啟應(yīng)用后再訪問(wèn)/info端點(diǎn),可以獲得類似下面更為詳細(xì)的版本信息了。
{
"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
Github:https://github.com/dyc87112
碼云:http://git.oschina.net/didispace/SpringBoot-Learning
以上所述是小編給大家介紹的Spring Boot中使用Actuator的/info端點(diǎn)輸出Git版本信息,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
基于Spring Data的AuditorAware審計(jì)功能的示例代碼
這篇文章主要介紹了基于Spring Data的AuditorAware審計(jì)功能的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
JPA @Basic單表查詢?nèi)绾螌?shí)現(xiàn)大字段懶加載
這篇文章主要介紹了JPA @Basic單表查詢?nèi)绾螌?shí)現(xiàn)大字段懶加載的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java類的初始化順序知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于Java類的初始化順序知識(shí)點(diǎn)總結(jié),需要的朋友們可以學(xué)習(xí)下。2020-02-02
SpringBoot 多線程事務(wù)回滾的實(shí)現(xiàn)
本文是基于springboot的@Async注解開(kāi)啟多線程,并通過(guò)自定義注解和AOP實(shí)現(xiàn)的多線程事務(wù),避免繁瑣的手動(dòng)提交/回滾事務(wù),感興趣的可以了解一下2024-02-02
Dubbo服務(wù)無(wú)法注冊(cè)到ZK上問(wèn)題
這篇文章主要介紹了Dubbo服務(wù)無(wú)法注冊(cè)到ZK上問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
hibernate 命名查詢?nèi)绾螌?shí)現(xiàn)
Hibernate允許在映射文件中定義字符串形式的查詢語(yǔ)句,這種查詢方式成為命名查詢,需要的朋友可以參考下2012-11-11

