1.首先在钉钉群创建自定义机器人
安全设置自定义关键字
2.xxl-job中编写钉钉报警功能的实现类在com.xxl.job.admin.core.alarm.impl下
package com.xxl.job.admin.core.alarm.impl;
import com.xxl.job.admin.core.alarm.JobAlarm;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.core.model.XxlJobLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 钉钉告警
*
* @author
*/
@Component
public class DingJobAlarm implements JobAlarm {
private static Logger logger = LoggerFactory.getLogger(DingJobAlarm.class);
@Value("${alarm.baseDingWebhook}")
private String baseDingWebhook;
@Autowired
private RestTemplate restTemplate;
/**
* fail alarm
*
* @param info
* @param jobLog
* @return
*/
@Override
public boolean doAlarm(XxlJobInfo info, XxlJobLog jobLog) {
boolean alarmResult = true;
logger.info("ding----------------");
if(info!=null && info.getAlarmEmail()!=null && info.getAlarmEmail().trim().length()>0){
if (!info.getAlarmEmail().contains("access_token")){
return alarmResult;
}
// 读取Webhook
Set<String> dingWebhookSet = new HashSet<String>(Arrays.asList(info.getAlarmEmail().split(",")));
Map<String, Object> map = loadEmailJobAlarmTemplate(info,jobLog);
// 发送钉钉消息
for (String dingWebhook: dingWebhookSet) {
try {
restTemplate.postForEntity(baseDingWebhook + dingWebhook,map,Object.class);
} catch (Exception e) {
logger.error(">>>>>>>>>>> xxl-job, job fail alarm email send error, JobLogId:{}", jobLog.getId(), e);
alarmResult = false;
}
}
}
return alarmResult;
}
/**
* 封装消息内容
* @param info
* @param jobLog
* @return
*/
private static Map<String, Object> loadEmailJobAlarmTemplate(XxlJobInfo info, XxlJobLog jobLog) {
HashMap<String, Object> requestMap = new HashMap<>();
// 消息类型
requestMap.put("msgtype", "text");
// 消息内容
String content = "【告警信息】 \t\n" +
"负责人 : \t" + info.getAuthor() + "\t\n" +
"任务id : \t" + info.getId() + "\t\n" +
"任务名称 : \t" + info.getJobDesc() + "\t\n" +
"执行器名称 : \t" + info.getExecutorHandler() + "\t\n" +
"执行器ip : \t" + jobLog.getExecutorAddress() + "\t\n" +
"任务参数 : \t" + jobLog.getExecutorParam() + "\t\n" +
"LogId : \t" + jobLog.getId() + "\t\n" +
"TriggerMsg : \t" + jobLog.getTriggerMsg().replace("<br>","\n") + "\t\n" +
"HandleCode : \t" + jobLog.getHandleMsg() + "\t\n" +
"报警时间 : \t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\t\n";
HashMap<String, Object> map = new HashMap<>();
map.put("content",content);
requestMap.put("text", map);
// 设置是否@指定人
// Map<String, Object> atmap = new HashMap<String, Object>();
// String[] authorList = info.getAuthor().split(",");
// ArrayList arrayList = new ArrayList();
// for (String author : authorList){
// if ("".equals(author) || author.split("-").length<2){
// continue;
// }
// arrayList.add(author.split("-")[1]);
// }
// if (arrayList.size() > 0){
// atmap.put("atMobiles",arrayList.toArray());
// requestMap.put("at",atmap);
// }
return requestMap;
}
}
3.在新建任务是将群机器人的access_token写入报警邮件中
参考文章:https://blog.csdn.net/xiaogang_1994/article/details/117284620?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.no_search_link