> For the complete documentation index, see [llms.txt](https://potoyang.gitbook.io/spring-in-action-v4/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://potoyang.gitbook.io/spring-in-action-v4/untitled-14/untitled-1.md).

# 21.4　通过 Actuator 获取了解应用内部状况

Spring Boot Actuator 所完成的主要功能就是为基于 Spring Boot 的应用添加多个有用的管理端点。这些端点包括以下几个内容。

* GET /autoconfig：描述了 Spring Boot 在使用自动配置的时候，所做出的决策；
* GET /beans：列出运行应用所配置的 bean；
* GET /configprops：列出应用中能够用来配置 bean 的所有属性及其当前的值；
* GET /dump：列出应用的线程，包括每个线程的栈跟踪信息；
* GET /env：列出应用上下文中所有可用的环境和系统属性变量；
* GET /env/{name}：展现某个特定环境变量和属性变量的值；
* GET /health：展现当前应用的健康状况；
* GET /info：展现应用特定的信息；
* GET /metrics：列出应用相关的指标，包括请求特定端点的运行次数；
* GET /metrics/{name}：展现应用特定指标项的指标状况；
* POST /shutdown：强制关闭应用；
* GET /trace：列出应用最近请求相关的元数据，包括请求和响应头。

为了启用 Actuator，我们只需将 Actuator Starter 依赖添加到项目中即可。如果你使用 Groovy 编写应用并通过 Spring Boot CLI 来运行，那么可以通过 @Grab 注解来添加 Actuator Starter，如下所示：

```groovy
@Grab("spring-boot-starter-actuator")
```

如果使用 Gradle 构建 Java 应用的话，那么在 build.gradle 的 dependencies 代码块中需要添加如下的依赖：

```
compile("org.springframework.boot:spring-boot-starter-actuator")
```

或者，在项目的 Maven pom.xml 文件中，我们可以添加如下的 \<dependency>：

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

添加完 Spring Boot Actuator 之后，我们可以重新构建并启动应用，然后打开浏览器访问以上所述的端点来获取更多信息。例如，如果想要查看 Spring 应用上下文中所有的 bean，那么可以访问 <http://localhost:8080/beans。如果使用> curl 命令行工具的话，所得到的结果将会如下所示（为了便于阅读，进行了格式化和删减）：

```
$ curl http://localhost:8080/beans
[
  {
    "beans": [
      {
        "bean": "contactController",
        "dependencies": [
          "contactRepository
          ],
          "resource": "null",
          "scope": "singleton",
          "type":"ContactController"
      },
      {
        "bean": "contactRepository",
        "dependencies": [
          "jdbcTemplate"
        ],
        "resource": "null",
        "scope": "singleton",
        "type":"ContactRepository"
      },
      {
        "bean": "jdbcTemplate",
        "dependencies": [],
        "resource": "class path resource [...]",
        "scope": "singleton",
        "type": "org.springframework.jdbc.core.JdbcTemplate"
      },
      
      ...
    ]
  }
]
```

从这里，我们可以看到有一个 ID 为 contactController 的 bean，它依赖于名为 contactRepository 的 bean，而 contactRepository 又依赖于 jdbcTemplate bean。

因为我对输出进行了删减，所以有很多的 bean 没有展现出来，它们都包含在 “/beans” 端点所产生的 JSON 中。对于自动装配和自动配置所形成的神秘结果，这里提供了一种了解内部实现的手段。

另外一个端点也能帮助我们了解 Spring Boot 自动配置的内部情况，这就是 “/autoconfig”。这个端点所返回的 JSON 描述了 Spring Boot 在自动配置 bean 的时候所做出的决策。例如，当针对 Contacts 应用调用 “/autoconfig” 端点时，如下展现了删减后（并进行了格式化）的 JSON 结果：

```
$ curl http://localhost:8080/autoconfig
{
"negativeMatches": {
  "AopAutoConfiguration": [
    {
      "condition": "OnClassCondition",
      "message": "required @ConditionalOnClass classes not found:
        org.aspectj.lang.annotation.Aspect,
        org.aspectj.lang.reflect.Advice"
    }
  ],
  "BatchAutoConfiguration": [
    {
      "condition": "OnClassCondition",
      "message": "required @ConditionalOnClass classes not found:
        org.springf ramework.batch.core.launch.JobLauncher"
    }
  ],
  
  ...
},
"positiveMatches": {
  "ThymeleafAutoConfiguration": [
    {
      "condition": "OnClassCondition",
      "message": "QConditionalOnClass classes found:
        org.thymeleaf.spring4.SpringTemplateEngine"
    }
  ],
  "ThymeleafAutoConfiguration.DefaultTemplateResolverConfiguration": [
    {
      "condition": "OnBeanCondition",
      "message": "©ConditionalOnMissingBean
        (names: defaultTemplateResolver; SearchStrategy: all)
        found no beans"
    }
  ],
  "ThymeleafAutoConfiguration.ThymeleafDefaultConfiguration": [
    {
      "condition": "OnBeanCondition",
      "message": "OConditionalOnMissingBean (types:
        org.thymeleaf.spring4.SpringTemplateEngine;
        SearchStrategy: all) found no beans"
    }
  ],
  "ThymeleafAutoConfiguration.ThymeleafViewResolverConfiguration": [
    {
      "condition": "OnClassCondition",
      "message": "OConditionalOnClass classes found:
        javax.servlet.Servlet"
    }
  ],
  "ThymeleafAutoConfiguration.ThymeleafViewResolverConfiguration
      #thymeleafViewResolver": [
    {
      "condition": "OnBeanCondition",
      "message": "@ConditionalOnMissingBean (names:
        thymeleafViewResolver; SearchStrategy: all)
        found no beans"
    }
  ],
  
  ...
}
}
```

我们可以看到，这个报告包含了两部分：一部分是没有匹配上的（negative matches），另一部分是匹配上的（positive matches）。在没有匹配的部分中，表明没有使用 AOP 和自动配置，因为在类路径中没有找到所需的类。在匹配上的部分中，我们可以看到，因为在类路径下找到了 SpringTemplateEngine，Thymeleaf 自动配置将会发挥作用。同时还可以看到，除非明确声明了默认的模板解析器、视图解析器以及模板 bean 否则的话，这些 bean 会进行自动配置。另外，只 有在类路径中能够找到 Servlet 类，才会自动配置默认的视图解析器。

“/beans” 和 “/autoconfig” 端点只是 Spring Boot Actuator 所提供的观察应用内部状况的两个样例。在本章中，我们没有足够的篇幅详细讨论每个端点，但是我建议你自行尝试这些端点，以便掌握 Actuator 都提供了哪些功能来帮助我们了解应用的内部状况。
