乐趣区

关于java:Spring-Boot通过Actuator显示git和build的信息

1 简介

为了更好的版本控制和问题定位,咱们须要晓得正在运行的利用是什么版本,什么时候打包的,Git 的相干信息等。通过 /actuator/info 能够帮忙咱们获取这些信息。

2 配置

首先要有 actuator 的依赖:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

而后关上对应的端口:

management:
  endpoints:
    web:
      exposure:
        include: "*"

这时就能够拜访 /actuator/info 了,不过返回是空的。

要返回 git 和 build 的信息,咱们须要减少插件:

<plugins>
  <plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
    <version>4.0.0</version>
    <executions>
      <execution>
        <id>get-the-git-infos</id>
        <goals>
          <goal>revision</goal>
        </goals>
        <phase>initialize</phase>
      </execution>
    </executions>
    <configuration>
      <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
      <generateGitPropertiesFile>true</generateGitPropertiesFile>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot-dependencies.version}</version>
    <executions>
      <execution>
        <goals>
          <goal>build-info</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

这两个插件会为咱们生成两个文件,一个是 build-info.properties,专门放一些 build 的信息;另一个是 git.properties,放一些版本控制的信息:

当咱们再拜访 /actuator/info 时,Spring Boot 就会读取并显示对应的信息了:

3 总结

代码请查看:https://github.com/LarryDpk/p…

退出移动版