钉钉消息结合链路监控在项目中的应用

官方对接地址

https://developers.dingtalk.com/document/app/custom-robot-access

 

第一步:拉一个钉钉群,然后添加一个机器人,获取发送权限

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA3NzIyMzA=,size_16,color_FFFFFF,t_70

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA3NzIyMzA=,size_16,color_FFFFFF,t_70

添加完成后,会返回发送的地址链接:

https://oapi.dingtalk.com/robot/send?access_token=xxxx

 

注意点:发送内容必须含有自定义关键词,才能发送成功

20210328223113811.png

 

第二步:编写发送钉钉消息代码

注意点:content中必须含有  第一步设置的关键词  “告警” 两个字否则发送不成功。

@Component
@Slf4j
public class DingDingHelper {

    @Resource
    private RestTemplate restTemplate;
    @Autowired
    private EnvHelper envHelper;

    @Async
    public void sendAlert(String title, String context) {
        try {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("【类型】: ").append("某某项目业务告警").append("\n");
            stringBuffer.append("【标题】: ").append(title).append("\n");
            stringBuffer.append("【环境】: ").append(envHelper.getEnv()).append("\n");
            stringBuffer.append("【链路id】: ").append(TraceContext.traceId()).append("\n");
            stringBuffer.append("【内容】: ").append( context).append("\n");
            stringBuffer.append("【时间】: ").append(DateTimeUtils.toString(LocalDateTime.now())).append("\n");

            HashMap<String, Object> text = new HashMap<>(2);
            text.put("content", stringBuffer.toString());
            HashMap<String, Object> params = new HashMap<>(4);
            params.put("msgtype", "text");
            params.put("text", text);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            
            restTemplate.postForEntity("https://oapi.dingtalk.com/robot/send?access_token=xxx",
                        new HttpEntity<>(params, headers), Void.class);
         
        } catch (RestClientException e) {
            log.error("发送钉钉异常={}", e.getMessage());
        }
    }

}

 

将发送钉钉消息,结合项目和环境链路,可以在程序中打点告警,方便问题及时人工处理和问题的排查。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值