【服務離線】記一次組件引入形成actuator下線問題

SpringBoot Admin能夠看做是Spring actuator 端點監控的一個UI界面,能夠很方便的查看服務的運行狀況。react

部分服務不在線

如上圖,在項目整合了部分組件後,出現狀態顯示不在線,但應用仍是可以正常的對外提供服務。git

  • 咱們知道 應用展現的在線狀態是讀取的 Spring actuator health endpoint數據
curl http://127.0.0.1:5002/actuator/health

{"status":"DOWN"}

複製代碼
  • 此時沒法經過 SpringBoot Admin 沒法獲取更多的細節, 建議開發環境開啓端點的詳細信息。
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always #dev open
複製代碼
  • 再次嘗試訪問, 輸出了所有組件的狀態,能夠明確的看到因爲 sentinel 狀態不對影響全局狀態爲 down
curl http://127.0.0.1:5002/actuator/health

{"status":"DOWN","components":{"discoveryComposite":{"status":"UP","components":{"discoveryClient":{"status":"UP","details":{"services":["pigx-upms-biz","pigx-tx-manager","pigx-daemon-elastic-job","pigx-mp-platform","pigx-daemon-quartz","pigx-gateway","pigx-auth","pigx-pay-platform","pigx-codegen","pigx-oa-platform","pigx-monitor"]}}}},"diskSpace":{"status":"UP","details":{"total":42927636480,"free":21334122496,"threshold":10485760}},"nacosConfig":{"status":"UP"},"nacosDiscovery":{"status":"UP"},"ping":{"status":"UP"},"reactiveDiscoveryClients":{"status":"UP","components":{"Simple Reactive Discovery Client":{"status":"UP","details":{"services":[]}}}},"redis":{"status":"UP","details":{"version":"5.0.7"}},"refreshScope":{"status":"UP"},"sentinel":{"status":"DOWN","details":{"dataSource":{},"enabled":true,"dashboard":{"description":"pigx-sentinel:5020 can't be connected","status":"DOWN"}}}}}
複製代碼

sentinel dashboadr can't be connected

  • SentinelHealthIndicator.doHealthCheck 來看下 sentinel 的健康檢查是如何處理的
protected void doHealthCheck(Health.Builder builder) throws Exception {
Map<String, Object> detailMap = new HashMap<>();

// 獲取心跳發送器
HeartbeatSender heartbeatSender = HeartbeatSenderProvider
.getHeartbeatSender();
// 發送心跳
boolean result = heartbeatSender.sendHeartbeat();
if (result) {
// 成功標記爲UP
detailMap.put("dashboard", Status.UP);
}
else {
// 失敗 標記爲down
dashboardUp = false;
detailMap.put("dashboard", new Status(Status.DOWN.getCode(),
consoleServer + " can't be connected"));
}
}

// If Dashboard and DataSource are both OK, the health status is UP
if (dashboardUp && dataSourceUp) {
builder.up().withDetails(detailMap);
}
else {
builder.down().withDetails(detailMap);
}
}

複製代碼
  • SimpleHttpHeartbeatSender.sendHeartbeat 心跳發送器的處理邏輯
public boolean sendHeartbeat() throws Exception {

// 1. 判斷客戶端是和服務端是否交互 ,只有交互時dashboard 纔有數據展現
if (TransportConfig.getRuntimePort() <= 0) {
RecordLog.info("[SimpleHttpHeartbeatSender] Runtime port not initialized, won't send heartbeat");
return false;
}
InetSocketAddress addr = getAvailableAddress();
if (addr == null) {
return false;
}

// 2. 訪問dashboard 的接口
SimpleHttpRequest request = new SimpleHttpRequest(addr, TransportConfig.getHeartbeatApiPath());
request.setParams(heartBeat.generateCurrentMessage());
SimpleHttpResponse response = httpClient.post(request);
if (response.getStatusCode() == OK_STATUS) {
return true;
}
return false;
}
複製代碼
  • 究竟是上文代碼 第一步未交互被判斷爲down、仍是第二步訪問出錯判斷爲down

查看 sentinel client 的日誌github

cat ~/csp/sentinel-record.log 

2020-03-23 13:54:04.634 INFO [SimpleHttpHeartbeatSender] Runtime port not initialized, won't send heartbeat

複製代碼

開始是第一步沒有初始化交互形成,服務判斷爲 downweb

sentinel 如何初始化交互

  • 初始化交互端口 commandCenter.start()
public class CommandCenterInitFunc implements InitFunc {

@Override
public void init() throws Exception {
CommandCenter commandCenter = CommandCenterProvider.getCommandCenter();

if (commandCenter == null) {
RecordLog.warn("[CommandCenterInitFunc] Cannot resolve CommandCenter");
return;
}

commandCenter.beforeStart();
// 開始初始化端口交互
commandCenter.start();
RecordLog.info("[CommandCenterInit] Starting command center: "
+ commandCenter.getClass().getCanonicalName());
}
}
複製代碼
  • 配置啓動當即加載初始化,而不是有流量請求再初始化
public class SentinelAutoConfiguration {

@Value("${project.name:${spring.application.name:}}")
private String projectName;

@Autowired
private SentinelProperties properties;

@PostConstruct
private void init() {

// earlier initialize
if (properties.isEager()) {
InitExecutor.doInit();
}

}
複製代碼
spring:
application:
name: @artifactId@
cloud:
sentinel:
eager: true
複製代碼
『★★★★★』 基於Spring Boot 2.二、 Spring Cloud Hoxton & Alibaba、 OAuth2 的RBAC 權限管理系統

image
相關文章
相關標籤/搜索