在SpringBoot2.0版本中,actuator可以为我们提供以下端点:

访问路径

描述

/actuator/auditevents

显示当前应用程序的审计事件信息

/actuator/beans

显示一个应用中所有Spring Beans的完整列表

/actuator/conditions

显示配置类和自动配置类的状态及它们被应用或未被应用的原因

/actuator/configprops

显示一个所有@ConfigurationProperties的集合列表

/actuator/env

显示来自Spring的 ConfigurableEnvironment的属性

/actuator/features

显示系统启动的一些features

/actuator/health

显示应用的健康信息

/actuator/httptrace

最后100个HTTP请求

/actuator/info

显示任意的应用信息

/actuator/metrics

展示当前应用的metrics信息

/actuator/mappings

显示一个所有@RequestMapping路径的集合列表

/actuator/refresh

更新配置

/actuator/scheduledtasks

显示应用程序中的定时任务

/actuator/service-registry

当前应用在注册中心的状态

/actuator/shutdown

允许应用以优雅的方式关闭

/actuator/threaddump

执行一个线程dump

/actuator/heapdump

返回一个GZip压缩的hprof堆dump文件

/actuator/loggers

返回系统的一些日志

虽然actuator默认给我们提供了这么多的端点供我们使用,但是为了安全起见,在SpringBoot2.0中它仅仅开放了health和info两个端口,如果想要使用其他的端口就需要我们增加一些配置了,一起来看一下如何使用actuator吧。

1. 引入依赖



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



2. 修改配置文件



management:
  endpoints:
    web:
      exposure:
        #exclude: shutdown,threaddump   #此处控制的是不开放哪些端点
        include: "*" #此处控制的是开放哪些端点,如果需要开启少数端点可以这样配置:health,info。如果开启多个则使用*号开启除了exclude的端点


这个时候我们使用postman等接口调用工具访问 ip:端口/actuator 这个路径时就会得到下图所示的这么一个json串,这个json串中就是对应的各个端点的地址信息。

spring cloud 项目初始化监听 spring cloud监控工具_连接时间

3. 健康检查

默认我们访问/actuator/health得到的只是一个状态值,其实它的详细信息里包含了很多有用的东西,比如说检查磁盘空间、DataSource的连接、Elasticsearch、Mongo、Rabbit、Redis等信息,我们可以通过如下配置来开启详细的健康检查:



management:
  endpoint:
    health:
      show-details: always



不仅如此,健康检查的指标我们还可以自定义,创建如下的一个bean提供健康检查的功能。



@Component
public class ConnectTimeHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        long connectTime=(long)Math.random()*10;//模拟一个连接操作
        if(connectTime>3){
            //如果连接时间大于3则认为连接失败,返回状态为down
            return Health.down().withDetail("code", "504").withDetail("msg","xx应用连接超时").build();
        }
        return Health.up().build();
    }
}


此时我们访问 ip:端口/actuator/health 访问时可能就会根据连接时间呈现下方的两种状态

spring cloud 项目初始化监听 spring cloud监控工具_连接时间_02


spring cloud 项目初始化监听 spring cloud监控工具_连接时间_03