关于springboot:SpringBoot-Admin-钉钉预警

32次阅读

共计 2794 个字符,预计需要花费 7 分钟才能阅读完成。

前言

????????????

 本次分享 SpringBoot Admin - 应用程序监控预警(钉钉机器人)

概述

 生产环境中微服务利用集群服务数量较大,为更快,更准确的在第一工夫监控到服务衰弱状态。应用 SpringBoot Admin 提供的监控告警性能实现钉钉机器人预警告诉。在 Springboot Admin 中内置多种开箱即用的监控告警零碎告诉,例如 Mail、Slack、Telegram、Hipchat 等多种告诉渠道。思考到它所反对的大多是一些国外的支流工具,而目前理论工作场景中大多应用钉钉办公,故本次分享钉钉机器人预警告诉。

疾速开始

  1. 创立钉钉机器人

    创立自定义机器人

    复制 WebHook – token,实现创立

  2. 编码 - 自定义音讯告诉

    1. 编码 – 钉钉告诉

      public class DingDingNotifier extends AbstractStatusChangeNotifier {public DingDingNotifier(InstanceRepository repository) {super(repository);
          }
          
          @Override
          protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {String serviceName = instance.getRegistration().getName();
              String serviceUrl = instance.getRegistration().getServiceUrl();
              String status = instance.getStatusInfo().getStatus();
              Map<String, Object> details = instance.getStatusInfo().getDetails();
              StringBuilder str = new StringBuilder();
              str.append("监控报警 :【" + serviceName + "】");
              str.append("【服务地址】" + serviceUrl);
              str.append("【状态】" + status);
              str.append("【详情】" + JSONObject.toJSONString(details));
              return Mono.fromRunnable(() -> {DingDingMessageUtil.sendTextMessage(str.toString());
              });
          }
      }
    2. 编码 – 钉钉音讯

      
      public class DingDingMessageUtil {
         
         public static String access_token = "TOKEN";
          
         public static void sendTextMessage(String msg) {
             try {Message message = new Message();
                 message.setMsgtype("text");
                 message.setText(new MessageInfo(msg));
                 URL url = new URL("https://oapi.dingtalk.com/robot/send?access_token=" + access_token);
                 // 建设 http 连贯
                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                 conn.setDoOutput(true);
                 conn.setDoInput(true);
                 conn.setUseCaches(false);
                 conn.setRequestMethod("POST");
                 conn.setRequestProperty("Charset", "UTF-8");
                 conn.setRequestProperty("Content-Type", "application/Json; charset=UTF-8");
                 conn.connect();
                 OutputStream out = conn.getOutputStream();
                 String textMessage = JSONObject.toJSONString(message);
                 byte[] data = textMessage.getBytes();
                 out.write(data);
                 out.flush();
                 out.close();
                 InputStream in = conn.getInputStream();
                 byte[] data1 = new byte[in.available()];
                 in.read(data1);
                 } catch (Exception e) {e.printStackTrace();
                 }
            }
        }
        class Message {
           private String msgtype;
           private MessageInfo text;
           public String getMsgtype() {return msgtype;}
           public void setMsgtype(String msgtype) {this.msgtype = msgtype;}
           public MessageInfo getText() {return text;}
           public void setText(MessageInfo text) {this.text = text;}
       }
       class MessageInfo {
          private String content;
          public MessageInfo(String content) {this.content = content;}
          public String getContent() {return content;}
          public void setContent(String content) {this.content = content;}
      }
    3. 编码 – Application 初始化钉钉告诉

      @EnableAdminServer
      @EnableDiscoveryClient
      @SpringBootApplication
      public class AdminApplication {public static void main(String[] args) {SpringApplication.run(AdminApplication.class, args);
          }
          @Bean
          public DingDingNotifier dingDingNotifier(InstanceRepository repository) {return new DingDingNotifier(repository);
          }
      }
  3. 成绩验证

    • 启动 SpringBoot Admin Server,启动敞开 Client 服务

结束语

  • 以上就是 SpringBoot Admin – 钉钉预警的示例,另反对多种监控告警告诉,例如 Mail 形式,可自行实际理解。

✔ END

© ???????? ???? ????????????

正文完
 0