From d43afb282e7f119a37f52829ca85d5066054e124 Mon Sep 17 00:00:00 2001 From: heavyrain2012 Date: Fri, 13 Aug 2021 16:37:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=81=A5=E5=BA=B7=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../loServer/action/admin/HealthAction.java | 8 +-- .../cn/wildfirechat/common/ErrorCode.java | 1 + .../wildfirechat/pojos/HealthCheckResult.java | 29 +++++++++++ .../cn/wildfirechat/sdk/GeneralAdmin.java | 4 ++ .../main/java/cn/wildfirechat/sdk/Main.java | 8 +++ .../sdk/utilities/AdminHttpUtils.java | 50 ++++++++++++++++++- 6 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 common/src/main/java/cn/wildfirechat/pojos/HealthCheckResult.java diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/HealthAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/HealthAction.java index fd11ee49..b58c733e 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/HealthAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/HealthAction.java @@ -37,6 +37,8 @@ public class HealthAction extends AdminAction { if (request.getNettyRequest() instanceof FullHttpRequest) { JSONObject jsonObject = new JSONObject(); + jsonObject.put("node", "1"); + JSONObject cpuObject = new JSONObject(); jsonObject.put("cpu", cpuObject); @@ -60,9 +62,9 @@ public class HealthAction extends AdminAction { fileObject.put("space", root.getTotalSpace()); fileObject.put("free", root.getFreeSpace()); fileObject.put("usable", root.getUsableSpace()); - - JSONObject out = new JSONObject(); - out.put("node1", jsonObject); + + JSONArray out = new JSONArray(); + out.add(jsonObject); RestResult result = RestResult.ok(out); setResponseContent(result, response); } diff --git a/common/src/main/java/cn/wildfirechat/common/ErrorCode.java b/common/src/main/java/cn/wildfirechat/common/ErrorCode.java index cb2f06bb..1d5776a3 100755 --- a/common/src/main/java/cn/wildfirechat/common/ErrorCode.java +++ b/common/src/main/java/cn/wildfirechat/common/ErrorCode.java @@ -44,6 +44,7 @@ public enum ErrorCode { ERROR_CODE_RECALL_TIME_EXPIRED(24, "time expired"), ERROR_CODE_CONFERENCE_ROOM_NOT_EXIST(60, "conference room not exist"), + ERROR_CODE_USER_NOT_PREPARED(238, "user not prepared"), ERROR_CODE_API_NOT_SIGNED(239, "api not signed or sign parameter not completion"), ERROR_CODE_GROUP_EXCEED_MAX_MEMBER_COUNT(240, "group exceed max member count"), ERROR_CODE_GROUP_MUTED(241, "group is muted"), diff --git a/common/src/main/java/cn/wildfirechat/pojos/HealthCheckResult.java b/common/src/main/java/cn/wildfirechat/pojos/HealthCheckResult.java new file mode 100644 index 00000000..963511f0 --- /dev/null +++ b/common/src/main/java/cn/wildfirechat/pojos/HealthCheckResult.java @@ -0,0 +1,29 @@ +package cn.wildfirechat.pojos; + +import java.util.ArrayList; +import java.util.List; + +public class HealthCheckResult extends ArrayList +{ + public static class Node { + public String node; + public CPU cpu; + public Memory memory; + public Disk disk; + } + + public static class Memory { + public long avail; + public long max; + public long free; + } + public static class Disk { + public long usable; + public long free; + public long space; + } + public static class CPU { + public int cores; + public double load; + } +} diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/GeneralAdmin.java b/sdk/src/main/java/cn/wildfirechat/sdk/GeneralAdmin.java index 58e014e0..6077c38c 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/GeneralAdmin.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/GeneralAdmin.java @@ -63,4 +63,8 @@ public class GeneralAdmin { pojo.setValue(value); return AdminHttpUtils.httpJsonPost(path, pojo, Void.class); } + + public static IMResult healthCheck() throws Exception { + return AdminHttpUtils.httpGet(APIPath.Health, HealthCheckResult.class); + } } diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java index b545b4bf..b4beec1f 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java @@ -707,6 +707,14 @@ public class Main { System.out.println("create channel failure"); System.exit(-1); } + + IMResult health = GeneralAdmin.healthCheck(); + if(health != null && health.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println(health.result); + } else { + System.out.println("health check failure"); + System.exit(-1); + } } static void testChatroom() throws Exception { diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/AdminHttpUtils.java b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/AdminHttpUtils.java index f200a26d..98c45707 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/AdminHttpUtils.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/AdminHttpUtils.java @@ -7,6 +7,7 @@ import org.apache.commons.codec.digest.DigestUtils; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; @@ -34,7 +35,54 @@ public class AdminHttpUtils { httpClient = HttpClients.createDefault(); } - public static IMResult httpJsonPost(String path, Object object, Class clazz) throws Exception{ + public static IMResult httpGet(String path, Class clazz) throws Exception { + if (isNullOrEmpty(adminUrl) || isNullOrEmpty(adminSecret)) { + LOG.error("野火IM Server SDK必须先初始化才能使用,是不是忘记初始化了!!!!"); + throw new Exception("SDK没有初始化"); + } + + if (isNullOrEmpty(path)) { + throw new Exception("路径缺失"); + } + HttpGet get = null; + try { + get = new HttpGet(adminUrl + path); + HttpResponse response = httpClient.execute(get); + + int statusCode = response.getStatusLine().getStatusCode(); + String content = null; + if (response.getEntity().getContentLength() > 0) { + BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8")); + StringBuilder sb = new StringBuilder(); + String line; + String NL = System.getProperty("line.separator"); + while ((line = in.readLine()) != null) { + sb.append(line).append(NL); + } + + in.close(); + + content = sb.toString(); + LOG.info("http request response content: {}", content); + } + + if(statusCode != HttpStatus.SC_OK){ + LOG.info("Request error: " + statusCode + " error msg:" + content); + throw new Exception("Http request error with code:" + statusCode); + } else { + return fromJsonObject(content, clazz); + } + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + if(get != null) { + get.releaseConnection(); + } + } + } + + public static IMResult httpJsonPost(String path, Object object, Class clazz) throws Exception { if (isNullOrEmpty(adminUrl) || isNullOrEmpty(adminSecret)) { LOG.error("野火IM Server SDK必须先初始化才能使用,是不是忘记初始化了!!!!"); throw new Exception("SDK没有初始化");