# 16.3.1 向 /info 端点提供信息

正如您在第 16.2.1 节中所看到的，`/info` 端点开始时是空的，没有信息。但是，您可以通过创建以 `info.` 为前缀的属性，轻松地向里边添加数据。

为属性添加 `info.` 前缀，可以方便的将自定义数据添加到 `/info` 端点，但这并不是唯一的方法。Spring Boot 提供了一个名为 InfoContributor 的接口，允许您以代码方式，将任何信息添加到 `/info` 端点的响应数据中。Spring Boot 甚至已经提供了一些实现类，您会发现它们非常有用。

让我们看看如何编写自己的 InfoContributor，并向 `/info` 端点添加一些自定义信息。

**创建自定义 InfoContributor 实现类**

假设您想在 `/info` 端点中，添加一些有于 Taco Cloud 的简单统计信息。例如，有多少玉米卷已经制作出来了。为此，您可以编写一个实现 InfoContributor 接口的实现类，并注入 TacoRepository。然后就可以把 TacoRepository 能够提供的统计数据，提供给 `/info` 端点使用。以下的程序清单展示了如何实现这样的类。

{% code title="程序清单 16.3 InfoContributor 的自定义实现类" %}

```java
package tacos.tacos;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.actuate.info.Info.Builder;
@Component
public class TacoCountInfoContributor implements InfoContributor {
  private TacoRepository tacoRepo;
  public TacoCountInfoContributor(TacoRepository tacoRepo) {
    this.tacoRepo = tacoRepo;
  }
  @Override
  public void contribute(Builder builder) {
    long tacoCount = tacoRepo.count();
    Map<String, Object> tacoMap = new HashMap<String, Object>();
    tacoMap.put("count", tacoCount);
    builder.withDetail("taco-stats", tacoMap);
  }
}
```

{% endcode %}

实现 InfoContributor 接口时，TacoCountInfoContributor 需要实现 `contribute()`方法。此方法传入了一个 builder 对象，在该对象上调用 `withDetail()` 以添加详细信息。在上述实现中，通过调用 TacoRepository 的 `count()` 方法，查找已经制作了多少玉米卷。然后把这个数字放到一个 Map 对象中，最后将标签 `taco-stats` 提供给 Builder 对象。`/info` 端点的结果将包括该计数，如下所示：

```
{
  "taco-stats": {
    "count": 44
  }
}
```

如您所见，InfoContributor 的实现类可以提供动态统计信息。这与简单地为属性加 `info.` 前缀形成对比，虽然简单，但仅限于静态值。

**将构建信息注入 /INFO 端点**

Spring Boot 附带了几个 InfoContributor 的内置实现，可以自动将信息添加到 `/info` 端点的结果数据中。其中包括 BuildInfoContributor，它将项目构建信息添加到 `/info` 端点。这包括项目版本、时间戳等基本信息，以及执行构建的主机和用户。

要使生成信息包含在 `/info` 端点的结果数据中，需要添加 build-info 到 Spring Boot Maven 插件执行目标中，如下所示：

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

如果使用 Gradle 构建项目，只需将以下行添加到您的 build.gradle 文件：

```
springBoot {
  buildInfo()
}
```

在这两种情况下，构建都将在 JAR 或 WAR 文件中生成名为 build-info.properties 的文件，BuildInfoContributor 会把这些信息附加到 `/info` 端点返回数据中。以下代码片段显示了 `/info` 端点中的构建信息：

```
{
  "build": {
    "version": "0.0.16-SNAPSHOT",
    "artifact": "ingredient-service",
    "name": "ingredient-service",
    "group": "sia5",
    "time": "2018-06-04T00:24:04.373Z"
  }
}
```

此信息有助于准确了解，正在运行的应用程序的版本、构建时间。通过对 `/info` 端点执行 GET 请求，您将知道您是否正在运行项目的最新版本。

**公开 GIT 提交信息**

假设您的项目使用 Git 进行源代码版本控制，您可能希望在 `/info` 端点中包括 Git 提交信息。要做到这一点，您需要在 Maven 项目 pom.xml 中添加以下插件：

```markup
<build>
  <plugins>
  ...
    <plugin>
      <groupId>pl.project13.maven</groupId>
      <artifactId>git-commit-id-plugin</artifactId>
    </plugin>
  </plugins>
</build>
```

如果您是 Gradle 用户，别担心，可以在 build.gradle 文件中添加一个同样功能的插件：

```
plugins {
  id "com.gorylenko.gradle-git-properties" version "1.4.17"
}
```

基本上这两个插件做的是相同的事情：它们生成一个构建产物 git.properties，包含项目的所有 git 元数据。专门的 InfoContributor 实现类在运行时扫描该文件，并将其内容作为 `/info` 端点返回数据的一部分。

最简单的形式，`/info` 端点中显示的 Git 信息包括，构建应用程序所依据的 Git 分支、提交哈希和时间戳：

```
{
  "git": {
    "commit": {
      "time": "2018-06-02T18:10:58Z",
      "id": "b5c104d"
    },
    "branch": "master"
  },
...
}
```

此信息非常明确的描述项目启动时的代码状态。还可以将 management.info.git.mode 属性设置为 full

```
management:
  info:
    git:
      mode: full
```

您可以获得项目构建时，有关 Git 提交的详细信息。下面的清单显示了完整 Git 的示例信息。

{% code title="清单 16.4 通过 /info 端点公开的完整 Git 提交信息" %}

```
{
  "git": {
    "build": {
      "host": "DarkSide.local",
      "version": "0.0.16-SNAPSHOT",
      "time": "2018-06-02T18:11:23Z",
      "user": {
        "name": "Craig Walls",
        "email": "craig@habuma.com"
      }
    },
    "branch": "master",
    "commit": {
      "message": {
        "short": "Add Spring Boot Admin and Actuator",
        "full": "Add Spring Boot Admin and Actuator"
      },
      "id": {
        "describe": "b5c104d-dirty",
        "abbrev": "b5c104d",
        "describe-short": "b5c104d-dirty",
        "full": "b5c104d1fcbe6c2b84965ea08a330595100fd44e"
      },
      "time": "2018-06-02T18:10:58Z",
      "user": {
        "email": "craig@habuma.com",
        "name": "Craig Walls"
      }
    },
    "closest": {
      "tag": {
        "name": "",
        "commit": {
          "count": ""
        }
      }
    },
    "dirty": "true",
    "remote": {
      "origin": {
        "url": "Unknown"
      }
    },
    "tags": ""
  }
  },
...
}
```

{% endcode %}

除了时间戳和 Git 提交哈希之外，完整版本还包括，提交代码的用户姓名和电子邮件，以及提交消息和其他信息。允许您精确地识别构建项目时使用的代码。事实上，请注意清单 16.4 中的 dirty 字段为 true，指出构建目录中有一些未提交的更改，这样的代码状态，真是没有什么情况比这更槽糕了！


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://potoyang.gitbook.io/spring-in-action-v5/di-16-zhang-shi-yong-springboot-actuator/16.3-zi-ding-yi-actuator/16.3.1-xiang-info-duan-dian-ti-gong-xin-xi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
