shusheng007
Published on 2024-03-08 / 21 Visits
0
0

秒懂SpringBoot之如何获取线上服务的build与git信息

[版权申明] 非商业目的注明出处可自由转载 出自:shusheng007

概述

最近团队遇到一个问题,发现通过CI/CD发布的服务里面不包含自己的功能代码,于是两个小哥就开始扯皮了:开发说他的代码没有部署上去,运维说他已经部署了,于是两小哥相爱相杀了一下午。

难道就没有办法获取线上服务的当前版本信息吗?例如服务版本,构建时间,git提交记录等等。答案是肯定的,我们今天就来聊一下这个话题,这在快速迭代部署的微服务环境中环是极其有用的。

解决方案

其中一个解决方案就是通过SpringBoot提供的Actuator结合相关Maven插件来解决。通过Maven插件来生成build以及git的信息,然后通过Actuator的info Endpoint 来暴露出相关信息。

{
  "git": {
    "commit": {
      "user": {
        "name": "shusheng007",
        "email": "shusheng007wb@gmail.com"
      },
      "id": {
        "full": "0cd06517ecced826f361e2291dc96f34807223ac"
      },
...
    },
    "branch": "master",
  },
  "build": {
    "artifact": "composite",
    "name": "composite",
    "time": "2024-03-06T14:25:31.933Z",
    "version": "0.0.1-SNAPSHOT",
    "group": "top.shusheng007"
  }
}

下面我们就来实现以下

启用Actuator

Actuator 通过一系列Endpoint对外暴露应用非常多有用的信息,详情可以阅读相关资料。

那如何在Springboot程序中启用Actuator呢?

  1. 引入actuator依赖

创建一个Springboot程序,在pom.xml文件中引入如下依赖

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
  1. 开放actuator相关的Endpoint

考虑到安全问题,actuator的大部分endpoint默认都是关闭的,不能直接访问,我们需要声明打开。

将如下配置添加到application.yaml文件中

management:
  endpoints:
    web:
      exposure:
        include: info

如果此时访问 info 端点,访问 http://localhost:8001/actuator/info 得到的结果为空,如下所示

{}

说明info没有暴露任何信息,那我们要如何才能将build以及git相关的信息给暴露出来呢?

info节点的信息是由各种InfoContributor来提共的,它是一个接口,其定义如下:

@FunctionalInterface
public interface InfoContributor {
    void contribute(Info.Builder builder);
}

如果它的实现类被赋值的话info节点就有值,反之则没有。

下面是InfoContributor各种实现类:

各个Contributor的职责如下,我们这里重点关注build与git相关的两个实现类

ID

Bean Name

职责

build

BuildInfoContributor

暴露build相关信息,例如版本号

git

GitInfoContributor

暴露git相关信息,例如commit id

java

JavaInfoContributor

暴露Java运行时相关信息,例如JDK版本

env

EnvironmentInfoContributor

暴露环境变量

暴露build相关信息

如果想暴露build相关的信息,我们就需要给BuildInfoContributor 赋值,我们通过如下插件实现。

 <plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
     <executions>
         <execution>
             <goals>
                 <goal>build-info</goal>
             </goals>
         </execution>
     </executions>
 </plugin>

这个老朋友是不是非常熟悉啊?对,就是它spring-boot-maven-plugin!通过配置build-info 这个 goal,这个插件就会帮我们生成一个文件META-INF/build-info.properties。这个文件包含了各种build相关的信息,内容如下

build.artifact=composite
build.group=top.shusheng007
build.name=composite
build.time=2024-03-06T14\:01\:20.673Z
build.version=0.0.1-SNAPSHOT

此时再次访问info端点就有内容了,输出如下

{
  "build": {
    "artifact": "composite",
    "name": "composite",
    "time": "2024-03-06T14:01:20.673Z",
    "version": "0.0.1-SNAPSHOT",
    "group": "top.shusheng007"
  }
}

暴露git相关信息

  1. 配置插件

