1. 环境准备
Centos7.9
java 1.8
maven 3.0.5
xxl-job-admin 2.3.0
yum install java-1.8.0-openjdk java-1.8.0-openjdk-devel -y
yum install maven -y
# vim /etc/maven/settings.xml
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
下载安装
git clone https://gitee.com/xuxueli0323/xxl-job.git
cd xxl-job/
git checkout 2.3.0
# 初始数据库
xxl-job/doc/db/tables_xxl_job.sql
2. 修改配置
2.1 application.properties配置
path: xxl-job-admin/src/main/resources/application.properties
### web
server.port=9090
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://x.x.x.x:3306/xxl_job_dev?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=xxljob_db_user
spring.datasource.password=XXLjob_db_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
### xxl-job, access token
xxl.job.accessToken=
# 增加钉钉配置信息
alarm.baseDingWebhook=https://oapi.dingtalk.com/robot/send?
2.2 添加DingDing通知文件
图片.png 图片.png原生的xxl-job并没有钉钉报警功能,但是官方文档有写:可以很方便的集成钉钉报警功能,即新增一个实现 "com.xxl.job.admin.core.alarm.JobAlarm" 接口的告警实现即可
DingJobAlarm.java
路径:xxl-job-admin/src/main/java/com/xxl/job/admin/core/alarm/impl/DingJobAlarm.java
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 com.xxl.job.admin.core.scheduler.XxlJobScheduler;
import com.xxl.job.core.biz.ExecutorBiz;
import com.xxl.job.core.biz.model.LogParam;
import com.xxl.job.core.biz.model.LogResult;
import com.xxl.job.core.biz.model.ReturnT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
private final RestTemplate restTemplate = new 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){
// 如果不包含access_token,则直接退出,调用email告警任务
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<>();
ExecutorBiz executorBiz = null;
try {
executorBiz = XxlJobScheduler.getExecutorBiz(jobLog.getExecutorAddress());
} catch (Exception e) {
e.printStackTrace();
}
// 获取执行日志信息
ReturnT<LogResult> logResult = executorBiz.log(new LogParam(jobLog.getTriggerTime().getTime(), jobLog.getId(), 1));
String errorMsg = logResult.getContent().getLogContent();
// 消息类型
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. 编译运行xxl-job-admin
编译
cd xxl-job-admin
mvn -B -Dmaven.test.skip=true clean package
运行
mkdir /opt/xxl-job-2.3.0
cp xxl-job-admin/target/xxl-job-admin-2.3.0.jar /opt/xxl-job-2.3.0
cd /opt/xxl-job-2.3.0
nohup java -jar xxl-job-admin-2.3.0.jar &
访问
# 浏览器问:
http://localhost:9090/xxl-job-admin/
#默认用户名与口令:
admin/123456
4. 钉钉群创建自定义机器人
图片.png安全设置自定义关键字
图片.png5. 页面配置通知
图片.png负责人格式为 姓名-手机号 即 李四-151XXXX1234
报警邮件为 access_token=26c54 机器人的地址
6. 报警展示
图片.png参考:
https://www.xuxueli.com/xxl-job/
https://blog.csdn.net/qq_31021213/article/details/125763157
https://blog.csdn.net/IT_world_/article/details/120785289
https://blog.csdn.net/xiaogang_1994/article/details/117284620
https://segmentfault.com/a/1190000039236981
网友评论