xxl-job

传统定时任务

定时任务的问题

请求量大的情况下,独立的job服务器宕机之后如何处理未完成的任务

分布式集群的情况下,怎么保证定时任务不被重复执行

分布式定时任务解决方案

架构

202031144254

工作原理

批注 2020-03-01 153716

执行器

<dependency>    <groupId>com.xuxueli</groupId>    <artifactId>xxl-job-core</artifactId>    <version>2.1.2</version></dependency>
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-adminxxl.job.executor.appname=xxl-job-executor-samplexxl.job.executor.ip=xxl.job.executor.port=7777### xxl-job, access tokenxxl.job.accessToken=### xxl-job log pathxxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler### xxl-job log retention daysxxl.job.executor.logretentiondays=30
@Configurationpublic class XxlJobConfig {    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);    @Value("${xxl.job.admin.addresses}")    private String adminAddresses;    @Value("${xxl.job.executor.appname}")    private String appName;    @Value("${xxl.job.executor.ip}")    private String ip;    @Value("${xxl.job.executor.port}")    private int port;    @Value("${xxl.job.accessToken}")    private String accessToken;    @Value("${xxl.job.executor.logpath}")    private String logPath;    @Value("${xxl.job.executor.logretentiondays}")    private int logRetentionDays;    @Bean    public XxlJobSpringExecutor xxlJobExecutor() {        logger.info(">>>>>>>>>>> xxl-job config init.");        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);        xxlJobSpringExecutor.setAppName(appName);        xxlJobSpringExecutor.setIp(ip);        xxlJobSpringExecutor.setPort(port);        xxlJobSpringExecutor.setAccessToken(accessToken);        xxlJobSpringExecutor.setLogPath(logPath);        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);        return xxlJobSpringExecutor;    }}
@XxlJob("demoJobHandler")public ReturnT<String> demoJobHandler(String param) throws Exception {    XxlJobLogger.log("XXL-JOB, Hello World.");    for (int i = 0; i < 5; i++) {        XxlJobLogger.log("beat at:" + i);        TimeUnit.SECONDS.sleep(2);    }    return ReturnT.SUCCESS;}

可以配置多个同appname的执行器,来实现定时任务执行器负载均衡

路由策略

调度中心集群

需要注意的是,调度中心是一主多备的关系,只有一台调度中心进行调度任务

xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin,http://127.0.0.1:8079/xxl-job-admin