Browse Source

钉钉机器人消息改造

dev
jianjun 5 years ago
parent
commit
cf0402bb51
  1. 44
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/enums/EnvEnum.java
  2. 10
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/filter/LogMsgSendFilter.java
  3. 73
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/DingdingMsgSender.java
  4. 8
      epmet-module/data-statistical/data-statistical-server/pom.xml
  5. 6
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java
  6. 7
      epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml
  7. 8
      epmet-module/data-statistical/data-statistical-server/src/main/resources/logback-spring.xml
  8. 8
      epmet-module/epmet-job/epmet-job-server/pom.xml
  9. 5
      epmet-module/epmet-job/epmet-job-server/src/main/resources/bootstrap.yml
  10. 8
      epmet-module/epmet-job/epmet-job-server/src/main/resources/logback-spring.xml

44
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;
}
}

10
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;
}

73
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<String> 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<String> sendPostByJSON(DingTalkTextMsg param) {
private Result<String> 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("&timestamp=" + 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<String> 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<String>().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<String>().ok(result);
}
public static void main(String[] args) {
for (int i = 0; i < 50; i++) {

8
epmet-module/data-statistical/data-statistical-server/pom.xml

@ -175,6 +175,10 @@
<thread.pool.max-pool-size>8</thread.pool.max-pool-size>
<thread.pool.queue-capacity>10</thread.pool.queue-capacity>
<thread.pool.keep-alive>30</thread.pool.keep-alive>
<!--钉钉 机器人地址-->
<dingTalk.robot.webHook>https://oapi.dingtalk.com/robot/send?access_token=90782b119f82a5b6bb8e0f819b6a77bbc2102b53aa2d7d2e24fa10b66d580b1c</dingTalk.robot.webHook>
<dingTalk.robot.secret>SEC080aac67ff78e79fdaba132aa51e3fb3f6060dec99492feaac82cabf9f8b6a19</dingTalk.robot.secret>
</properties>
</profile>
<profile>
@ -257,6 +261,10 @@
<thread.pool.max-pool-size>8</thread.pool.max-pool-size>
<thread.pool.queue-capacity>10</thread.pool.queue-capacity>
<thread.pool.keep-alive>30</thread.pool.keep-alive>
<!--钉钉 机器人地址-->
<dingTalk.robot.webHook>https://oapi.dingtalk.com/robot/send?access_token=a5f66c3374b1642fe2142dbf56d5997e280172d4e8f2b546c9423a68c82ece6c</dingTalk.robot.webHook>
<dingTalk.robot.secret>SEC95f4f40b533ad379ea6a6d1af6dd37029383cfe1b7cd96dfac2678be2c1c3ed1</dingTalk.robot.secret>
</properties>
</profile>
</profiles>

6
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")

7
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@
keepAlive: @thread.pool.keep-alive@
dingTalk:
robot:
webHook: @dingTalk.robot.webHook@
secret: @dingTalk.robot.secret@

8
epmet-module/data-statistical/data-statistical-server/src/main/resources/logback-spring.xml

@ -5,6 +5,9 @@
<property name="log.path" value="logs/data-statistical"/>
<springProperty scope="context" name="appname" source="spring.application.name"/>
<springProperty scope="context" name="activeEnv" source="spring.profiles.active"/>
<springProperty scope="context" name="webHook" source="dingTalk.robot.webHook"/>
<springProperty scope="context" name="secret" source="dingTalk.robot.secret"/>
<!-- 日志上下文名称 -->
<contextName>${appname}</contextName>
@ -130,8 +133,9 @@
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
<webHook>https://oapi.dingtalk.com/robot/send?access_token=90782b119f82a5b6bb8e0f819b6a77bbc2102b53aa2d7d2e24fa10b66d580b1c</webHook>
<secret>SEC080aac67ff78e79fdaba132aa51e3fb3f6060dec99492feaac82cabf9f8b6a19</secret>
<activeEnv>${activeEnv}</activeEnv>
<webHook>${webHook}</webHook>
<secret>${secret}</secret>
</filter>
</appender>

8
epmet-module/epmet-job/epmet-job-server/pom.xml

@ -130,11 +130,14 @@
<nacos.ip/>
<spring.flyway.enabled>false</spring.flyway.enabled>
<!--钉钉 机器人地址-->
<dingTalk.robot.webHook>https://oapi.dingtalk.com/robot/send?access_token=90782b119f82a5b6bb8e0f819b6a77bbc2102b53aa2d7d2e24fa10b66d580b1c</dingTalk.robot.webHook>
<dingTalk.robot.secret>SEC080aac67ff78e79fdaba132aa51e3fb3f6060dec99492feaac82cabf9f8b6a19</dingTalk.robot.secret>
</properties>
</profile>
<profile>
<id>test</id>
<!--<activation>
<!-- <activation>
<activeByDefault>true</activeByDefault>
</activation>-->
<properties>
@ -162,6 +165,9 @@
<nacos.ip/>
<spring.flyway.enabled>true</spring.flyway.enabled>
<!--钉钉 机器人地址-->
<dingTalk.robot.webHook>https://oapi.dingtalk.com/robot/send?access_token=a5f66c3374b1642fe2142dbf56d5997e280172d4e8f2b546c9423a68c82ece6c</dingTalk.robot.webHook>
<dingTalk.robot.secret>SEC95f4f40b533ad379ea6a6d1af6dd37029383cfe1b7cd96dfac2678be2c1c3ed1</dingTalk.robot.secret>
</properties>
</profile>
</profiles>

5
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@

8
epmet-module/epmet-job/epmet-job-server/src/main/resources/logback-spring.xml

@ -5,6 +5,9 @@
<property name="log.path" value="logs/job"/>
<springProperty scope="context" name="appname" source="spring.application.name"/>
<springProperty scope="context" name="activeEnv" source="spring.profiles.active"/>
<springProperty scope="context" name="webHook" source="dingTalk.robot.webHook"/>
<springProperty scope="context" name="secret" source="dingTalk.robot.secret"/>
<!-- 日志上下文名称 -->
<contextName>${appname}</contextName>
@ -129,8 +132,9 @@
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
<webHook>https://oapi.dingtalk.com/robot/send?access_token=90782b119f82a5b6bb8e0f819b6a77bbc2102b53aa2d7d2e24fa10b66d580b1c</webHook>
<secret>SEC080aac67ff78e79fdaba132aa51e3fb3f6060dec99492feaac82cabf9f8b6a19</secret>
<activeEnv>${activeEnv}</activeEnv>
<webHook>${webHook}</webHook>
<secret>${secret}</secret>
</filter>
</appender>

Loading…
Cancel
Save