Use Spring Boot 2.x Actuator to monitor applications and control UP/DOWN

The Admin monitoring component of SpringCloud is based on the Actuator
Actuator to report the health status to the registry (such as Eureka) through the heartbeat of the service

Service add dependency

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

Default endpoint

The Actuator of Spring Boot 2.0 only exposes health and info endpoints, and the monitoring information provided cannot meet our needs

There are more than n nodes for us to monitor in 1.x, the official answer is for safety

Open all endpoints

Add the following configuration information in application.properties

*Means all nodes are loaded

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

test

Open the service, open: http://{host}:{port}/actuator

You can see that a string of JSON is returned, among which there is health, which is the health status of the service. The so-called health status does not mean that the service is down when it is down. It may be that the service is not down, but MySQL is not connected, or SMS is sent. The amount has reached the limit, these may need to make the service status 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}}}

Open: http://{host}:{port}/actuator/health, the page displays as follows, proving that the service is available

{"status":"UP"}

Remote control service switch

Add the following configuration to the service configuration file application.properties

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

Add a class

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();
  }
}

Add the following code to the Controller

  @Autowired
  private HealthStatusService healthStatusService;
  /**
   * 用来改变当前服务的状态,测试用
   */
  @GetMapping("/health")
  public String health(@RequestParam("status") Boolean status) {
    // 改变状态
    healthStatusService.setStatus(status);
    // 返回当前状态
    return healthStatusService.getStatus();
  }

Browser or postman access: http://{host}:{port}/health?status=false

In Eureka Server, you can see that the service has gone offline. If you want to go online, change it to true and it will go online again within 30 seconds. The manufacturer will open it cautiously, but the intranet does not matter.

Guess you like

Origin blog.csdn.net/qq_38238041/article/details/106749040