Spring Boot中使用Actuator的/info端点输出

作者: 程序猿DD | 来源:发表于2017-06-03 13:08 被阅读743次

    对于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插件

    这个文件就是当前项目的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端点输出

        本文链接:https://www.haomeiwen.com/subject/gzopfxtx.html