|
@@ -0,0 +1,89 @@
|
|
|
|
|
+package cn.reghao.devops.mgr.mgr.thirdparty.consul;
|
|
|
|
|
+
|
|
|
|
|
+import cn.reghao.jutil.jdk.http.WebClient;
|
|
|
|
|
+import cn.reghao.jutil.jdk.http.WebRequest;
|
|
|
|
|
+import cn.reghao.jutil.jdk.http.WebResponse;
|
|
|
|
|
+import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
|
|
+import com.google.gson.JsonArray;
|
|
|
|
|
+import com.google.gson.JsonElement;
|
|
|
|
|
+import com.google.gson.JsonObject;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.net.URI;
|
|
|
|
|
+import java.net.URISyntaxException;
|
|
|
|
|
+import java.net.http.HttpClient;
|
|
|
|
|
+import java.net.http.HttpRequest;
|
|
|
|
|
+import java.net.http.HttpResponse;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author reghao
|
|
|
|
|
+ * @date 2023-02-22 17:29:13
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+public class ConsulService {
|
|
|
|
|
+ private final WebRequest webRequest = new WebClient();
|
|
|
|
|
+ private final String host = "http://consul.iquizoo.cn";
|
|
|
|
|
+
|
|
|
|
|
+ public List<ConsulNode> getNodes() {
|
|
|
|
|
+ List<ConsulNode> nodes = new ArrayList<>();
|
|
|
|
|
+ String api = String.format("%s/%s", host, "v1/catalog/nodes");
|
|
|
|
|
+ WebResponse webResponse = webRequest.get(api);
|
|
|
|
|
+ if (webResponse.getStatusCode() == 200) {
|
|
|
|
|
+ JsonArray jsonArray = JsonConverter.jsonToJsonElement(webResponse.getBody()).getAsJsonArray();
|
|
|
|
|
+ for (JsonElement jsonElement : jsonArray) {
|
|
|
|
|
+ JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
|
|
|
+ String dc = jsonObject.get("Datacenter").getAsString();
|
|
|
|
|
+ String address = jsonObject.get("Address").getAsString();
|
|
|
|
|
+ nodes.add(new ConsulNode(dc, address));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return nodes;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<String> getServices() {
|
|
|
|
|
+ String api = String.format("%s/%s", host, "v1/catalog/services");
|
|
|
|
|
+ WebResponse webResponse = webRequest.get(api);
|
|
|
|
|
+ if (webResponse.getStatusCode() == 200) {
|
|
|
|
|
+ JsonObject jsonObject = JsonConverter.jsonToJsonElement(webResponse.getBody()).getAsJsonObject();
|
|
|
|
|
+ return new ArrayList<>(jsonObject.keySet());
|
|
|
|
|
+ }
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<String> getCriticalServices() {
|
|
|
|
|
+ String api = String.format("%s/%s", host, "v1/health/state/critical");
|
|
|
|
|
+ WebResponse webResponse = webRequest.get(api);
|
|
|
|
|
+ if (webResponse.getStatusCode() == 200) {
|
|
|
|
|
+ JsonObject jsonObject = JsonConverter.jsonToJsonElement(webResponse.getBody()).getAsJsonObject();
|
|
|
|
|
+ return new ArrayList<>(jsonObject.keySet());
|
|
|
|
|
+ }
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void deregister(String service) throws URISyntaxException, IOException, InterruptedException {
|
|
|
|
|
+ String url = String.format("%s/%s/%s", host, "v1/agent/service/deregister", service);
|
|
|
|
|
+ HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(url)).version(HttpClient.Version.HTTP_1_1);
|
|
|
|
|
+
|
|
|
|
|
+ HttpRequest httpRequest = builder.PUT(HttpRequest.BodyPublishers.noBody()).build();
|
|
|
|
|
+ HttpClient httpClient = HttpClient.newBuilder().build();
|
|
|
|
|
+ HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
+ int statusCode = httpResponse.statusCode();
|
|
|
|
|
+ String body = httpResponse.body();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
|
|
|
|
|
+ ConsulService consulService = new ConsulService();
|
|
|
|
|
+ List<ConsulNode> consulNodes = consulService.getNodes();
|
|
|
|
|
+
|
|
|
|
|
+ List<String> list = consulService.getServices();
|
|
|
|
|
+ System.out.println(list);
|
|
|
|
|
+
|
|
|
|
|
+ String service = "FileGrpcService_172_16_45_66_8104";
|
|
|
|
|
+ service = "FileService-WebApi_172_16_45_66_8004";
|
|
|
|
|
+ //consulService.deregister(service);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|