Pārlūkot izejas kodu

feat: 添加短信提醒

许家凯 4 gadi atpakaļ
vecāks
revīzija
014951650f

+ 10 - 0
pom.xml

@@ -19,6 +19,16 @@
 
     <dependencies>
         <dependency>
+            <groupId>com.aliyun.openservices</groupId>
+            <artifactId>ons-client</artifactId>
+            <version>1.8.4.Final</version>
+        </dependency>
+        <dependency>
+            <groupId>com.aliyun.mq</groupId>
+            <artifactId>mq-http-sdk</artifactId>
+            <version>1.0.0</version>
+        </dependency>
+        <dependency>
             <groupId>com.aliyun.odps</groupId>
             <artifactId>odps-sdk-core</artifactId>
             <version>${sdk.version}</version>

+ 1 - 0
src/main/java/com/winhc/dataworks/flow/touch/Main.java

@@ -149,6 +149,7 @@ public class Main {
                     }
                 }
             }
+            System.exit(0);
         } catch (ParseException e) {
             HelpFormatter formatter = new HelpFormatter();
             formatter.printHelp("很棒,参数错误", options);

+ 21 - 6
src/main/java/com/winhc/dataworks/flow/touch/QueryStatus.java

@@ -6,6 +6,7 @@ import com.helospark.lightdi.annotation.Autowired;
 import com.helospark.lightdi.annotation.Service;
 import com.winhc.dataworks.flow.touch.service.QueryService;
 import com.winhc.dataworks.flow.touch.utils.DingUtils;
+import com.winhc.dataworks.flow.touch.utils.SmsUtils;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 
@@ -27,6 +28,13 @@ public class QueryStatus {
         bean.waitTaskDone();
     }
 
+    private static final String[] to = new String[]{
+            "18538736690"
+            , "17602140784"
+            , "13918612566"
+            , "18866771835"
+    };
+
 
     @Autowired
     private QueryService queryService;
@@ -34,6 +42,9 @@ public class QueryStatus {
     @Autowired
     private DingUtils dingUtils;
 
+    @Autowired
+    private SmsUtils smsUtils;
+
     @SneakyThrows
     public void waitTaskDone() {
         boolean flag = false;
@@ -45,23 +56,27 @@ public class QueryStatus {
         while (!queryService.isSuccess()) {
             log.warn("等待上游数据同步...");
 
+            if (LocalTime.now().isAfter(exitTime)) {
+                log.error("数据同步任务长时间未完成,程序退出!");
+                smsUtils.send("ERROR,数据同步任务长时间未完成,数据处理程序退出!【odps数据同步】", to);
+                dingUtils.send("数据同步任务长时间未完成,程序退出!", "");
+                System.exit(-99);
+            }
+
             if (LocalTime.now().isAfter(sendTime) && !flag) {
                 flag = true;
                 log.error("上游数据同步任务长时间阻塞,请及时排查!");
-                dingUtils.send("上游数据同步任务长时间阻塞,请及时排查!");
+                smsUtils.send("WARNING,上游数据同步任务长时间阻塞,请及时排查!【odps数据同步】", to);
+                dingUtils.send("上游数据同步任务长时间阻塞,请及时排查!", "");
             }
 
-            if (LocalTime.now().isAfter(exitTime)) {
-                log.error("数据同步任务长时间未完成,程序退出!");
-                dingUtils.send("数据同步任务长时间未完成,程序退出!");
-                System.exit(-99);
-            }
             Thread.sleep(120000);
         }
         long between = ChronoUnit.MINUTES.between(startTime, LocalTime.now());
         String msg = "上游数据同步任务已经完成!累计阻塞" + between + "分钟";
         dingUtils.send(msg);
         log.info(msg);
+        System.exit(0);
     }
 
 }

+ 26 - 0
src/main/java/com/winhc/dataworks/flow/touch/bean/SmsBean.java

@@ -0,0 +1,26 @@
+package com.winhc.dataworks.flow.touch.bean;
+
+import com.winhc.dataworks.flow.touch.utils.JsonUtils;
+import lombok.Getter;
+
+import java.io.Serializable;
+
+/**
+ * @author: XuJiakai
+ * 2020/10/30 17:07
+ */
+@Getter
+public class SmsBean implements Serializable {
+    public SmsBean(String[] mobileNo, String content) {
+        this.mobileNo = mobileNo;
+        this.content = content;
+    }
+
+    private String[] mobileNo;
+    private String content;
+
+    @Override
+    public String toString() {
+        return JsonUtils.jsonObjToString(this);
+    }
+}

+ 34 - 0
src/main/java/com/winhc/dataworks/flow/touch/configuration/RocketMqConfiguration.java

@@ -0,0 +1,34 @@
+package com.winhc.dataworks.flow.touch.configuration;
+
+import com.aliyun.openservices.ons.api.ONSFactory;
+import com.aliyun.openservices.ons.api.Producer;
+import com.aliyun.openservices.ons.api.PropertyKeyConst;
+
+import java.util.Properties;
+
+/**
+ * @author: XuJiakai
+ * 2020/10/30 17:13
+ */
+//@Configuration
+public class RocketMqConfiguration {
+    public static final String ACCESS_KEY = "LTAIHAdBYQjCFHki";
+    public static final String SECRET_KEY = "sVj8EYkyKRYqdwAF0oY5GzCcTt9h3G";
+    public static final String NAMESRV_ADDR = "http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet";
+
+
+//    @Bean
+    public Producer bean() {
+        Properties producerProperties = new Properties();
+        producerProperties.setProperty(PropertyKeyConst.AccessKey, ACCESS_KEY);
+        producerProperties.setProperty(PropertyKeyConst.SecretKey, SECRET_KEY);
+        producerProperties.put(PropertyKeyConst.ONSAddr, NAMESRV_ADDR);
+        Producer producer = ONSFactory.createProducer(producerProperties);
+        producer.start();
+        return producer;
+    }
+
+
+
+
+}

+ 11 - 0
src/main/java/com/winhc/dataworks/flow/touch/utils/DingUtils.java

@@ -35,6 +35,17 @@ public class DingUtils {
         return sendByBody(getMdBody(msg.replace("\\", "\\\\")));
     }
 
+    public boolean send(String msg, String at) {
+        String a = " ,\"at\": {\"atMobiles\": [\n" +
+                "              \"" + at + "\"\n" +
+                "          ],\n" +
+                "          \"isAtAll\": true\n" +
+                "      }}";
+        String mdBody = getMdBody( msg.replace("\\", "\\\\"));
+        mdBody = mdBody.substring(0, mdBody.length() - 1) + a;
+        return sendByBody(mdBody);
+    }
+
     private boolean sendByBody(String body) {
         Entry<Long, String> sign = getSign();
         String query = "&timestamp=" + sign.getKey() + "&sign=" + sign.getValue();

+ 32 - 0
src/main/java/com/winhc/dataworks/flow/touch/utils/SmsUtils.java

@@ -0,0 +1,32 @@
+package com.winhc.dataworks.flow.touch.utils;
+
+import com.helospark.lightdi.annotation.Autowired;
+import com.helospark.lightdi.annotation.Component;
+import com.winhc.dataworks.flow.touch.bean.SmsBean;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * @author: XuJiakai
+ * 2020/10/30 16:50
+ */
+@Slf4j
+@Component
+public class SmsUtils {
+    @Autowired
+    private OkHttpUtils client;
+
+    public boolean send(String msg, String to) {
+        return send(msg, new String[]{to});
+    }
+
+    public boolean send(String msg, String[] to) {
+        try {
+            String post = client.post("http://47.101.221.131:8288/sms/send?sign=ansdvhhanvisdnhacamjklvrvrsjka", new SmsBean(to, msg).toString());
+            log.info(post);
+            return post.contains("true");
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+            return false;
+        }
+    }
+}