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": ""
}
}
Spring Boot中使用Actuator的/info端点输出Git版本信息的更多相关文章
- 如何在 Spring Boot 中禁用 Actuator 端点安全性?
默认情况下,所有敏感的 HTTP 端点都是安全的,只有具有 ACTUATOR 角色的用户才能访问它们.安全性是使用标准的 HttpServletRequest.isUserInRole 方法实施的. ...
- Spring Boot中文文档(官方文档翻译 基于1.5.2.RELEASE)
作者:Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, Andy Wilkinson, Marcel Overdijk, ...
- 必须知道的Spring Boot中的一些Controller注解
这篇文章是抄其他人的,原址:https://cloud.tencent.com/developer/article/1082720 本文旨在向你介绍在Spring Boot中controller中最基 ...
- Spring Boot中使用RSocket
1. 概述 RSocket应用层协议支持 Reactive Streams语义, 例如:用RSocket作为HTTP的一种替代方案.在本教程中, 我们将看到RSocket用在spring boot中, ...
- 在Spring Boot中使用Docker在测试中进行高级功能测试
最近又学到了很多新知识,感谢优锐课老师细致地讲解,这篇博客记录下自己所学所想. 想更多地了解Spring Boot项目中的功能测试吗?这篇文章带你了解有关在测试中使用Docker容器的更多信息. 本文 ...
- 传统的Servlet在spring boot中怎么实现的?
传统的Servlet在spring boot中怎么实现的? 本文主要内容: 1:springboot一些介绍 2:传统的servlete项目在spring boot项目中怎么实现的?web.xml.u ...
- Spring Boot中Tomcat是怎么启动的
Spring Boot一个非常突出的优点就是不需要我们额外再部署Servlet容器,它内置了多种容器的支持.我们可以通过配置来指定我们需要的容器. 本文以我们平时最常使用的容器Tomcat为列来介绍以 ...
- 【Java面试】如何理解Spring Boot中的Starter?
一个工作了3年的Java程序员,遇到一个Spring Boot的问题. 他对这个问题有一些了解,但是回答得不是很好,希望参考我的高手回答. 这个问题是:"如何理解Spring Boot中的S ...
- spring boot(三):Spring Boot中Redis的使用
spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...
随机推荐
- Linux网络设置(第二版) --互联网寻址过程
Linux网络设置 --互联网寻址过程 1.TCP/IP与OSI参考模型比较 TCP/IP OSI 物理层 网卡 数据链路层 * MAC地址 网络层 IP,ICMP,ARP协议 传输层 TCP,UDP ...
- 根据isbn获得图书的所有信息
几点说明 1这个豆瓣的api https://api.douban.com/v2/book/isbn/:9787549208869 可以以json的形式返回书籍的所有信息 2最开始的时候是我自己写的用 ...
- CRF资料
与最大熵模型相似,条件随机场(Conditional random fields,CRFs)是一种机器学习模型,在自然语言处理的许多领域(如词性标注.中文分词.命名实体识别等)都有比较好的应用效果.条 ...
- LeetCode刷题之合并排序链表
合并两个有序链表并返回一个新的列表.新列表应该由连接在一起的节点前两个列表 给定实例:Input: 1->2->4, 1->3->4Output: 1->1->2- ...
- 如何修改和查看tomcat内存大小
为了解决tomcat在大进行大并发请求时,出现内存溢出的问题,请修改tomcat的内存大小,其中分为以下两种方式: 一.使用 catalina.bat 等命令行方式运行的 tomcat 查看系统最大支 ...
- 如何实现Zabbix的主动注册功能
主动注册(Active Agent Auto-Registration),顾名思义,无需在Zabbix Web上手动添加host信息,即可实现主机的监控. 它是由Agent主动向Server注册. 相 ...
- Mina框架(实战详解)
Apache Mina Server 是一个网络通信应用框架,为开发高性能和高可用性的网络应用程序提供了非常便利的框架. 特点:异步的NIO框架,将UDP当成"面向连接"的协议 一 ...
- memcached server install(WSL)
prepare:0) libevent-dev1) libseccomp-dev2) build-essential3) automake install: https://www.liquidweb ...
- 分布式任务调度平台XXL-JOB
<分布式任务调度平台XXL-JOB> 一.简介 1.1 概述 XXL-JOB是一个轻量级分布式任务调度框架,其核心设计目标是开发迅速.学习简单.轻量级.易扩展.现已开放源代码并 ...
- Flex 将默认日期格式转化成通用格式
flex 默认日期格式如:Wed Dec 16 00:00:00 GMT+0800 2015 想要得到的通用格式如:2015-12-16 转换方法如下: var sdate:String = &quo ...