功能:完善 YumiNail 全栈修复与打包
This commit is contained in:
+65
@@ -0,0 +1,65 @@
|
||||
package com.xiaowang.yumitailgateway.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/health")
|
||||
@RequiredArgsConstructor
|
||||
public class HealthController {
|
||||
|
||||
private final DiscoveryClient discoveryClient;
|
||||
private final Environment environment;
|
||||
|
||||
@GetMapping("/check")
|
||||
public Map<String, Object> check() {
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("code", 200);
|
||||
result.put("message", "YumiNail-Gateway is running");
|
||||
result.put("data", diagnosticsPayload());
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("/diagnostics")
|
||||
public Map<String, Object> diagnostics() {
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("code", 200);
|
||||
result.put("message", "operation successful");
|
||||
result.put("data", diagnosticsPayload());
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<String, Object> diagnosticsPayload() {
|
||||
Map<String, Object> diagnostics = new LinkedHashMap<>();
|
||||
String serviceName = environment.getProperty("spring.application.name", "yuminail-gateway");
|
||||
diagnostics.put("service", serviceName);
|
||||
diagnostics.put("port", environment.getProperty("server.port", ""));
|
||||
diagnostics.put("gatewayService", discoverySummary(serviceName));
|
||||
diagnostics.put("coreService", discoverySummary("yumi-nail-core"));
|
||||
return diagnostics;
|
||||
}
|
||||
|
||||
private Map<String, Object> discoverySummary(String serviceName) {
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
try {
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
|
||||
result.put("ok", !instances.isEmpty());
|
||||
result.put("service", serviceName);
|
||||
result.put("instanceCount", instances.size());
|
||||
} catch (Exception exception) {
|
||||
result.put("ok", false);
|
||||
result.put("service", serviceName);
|
||||
result.put("error", exception.getClass().getSimpleName());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user