Jelajahi Sumber

healthcheck

zhaotx 1 tahun lalu
induk
melakukan
cd73db98a3

+ 69 - 9
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/service/impl/HealthCheckServiceImpl.java

@@ -2,11 +2,24 @@ package cn.com.ctop.job.bytedance.data.service.impl;
 
 import cn.com.ctop.job.bytedance.data.service.IHealthCheckService;
 import cn.com.ctop.job.bytedance.data.utils.HttpUtils;
+import cn.com.ctop.job.bytedance.utils.FeishuRobotUtil;
 import com.alibaba.fastjson.JSONObject;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
 import org.springframework.stereotype.Service;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 @Service
 public class HealthCheckServiceImpl implements IHealthCheckService {
@@ -21,25 +34,72 @@ public class HealthCheckServiceImpl implements IHealthCheckService {
                 "123.57.9.211"//test
         );
 
-        for (int i = 0; i < 10; i++) {
+        Map<String, Integer> ipErrorCount = new HashMap<>();
+        for (String ip : ips) {
+            ipErrorCount.put(ip, 0);
+        }
+        for (int i = 0; i < 8; i++) {
             for (String ip : ips) {
-                String url = "http://" + ip + ":9002/actuator/health";
+                String url = "http://" + ip + ":9002/monitor/server/healthCheck";
                 System.out.println(url);
                 try {
-                    String res = HttpUtils.httpGet(url, null, null);
-                    JSONObject obj = JSONObject.parseObject(res);
-                    String status = obj.getString("status");
-                    if (!"UP".equals(status)) {
-                        System.out.println("Health check failed for " + ip + ": " + status);
-                    } else {
+                    int status = healthCheckRequest(url);
+                    if (status == 0) {
                         System.out.println("Health check passed for " + ip);
+                    } else {
+                        ipErrorCount.put(ip, ipErrorCount.get(ip) + 1);
                     }
                 }catch (Exception e) {
                     System.out.println("Health check failed for " + ip + ": " + e.getMessage());
+                    ipErrorCount.put(ip, ipErrorCount.get(ip) + 1);
+                }
+            }
+            Thread.sleep(1500);
+        }
+
+        for (String ip : ips) {
+            if (ipErrorCount.get(ip) > 6) {
+                System.out.println("Health check failed for " + ip + ", sending alert");
+//                FeishuRobotUtil.sendTextToWebhook("Health check failed for " + ip, FS_HOOK_URL);
+            }
+        }
+
+    }
+
+    private static int healthCheckRequest(String url) throws Exception {
+        //一个最简单的http请求,GET,无参数,超时时间为1秒
+        HttpGet httpEntity = new HttpGet(url);
+        CloseableHttpResponse response = null;
+        CloseableHttpClient client = null;
+        try {
+            client = HttpClientBuilder.create().build();
+            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(1000).setConnectTimeout(1000).build();
+            httpEntity.setConfig(requestConfig);
+            response = client.execute(httpEntity);
+            if (response != null && response.getStatusLine().getStatusCode() == 200) {
+                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
+                StringBuilder result = new StringBuilder();
+                String line = "";
+                while ((line = bufferedReader.readLine()) != null) {
+                    result.append(line);
                 }
+                bufferedReader.close();
+                return result.toString().equals("SUCCESS") ? 0 : 500;
+            }
+        } finally {
+            try {
+                if (response != null) {
+                    response.close();
+                }
+                if (client != null) {
+                    client.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
             }
-            Thread.sleep(3000);
         }
+        return 500;
+
     }