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添加如下代碼
@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,三十秒內就會重新上線,生產商謹慎打開,內網倒是無所謂