目前JOB的调度框架非常多,由于之前一直在用XXL-JOB这个轻量级的调度框架,所以这么多年也一直用,不过对于JOB调度来说,我们很多时候需要对JOB的执行情况进行监控,框架默认带了邮件的报警,不过由于使用习惯的问题,邮件报警不太习惯,所以这里改造为通过钉钉的机器人进行告警推送。
Step1、创建钉钉的群的机器人,这里就不截图了,这里选择的验证方式是签名验证(不同的方式改动会略有差异)
Step2、将相关配置写入到配置文件
找到xxl-job-admin的application.properties,在最下面增加配置:
```
dingding.accesstoken=你的钉钉机器人的token
dingding.key=机器人的SECkey,开头是“SEC”
dingding.url=https://oapi.dingtalk.com/robot/send?access_token=
```
Step3、读取application.properties的配置
找到com.xxl.job.admin.core.conf.XxlJobAdminConfig文件,读取出刚才的配置
Step4、在JobAlarmer增加钉钉机器人告警的方法
```
/**
* job alarm 通过钉钉机器人推送job告警
*
* @param info
* @param jobLog
* @return
*/
public boolean DingRobotalarm(XxlJobInfo info, XxlJobLog jobLog) {
boolean result =false;
if (jobAlarmList!=null &&jobAlarmList.size()>0) {
result =true; // success means all-success
for (JobAlarm alarm:jobAlarmList) {
boolean resultItem =false;
try {
DDMsg ddMsg=new DDMsg();
resultItem = ddMsg.PushRobotMsg(info, jobLog); //发送告警信息
}catch (Exception e) {
logger.error(e.getMessage(), e);
}
if (!resultItem) {
result =false;
}
}
}
return result;
}
```
Step5、增加DDMsg.java 类
```
package com.xxl.job.admin.core.alarm;
import com.xxl.job.admin.core.conf.XxlJobAdminConfig;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.core.model.XxlJobLog;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import com.xxl.job.admin.core.util.HttpRequest;
import org.apache.tomcat.util.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* @PROJECT xxl-job
* @ClassName DDMsg
* @Author 唐尘
* @date 2021/3/23
* @time 11:56
* @Description: TODO
* @return
*/
@Configuration
@Component
//@PropertySource({"classpath:application.properties"})
public class DDMsg {
private static Loggerlogger = LoggerFactory.getLogger(DDMsg.class);
public boolean PushRobotMsg(XxlJobInfo info, XxlJobLog jobLog)throws Exception {
String dingdingaccesstoken=XxlJobAdminConfig.getAdminConfig().getDingdingaccesstoken();
String url=XxlJobAdminConfig.getAdminConfig().getDingdingurl();
// 获取logid
String logid = String.valueOf(jobLog.getId());
// todo 根据id读取错误日志 ,这里后续再完善
Long timestamp = System.currentTimeMillis();
String signres =sign(timestamp);
String DingDingRobotUrl = url + dingdingaccesstoken +"×tamp=" + timestamp +"&sign=" + signres;
String msgbody ="任务ID:" + jobLog.getJobId() +",任务描述:" + info.getJobDesc() +"执行出错,请及时处理!LogId:" + jobLog.getId() +",出错信息:" + jobLog.getHandleMsg();
String msg ="{\"msgtype\": \"text\",\"text\": {\"content\": \"" + msgbody +"\"}}";
logger.info(msgbody);
try {
HttpRequest.postBody(DingDingRobotUrl, msg);
return true;
}catch (Exception e){
return false;
}
}
public static Stringsign(Long timestamp)throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
String key=XxlJobAdminConfig.getAdminConfig().getDingdingkey();
String stringToSign = timestamp +"\n" + key;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
return sign;
}
}
```
Step6、修改默认的钉钉的告警方法
找到类文件JobFailMonitorHelper.java,修改如下:
然后重新构建发布即可