---
title: "整合Spring boot Actuator"
author: "山野村夫"
date: "2018-04-01T18:28:46Z"
tags: []
categories: ["编程语言"]
slug: "spring-boot-actuator"
draft: false
lastmod: "2018-04-01T18:27:57Z"
id: 2415201
url: "https://scgaoll.iteye.com/blog/2415201"
---

执行器端点允许你监控应用及与应用进行交互。Spring Boot包含很多内置的端点，你也可以添加自己的。例如，health端点提供了应用的基本健康信息。

端点暴露的方式取决于你采用的技术类型。大部分应用选择HTTP监控，端点的ID映射到一个URL。例如，默认情况下，health端点将被映射到/health。

下面的端点都是可用的：

ID 描述 敏感（Sensitive） autoconfig 显示一个auto-configuration的报告，该报告展示所有auto-configuration候选者及它们被应用或未被应用的原因 true beans 显示一个应用中所有Spring Beans的完整列表 true configprops 显示一个所有@ConfigurationProperties的整理列表 true dump 执行一个线程转储 true env 暴露来自Spring　ConfigurableEnvironment的属性 true health 展示应用的健康信息（当使用一个未认证连接访问时显示一个简单的'status'，使用认证连接访问则显示全部信息详情） false info 显示任意的应用信息 false metrics 展示当前应用的'指标'信息 true mappings 显示一个所有@RequestMapping路径的整理列表 true shutdown 允许应用以优雅的方式关闭（默认情况下不启用） true trace 显示trace信息（默认为最新的一些HTTP请求） true

注：根据一个端点暴露的方式，sensitive参数可能会被用做一个安全提示。例如，在使用HTTP访问sensitive端点时需要提供用户名/密码（如果没有启用web安全，可能会简化为禁止访问该端点）。

 1、Enabling EndPoints

       默认情况下，除了shutdown，其它所有端点都是开放开的。以下样例可放开shutdown端点：

 

```
management.endpoint.shutdown.enabled=true
```

   禁用默认端点，只要把management.endpoints.enabled-by-default的值设置为false则可，以下样例

 

启动info端点而禁用其它端点：

 

```
management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true
```

 2、Exposing Endpoints

 

   端点可能包含了一些敏感信息，在生产环境中谨慎暴露这些端点。

  如下样例只暴露health和info端点：

 

```
management.endpoints.jmx.exposure.include=health,info
```

    

 

   以下样例暴露了所有端点，但禁用env和beans两个端点：

```
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
```

 3、端点安全性

 

    典型的一个安全设置如下：

```
@Configuration
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
.anyRequest().hasRole("ENDPOINT_ADMIN")
.and()
.httpBasic();
}
}
```

    如果希望放开所有端点的访问，则在application.properties文件中增加如下配置 ：

 

 

```
management.endpoints.web.exposure.include=*
```

   如果已增加安全访问限制，而希望不需要授权访问，则需要做如下配置 ：

 

 

```
@Configuration
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
.anyRequest().permitAll()
}
}
```

 4、Health信息

 

   以下健康信息自动加载：

Name  Description DiskSpaceHealthIndicator   DataSourceHealthIndicator   ElasticsearchHealthIndicator   InfluxDbHealthIndicator   JmsHealthIndicator   MailHealthIndicator   MongoHealthIndicator   RabbitHealthIndicator   RedisHealthIndicator  

5、自定义HealthIndicators

```
@Component
public class MyHealthIndicator implements HealthIndicator {
@Override
public Health health() {
int errorCode = check(); // perform some specific health check
if (errorCode != 0) {
return Health.down().withDetail("Error Code", errorCode).build();
}
return Health.up().build();
}
}
```

 6、自定义Info

  

```
@Component
public class ExampleInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("example",
Collections.singletonMap("key", "value"));
}
}
```