侧边栏壁纸
博主头像
程彬彬博主等级

业精于勤 荒于嬉 行成于思 毁于随

  • 累计撰写 101 篇文章
  • 累计创建 26 个标签
  • 累计收到 20 条评论

目 录CONTENT

文章目录

skywalking告警对接钉钉

程彬彬
2019-12-26 / 0 评论 / 1 点赞 / 5,097 阅读 / 0 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2019-12-26,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
广告 广告

一.获取自定义机器人webhook

参考官网文档一步步申请webhook

钉钉开发文档

配置一个WebHook地址 https://oapi.dingtalk.com/robot/send?access_token=********** image.png

二.引入钉钉jar

服务端API SDK下载 taobao-sdk-java-auto_1479188381469-20191225.jar

在resources下创建lib 导入taobao-sdk-java-auto_1479188381469-20191225.jar包

pom.xml 修改

<dependency>
    <groupId>com.dingtalk</groupId>
    <artifactId>com.dingtalk</artifactId>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/lib/taobao-sdk-java-auto_1479188381469-20191225.jar</systemPath>
</dependency>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <includeSystemScope>true</includeSystemScope>
            </configuration>
        </plugin>
    </plugins>
</build>

三.接口调用

@RequestMapping("/alarm")
@RestController
@Slf4j
public class AlarmController {


    private String secret = "密钥,机器人安全设置页面,加签一栏下面显示的SEC开头的字符串";


    private String webhook = "完成安全设置后,复制出机器人的Webhook地址";


    @RequestMapping(value = "/pushData", method = RequestMethod.POST)
    public void alarm(@RequestBody List<AlarmMessageDto> alarmMessageList) {
        log.info("alarmMessage:{}", alarmMessageList.toString());


        alarmMessageList.forEach(info -> {
            try {
                Long timestamp = System.currentTimeMillis();


                String stringToSign = timestamp + "\n" + secret;
                Mac mac = Mac.getInstance("HmacSHA256");
                mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
                byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
                String sign = "&timestamp=" + timestamp + "&sign=" + URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");


                DingTalkClient client = new DefaultDingTalkClient(webhook + sign);
                OapiRobotSendRequest request = new OapiRobotSendRequest();
                request.setMsgtype("text");
                OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
                text.setContent("业务告警:\n" + info.getAlarmMessage());
                request.setText(text);


                OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
                at.setAtMobiles(Arrays.asList("所有人"));
                request.setAt(at);


                OapiRobotSendResponse response = client.execute(request);
                log.info("execute:{}" + response.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}

四.效果

image.png

五.代码

GitHub

1
广告 广告

评论区