From cf0402bb51f31ffaf8fa846b56f1e95d7b742e75 Mon Sep 17 00:00:00 2001 From: jianjun Date: Fri, 3 Jul 2020 13:09:54 +0800 Subject: [PATCH] =?UTF-8?q?=E9=92=89=E9=92=89=E6=9C=BA=E5=99=A8=E4=BA=BA?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../epmet/commons/tools/enums/EnvEnum.java | 44 +++++++++++ .../tools/filter/LogMsgSendFilter.java | 10 +++ .../tools/utils/DingdingMsgSender.java | 73 +++++++++++++++++-- .../data-statistical-server/pom.xml | 8 ++ .../com/epmet/controller/DemoController.java | 6 +- .../src/main/resources/bootstrap.yml | 7 +- .../src/main/resources/logback-spring.xml | 8 +- .../epmet-job/epmet-job-server/pom.xml | 8 +- .../src/main/resources/bootstrap.yml | 5 ++ .../src/main/resources/logback-spring.xml | 8 +- 10 files changed, 163 insertions(+), 14 deletions(-) create mode 100644 epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/enums/EnvEnum.java diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/enums/EnvEnum.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/enums/EnvEnum.java new file mode 100644 index 0000000000..ed265248e6 --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/enums/EnvEnum.java @@ -0,0 +1,44 @@ +package com.epmet.commons.tools.enums; + +/** + * 系统环境变量枚举类 + * dev|test|prod + * + * @author jianjun liu + * @date 2020-07-03 11:14 + **/ +public enum EnvEnum { + DEV("dev", "开发环境"), + TEST("test", "体验环境"), + PROD("prod", "生产环境"), + UN_KNOWN("un_known", "未知"), + ; + + private String code; + private String name; + + + + EnvEnum(String code, String name) { + this.code = code; + this.name = name; + } + + public static EnvEnum getEnum(String code) { + EnvEnum[] values = EnvEnum.values(); + for (EnvEnum value : values) { + if (code != null && value.getCode().equals(code)) { + return value; + } + } + return EnvEnum.UN_KNOWN; + } + + public String getCode() { + return code; + } + + public String getName() { + return name; + } +} diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/filter/LogMsgSendFilter.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/filter/LogMsgSendFilter.java index f3c70840d6..2ffe50a5f4 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/filter/LogMsgSendFilter.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/filter/LogMsgSendFilter.java @@ -7,6 +7,7 @@ import ch.qos.logback.classic.spi.IThrowableProxy; import ch.qos.logback.classic.spi.StackTraceElementProxy; import ch.qos.logback.core.spi.FilterReply; import com.epmet.commons.tools.dto.form.DingTalkTextMsg; +import com.epmet.commons.tools.enums.EnvEnum; import com.epmet.commons.tools.utils.DingdingMsgSender; import com.epmet.commons.tools.utils.IpUtils; import org.apache.commons.lang3.StringUtils; @@ -26,6 +27,7 @@ public class LogMsgSendFilter extends LevelFilter { private DingdingMsgSender msgSender = new DingdingMsgSender(); private String webHook; private String secret; + private String activeEnv; @Override public FilterReply decide(ILoggingEvent event) { @@ -39,6 +41,10 @@ public class LogMsgSendFilter extends LevelFilter { stringBuilder.append("告警级别:" + event.getLevel()); stringBuilder.append("\n"); + if (StringUtils.isNotBlank(activeEnv)) { + stringBuilder.append("告警环境:" + EnvEnum.getEnum(activeEnv).getName()); + stringBuilder.append("\n"); + } String serverIp = IpUtils.getServerIp(); if (StringUtils.isNotBlank(serverIp)) { stringBuilder.append("IP地址:" + serverIp); @@ -112,6 +118,10 @@ public class LogMsgSendFilter extends LevelFilter { return dateFormat.format(timestamp); } + public void setActiveEnv(String activeEnv) { + this.activeEnv = activeEnv; + } + public void setWebHook(String webHook) { this.webHook = webHook; } diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/DingdingMsgSender.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/DingdingMsgSender.java index 4250c158b8..59016ccc88 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/DingdingMsgSender.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/DingdingMsgSender.java @@ -2,6 +2,7 @@ package com.epmet.commons.tools.utils; import com.alibaba.fastjson.JSON; import com.epmet.commons.tools.dto.form.DingTalkTextMsg; +import com.epmet.commons.tools.exception.EpmetErrorCode; import com.google.common.collect.Lists; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; @@ -12,7 +13,12 @@ import org.springframework.stereotype.Component; import javax.annotation.PreDestroy; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.URL; +import java.net.URLConnection; import java.net.URLEncoder; import java.util.concurrent.ArrayBlockingQueue; @@ -48,7 +54,7 @@ public class DingdingMsgSender { //阻塞取元素 msg = msgQueue.take(); if (msg != null) { - sendPostByJSON(msg); + sendMsg(msg); } else { Thread.sleep(1000); } @@ -81,7 +87,7 @@ public class DingdingMsgSender { DingTalkTextMsg param = new DingTalkTextMsg(); param.setContent("待发送消息队列已满,当前队列个数" + msgQueue.size() + "\n" + "最新消息内容:" + JSON.toJSONString(messageParam)); param.setWebHook(messageParam.getWebHook()); - sendPostByJSON(param); + sendMsg(param); } return flag; } @@ -94,7 +100,7 @@ public class DingdingMsgSender { * @throws IOException */ public Result sendMsgSync(DingTalkTextMsg messageParam) { - return sendPostByJSON(messageParam); + return sendMsg(messageParam); } private Thread getThread() { @@ -121,7 +127,7 @@ public class DingdingMsgSender { * @param param 请求参数,JSON格式 * @return */ - private Result sendPostByJSON(DingTalkTextMsg param) { + private Result sendMsg(DingTalkTextMsg param) { if (StringUtils.isBlank(param.getWebHook())) { param.setWebHook(webHook); } @@ -139,13 +145,70 @@ public class DingdingMsgSender { String url = param.getWebHook(); url = url.concat("×tamp=" + timestamp + "&sign=" + sign); String jsonStrParam = param.getMsgContent(); - result = HttpClientManager.getInstance().sendPostByJSON(url, jsonStrParam); + result = this.sendPostByJSON(url, jsonStrParam); } catch (Exception e) { logger.warn("sendPostByJSON error", e); } return result; } + /** + * 发送POST 请求 + * + * @param url 发送请求的 URL + * @param param 请求参数,JSON格式 + * @return + */ + public Result sendPostByJSON(String url, String param) throws IOException { + if (StringUtils.isEmpty(url) || StringUtils.isEmpty(param)) { + throw new IllegalArgumentException("参数不能为空"); + } + PrintWriter out = null; + BufferedReader in = null; + String result = ""; + try { + URL realUrl = new URL(url); + // 打开和URL之间的连接 + URLConnection conn = realUrl.openConnection(); + // 设置通用的请求属性 + conn.setRequestProperty("accept", "*/*"); + conn.setRequestProperty("Content-Type", "application/json"); + conn.setRequestProperty("connection", "Keep-Alive"); + conn.setRequestProperty("user-agent", + "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + // 发送POST请求必须设置如下两行 + conn.setDoOutput(true); + conn.setDoInput(true); + // 获取URLConnection对象对应的输出流 + out = new PrintWriter(conn.getOutputStream()); + // 发送请求参数 + out.print(param); + // flush输出流的缓冲 + out.flush(); + // 定义BufferedReader输入流来读取URL的响应 + in = new BufferedReader( + new InputStreamReader(conn.getInputStream())); + String line; + while ((line = in.readLine()) != null) { + result += line; + } + } catch (Exception e) { + logger.warn("sendPostByJSON error", e); + return new Result().error(EpmetErrorCode.SERVER_ERROR.getCode(), e.getMessage()); + } finally { + try { + if (out != null) { + out.close(); + } + if (in != null) { + in.close(); + } + } catch (IOException ex) { + } + + } + return new Result().ok(result); + } public static void main(String[] args) { for (int i = 0; i < 50; i++) { diff --git a/epmet-module/data-statistical/data-statistical-server/pom.xml b/epmet-module/data-statistical/data-statistical-server/pom.xml index 35dc700f49..5c343b5600 100644 --- a/epmet-module/data-statistical/data-statistical-server/pom.xml +++ b/epmet-module/data-statistical/data-statistical-server/pom.xml @@ -175,6 +175,10 @@ 8 10 30 + + + https://oapi.dingtalk.com/robot/send?access_token=90782b119f82a5b6bb8e0f819b6a77bbc2102b53aa2d7d2e24fa10b66d580b1c + SEC080aac67ff78e79fdaba132aa51e3fb3f6060dec99492feaac82cabf9f8b6a19 @@ -257,6 +261,10 @@ 8 10 30 + + + https://oapi.dingtalk.com/robot/send?access_token=a5f66c3374b1642fe2142dbf56d5997e280172d4e8f2b546c9423a68c82ece6c + SEC95f4f40b533ad379ea6a6d1af6dd37029383cfe1b7cd96dfac2678be2c1c3ed1 diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java index 5f562630dc..2bc11e4e43 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java @@ -27,9 +27,9 @@ public class DemoController { @GetMapping("testAlarm") public void testAlarm() { - for (int i = 0; i < 20; i++) { - log.error("测试消息"+i); - } + //for (int i = 0; i < 20; i++) { + log.error("测试消息"); + //} } @GetMapping("testtx") diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml b/epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml index ad6caadfa5..c2ed87bf1e 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml +++ b/epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml @@ -162,4 +162,9 @@ thread: corePoolSize: @thread.pool.core-pool-size@ maxPoolSize: @thread.pool.max-pool-size@ queueCapacity: @thread.pool.queue-capacity@ - keepAlive: @thread.pool.keep-alive@ \ No newline at end of file + keepAlive: @thread.pool.keep-alive@ + +dingTalk: + robot: + webHook: @dingTalk.robot.webHook@ + secret: @dingTalk.robot.secret@ \ No newline at end of file diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/resources/logback-spring.xml b/epmet-module/data-statistical/data-statistical-server/src/main/resources/logback-spring.xml index 39e86ff5ba..3d93d8a331 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/resources/logback-spring.xml +++ b/epmet-module/data-statistical/data-statistical-server/src/main/resources/logback-spring.xml @@ -5,6 +5,9 @@ + + + ${appname} @@ -130,8 +133,9 @@ ERROR ACCEPT DENY - https://oapi.dingtalk.com/robot/send?access_token=90782b119f82a5b6bb8e0f819b6a77bbc2102b53aa2d7d2e24fa10b66d580b1c - SEC080aac67ff78e79fdaba132aa51e3fb3f6060dec99492feaac82cabf9f8b6a19 + ${activeEnv} + ${webHook} + ${secret} diff --git a/epmet-module/epmet-job/epmet-job-server/pom.xml b/epmet-module/epmet-job/epmet-job-server/pom.xml index 96844937f4..2458788f69 100644 --- a/epmet-module/epmet-job/epmet-job-server/pom.xml +++ b/epmet-module/epmet-job/epmet-job-server/pom.xml @@ -130,11 +130,14 @@ false + + https://oapi.dingtalk.com/robot/send?access_token=90782b119f82a5b6bb8e0f819b6a77bbc2102b53aa2d7d2e24fa10b66d580b1c + SEC080aac67ff78e79fdaba132aa51e3fb3f6060dec99492feaac82cabf9f8b6a19 test - @@ -162,6 +165,9 @@ true + + https://oapi.dingtalk.com/robot/send?access_token=a5f66c3374b1642fe2142dbf56d5997e280172d4e8f2b546c9423a68c82ece6c + SEC95f4f40b533ad379ea6a6d1af6dd37029383cfe1b7cd96dfac2678be2c1c3ed1 diff --git a/epmet-module/epmet-job/epmet-job-server/src/main/resources/bootstrap.yml b/epmet-module/epmet-job/epmet-job-server/src/main/resources/bootstrap.yml index e7ace5304a..b8faf3661a 100644 --- a/epmet-module/epmet-job/epmet-job-server/src/main/resources/bootstrap.yml +++ b/epmet-module/epmet-job/epmet-job-server/src/main/resources/bootstrap.yml @@ -113,3 +113,8 @@ hystrix: ribbon: ReadTimeout: 300000 ConnectTimeout: 300000 + +dingTalk: + robot: + webHook: @dingTalk.robot.webHook@ + secret: @dingTalk.robot.secret@ diff --git a/epmet-module/epmet-job/epmet-job-server/src/main/resources/logback-spring.xml b/epmet-module/epmet-job/epmet-job-server/src/main/resources/logback-spring.xml index ba7a073bbe..9f63b9901f 100644 --- a/epmet-module/epmet-job/epmet-job-server/src/main/resources/logback-spring.xml +++ b/epmet-module/epmet-job/epmet-job-server/src/main/resources/logback-spring.xml @@ -5,6 +5,9 @@ + + + ${appname} @@ -129,8 +132,9 @@ ERROR ACCEPT DENY - https://oapi.dingtalk.com/robot/send?access_token=90782b119f82a5b6bb8e0f819b6a77bbc2102b53aa2d7d2e24fa10b66d580b1c - SEC080aac67ff78e79fdaba132aa51e3fb3f6060dec99492feaac82cabf9f8b6a19 + ${activeEnv} + ${webHook} + ${secret}