写作时间:2019-08-02
Spring Boot: 2.1 ,JDK: 1.8, IDE: IntelliJ IDEA, MySQL 8.0.13

说明

Spring Boot Actuator 是安全检查组件。Spring Boot includes a number of additional features to help you monitor and manage your application when you push it to production. You can choose to manage and monitor your application by using HTTP endpoints or with JMX. Auditing, health, and metrics gathering can also be automatically applied to your application.

工程建立

参照教程【SpringBoot 2.1 | 第一篇:构建第一个SpringBoot工程】新建一个Spring Boot项目,名字叫demoactuator, 在目录src/main/java/resources 下找到配置文件application.properties,重命名为application.yml

在Dependency中选择Ops > Spring Boot Actuator。

自定义springboot加载配置的顺序 springboot actuator 自定义_spring

配置pom.xml依赖

pom.xml添加Spring Boot Actuator依赖(如果上面选择了Actuator,则配置已经存在)

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

Controller处理请求

com.zgpeace.thymeleaf.demoactuator.DemoactuatorApplication

package com.zgpeace.demoactuator;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoactuatorApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoactuatorApplication.class, args);
  }

  @RequestMapping("/hello")
  public String hello() {
    return "Hello Actuator";
  }
}

启动代码

代码启动成功

自定义springboot加载配置的顺序 springboot actuator 自定义_Sprint Boot_02

启动Idea 的Terminal,

看服务器能否相应request:

curl http://localhost:8080/hello
Hello Actuator%

自定义springboot加载配置的顺序 springboot actuator 自定义_Sprint Boot_03

查看Actuator health
% curl http://localhost:8080/actuator/health
{“status”:“UP”}%

health说明

health 主要用来检查应用的运行状态,这是我们使用最高频的一个监控点。通常使用此接口提醒我们应用实例的运行状态,以及应用不”健康“的原因,比如数据库连接、磁盘空间不够等。

默认情况下 health 的状态是开放的,添加依赖后启动项目,访问:http://localhost:8080/actuator/health即可看到应用的状态。

{
    "status" : "UP"
}

默认情况下,最终的 Spring Boot 应用的状态是由 HealthAggregator 汇总而成的,汇总的算法是:

  1. 设置状态码顺序:setStatusOrder(Status.DOWN, Status.OUT_OF_SERVICE, Status.UP, Status.UNKNOWN);。
  2. 过滤掉不能识别的状态码。
  3. 如果无任何状态码,整个 Spring Boot 应用的状态是 UNKNOWN。
  4. 将所有收集到的状态码按照 1 中的顺序排序。
  5. 返回有序状态码序列中的第一个状态码,作为整个 Spring Boot 应用的状态。
    health 通过合并几个健康指数检查应用的健康情况。Spring Boot Actuator 有几个预定义的健康指标比如DataSourceHealthIndicator, DiskSpaceHealthIndicator, MongoHealthIndicator, RedisHealthIndicator等,它使用这些健康指标作为健康检查的一部分。

举个例子,如果你的应用使用 Redis,RedisHealthindicator 将被当作检查的一部分;如果使用 MongoDB,那么MongoHealthIndicator 将被当作检查的一部分。

可以在配置文件中关闭特定的健康检查指标,比如关闭 redis 的健康检查:

management.health.redise.enabled=false

默认,所有的这些健康指标被当作健康检查的一部分。

总结

恭喜你,学会了应用Actuator查看 health。
代码下载:

https://github.com/zgpeace/Spring-Boot2.1/tree/master/tool/demoactuator

参考

https://spring.io/guides/gs/actuator-service/ https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready
http://www.ityouknow.com/springboot/2018/02/06/spring-boot-actuator.html