使用Spring Boot2.x Actuator监控应用并控制UP/DOWN

SpringCloud的Admin监控组件基于Actuator
Actuator通过服务的心跳向注册中心(比如Eureka)上报健康状况

服务添加依赖

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

默认端点

Spring Boot 2.0 的Actuator只暴露了health和info端点,提供的监控信息无法满足我们的需求

在1.x中有n多可供我们监控的节点,官方的回答是为了安全

开启所有端点

在application.properties中加入如下配置信息

*代表所有节点都加载

#开启所有端点
management.endpoints.web.exposure.include=*

测试

开启服务,打开:http://{host}:{port}/actuator

可以看到返回了一串JSON,其中有个health,就是服务的健康状态,所谓健康状态,不是说一定服务挂了才是DOWN,而是可能服务没挂,但是没连上MySQL,或者短信发送的量已经达到极限了,这些都有可能需要让服务状态变成DOWN

{"_links":{"self":{"href":"http://localhost:81/actuator","templated":false},"archaius":{"href":"http://localhost:81/actuator/archaius","templated":false},"beans":{"href":"http://localhost:81/actuator/beans","templated":false},"caches-cache":{"href":"http://localhost:81/actuator/caches/{cache}","templated":true},"caches":{"href":"http://localhost:81/actuator/caches","templated":false},"health":{"href":"http://localhost:81/actuator/health","templated":false},"health-path":{"href":"http://localhost:81/actuator/health/{*path}","templated":true},"info":{"href":"http://localhost:81/actuator/info","templated":false},"conditions":{"href":"http://localhost:81/actuator/conditions","templated":false},"shutdown":{"href":"http://localhost:81/actuator/shutdown","templated":false},"configprops":{"href":"http://localhost:81/actuator/configprops","templated":false},"env":{"href":"http://localhost:81/actuator/env","templated":false},"env-toMatch":{"href":"http://localhost:81/actuator/env/{toMatch}","templated":true},"loggers":{"href":"http://localhost:81/actuator/loggers","templated":false},"loggers-name":{"href":"http://localhost:81/actuator/loggers/{name}","templated":true},"heapdump":{"href":"http://localhost:81/actuator/heapdump","templated":false},"threaddump":{"href":"http://localhost:81/actuator/threaddump","templated":false},"metrics-requiredMetricName":{"href":"http://localhost:81/actuator/metrics/{requiredMetricName}","templated":true},"metrics":{"href":"http://localhost:81/actuator/metrics","templated":false},"scheduledtasks":{"href":"http://localhost:81/actuator/scheduledtasks","templated":false},"mappings":{"href":"http://localhost:81/actuator/mappings","templated":false},"refresh":{"href":"http://localhost:81/actuator/refresh","templated":false},"features":{"href":"http://localhost:81/actuator/features","templated":false},"service-registry":{"href":"http://localhost:81/actuator/service-registry","templated":false}}}

打开:http://{host}:{port}/actuator/health,页面显示如下,证明服务可用

{"status":"UP"}

远程控制服务开关

在服务的配置文件application.properties加上如下配置

#可以远程关闭服务节点
management.endpoint.shutdown.enabled=true

添加一个类

package com.bl.eurekaprovider;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Service;

/**
 * @Deacription 改变健康状态的Service
 * @Author BarryLee
 * @Date 2020/6/13 21:30
 */
@Service
public class HealthStatusService implements HealthIndicator {
  private Boolean status = true;

  public void setStatus(Boolean status) {
    this.status = status;
  }

  @Override
  public Health health() {
    if (status) {
      return new Health.Builder().up().build();
    }
    return new Health.Builder().down().build();
  }

  public String getStatus() {
    return this.status.toString();
  }
}

在Controller添加如下代码

扫描二维码关注公众号,回复: 12945083 查看本文章
  @Autowired
  private HealthStatusService healthStatusService;
  /**
   * 用来改变当前服务的状态,测试用
   */
  @GetMapping("/health")
  public String health(@RequestParam("status") Boolean status) {
    // 改变状态
    healthStatusService.setStatus(status);
    // 返回当前状态
    return healthStatusService.getStatus();
  }

浏览器或者postman访问:http://{host}:{port}/health?status=false

在Eureka Server中可以看到服务已经下线了,如果想要上线,改为true,三十秒内就会重新上线,生产商谨慎打开,内网倒是无所谓

猜你喜欢

转载自blog.csdn.net/qq_38238041/article/details/106749040