其原理与build类似,只是需要另一个插件git-commit-id-maven-plugin的帮助生成相关文件,其配置如下

 <plugin>
     <groupId>io.github.git-commit-id</groupId>
     <artifactId>git-commit-id-maven-plugin</artifactId>
     <version>${git-commit-id-maven-plugin.version}</version>
     <executions>
         <execution>
             <id>get-the-git-infos</id>
             <goals>
                 <goal>revision</goal>
             </goals>
             <phase>initialize</phase>
         </execution>
     </executions>
     <configuration>
         <generateGitPropertiesFile>true</generateGitPropertiesFile>
         <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties </generateGitPropertiesFilename>
         <commitIdGenerationMode>full</commitIdGenerationMode>
     </configuration>
 </plugin>

打包后会project.build.outputDirectory目录下生成一个git.properties文件,其内容如下

#Generated by Git-Commit-Id-Plugin
git.branch=master
git.build.host=DESKTOP-xxxxxxxx
git.build.time=2024-03-06T22\:10\:48+0800
git.build.user.email=shusheng007wb@gmail.com
git.build.user.name=shusheng007
git.build.version=0.0.1-SNAPSHOT
git.closest.tag.commit.count=
git.closest.tag.name=
git.commit.author.time=2024-03-06T16\:35\:04+0800
git.commit.committer.time=2024-03-06T16\:35\:04+0800
git.commit.id.abbrev=0cd0651
git.commit.id.describe=0cd0651-dirty
git.commit.id.describe-short=0cd0651-dirty
git.commit.id.full=0cd06517ecced826f361e2291dc96f34807223ac
git.commit.message.full=get git commit and build info
git.commit.message.short=get git commit and build info
git.commit.time=2024-03-06T16\:35\:04+0800
git.commit.user.email=shusheng007wb@gmail.com
git.commit.user.name=shusheng007
git.dirty=true
git.local.branch.ahead=0
git.local.branch.behind=0
git.remote.origin.url=git@github.com\:shusheng007/springboot-learn.git
git.tags=
git.total.commit.count=40
  1. 过滤生成内容

可见其包含了git相关的方方面面,有时我们不需要这么多信息,我们还可以对生成的信息进行过滤。只需要配置<includeOnlyProperties>即可,将需要的信息在此列出来,其支持正则表达式。

  <configuration>
      ...
      <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
      <includeOnlyProperties>
          <includeOnlyProperty>git.branch</includeOnlyProperty>
          <includeOnlyProperty>^git.build.(time|version|host)$</includeOnlyProperty>
          <includeOnlyProperty>^git.commit.user.(name|email)$</includeOnlyProperty>
          <includeOnlyProperty>^git.commit.id.(abbrev|full)$</includeOnlyProperty>
          <includeOnlyProperty>git.commit.message.full</includeOnlyProperty>
      </includeOnlyProperties>
      ...
  </configuration>
  1. 设置展示模式

展示git的信息有两种模式,一种为simple模式,在此模式下只返回简单信息。一种为full模式,在此模式下返回生成的所有内容

management:
  endpoints:
    web:
      exposure:
        include: info
  info:
    git:
      mode: full

此处我们设置为full模式,再次访问info端点,输出如下:

{
  "git": {
    "commit": {
      "user": {
        "name": "shusheng007",
        "email": "shusheng007wb@gmail.com"
      },
      "id": {
        "abbrev": "0cd0651",
        "full": "0cd06517ecced826f361e2291dc96f34807223ac"
      },
      "message": {
        "full": "get git commit and build info"
      }
    },
    "branch": "master",
    "build": {
      "time": "2024-03-06T14:25:34Z",
      "version": "0.0.1-SNAPSHOT",
      "host": "DESKTOP-9MSA03T"
    }
  },
  "build": {
    "artifact": "composite",
    "name": "composite",
    "time": "2024-03-06T14:25:31.933Z",
    "version": "0.0.1-SNAPSHOT",
    "group": "top.shusheng007"
  }
}

至此,我们已经将build和git的信息通过/info暴露出来了,通过这种方式可以清晰的看到当前运行服务的情况了,开发和运维再也不用扯皮了...

总结

以上就是如何通过Actuator获取build与git相关信息的内容了,希望对你有帮助。关注shusheng007,持续输出干货,让我们共同进步,成为朋友

源码

一如既往,你可以从Github找到本文源码


Comment