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支持更丰富的数据结 ...
随机推荐
- 数据结构---栈C语言实现
#include <stdio.h> #include <stdlib.h> #define uchar unsigned char #define uint unsigned ...
- 如何在centos操作系统上发布.net core的项目
环境:操作系统: centos 7.net core: 2.1.101 官方网站的示例地址: https://docs.microsoft.com/zh-cn/dotnet/core/linux-pr ...
- CDH安装系统环境准备——虚拟机网络配置
虚拟机网络配置教程如下: 1.修改网络配置文件[root@master ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0配置IP地址.网关.掩码.DNS ...
- Ubuntu18.04教程
pre.ctl { font-family: "Liberation Mono", monospace } h1 { margin-bottom: 0.21cm } h1.west ...
- Group Anagrams 群组错位词
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- SVN服务器搭建和配置使用详解
SVN服务器搭建和使用(一) Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上 ...
- tomcat jvm优化
tomcat优化(全) (2012-09-26 10:12:59) 转载▼ 标签: 杂谈 分类: java 1.内存设置(VM参数调优)(1). Windows环境下,是tomcat解压版(执行sta ...
- access窗口标签居中
Private Sub Form_Resize() On Error Resume Next Me.Width = Me.InsideWidth Me.Section(acDetail).Height ...
- Angular集成UEditor
1.Ueditor的集成主要通过把UEditor做成一个Component来实现,先上Component代码: import { AfterContentInit, Component, Input, ...
- C++判断字符串是否为空的一个小问题
刚才visual studio下处理一个函数字符串入参,判断入参字符串是否为空有点小问题. 接口函数声明是这样的:SHORT GETWFSINFPINKEYDETAIL(LPCTSTR strKeyN ...