分布式 任务调度(XXL-JOB)
简介:XXL-JOB是一个分布式任务调度平台,优点是开发迅速、学习简单、轻量级、易扩展。
1. xxl-job
A. xxl-job-admin:调度中心,作用是统一管理任务调度平台上调度任务,负责触发调度执行,并且提供任务管理平台;
B. xxl-job-core:公共jar依赖;
C. xxl-job-executor-samples:执行器Sample示例;
D. 初始化SQL脚本位置:/xxl-job-master/doc/db/tables_xxl_job.sql;
E. 访问地址:http://localhost:8080/xxl-job-admin,默认账号密码 “admin/123456”。
2. 执行器项目
A. pom.xml Maven依赖
<dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>2.2.0</version> </dependency>
B. 配置文件
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin ### xxl-job, access token xxl.job.accessToken= ### xxl-job executor appname xxl.job.executor.appname=xxl-job-executor-sample ### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null xxl.job.executor.address= ### xxl-job executor server-info xxl.job.executor.ip= xxl.job.executor.port=9999 ### xxl-job executor log-path xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler ### xxl-job executor log-retention-days xxl.job.executor.logretentiondays=30
C. 配置类
package com.xxl.job.executor.core.config; import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * xxl-job config * * @author xuxueli 2017-04-28 */ @Configuration public class XxlJobConfig { private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class); @Value("${xxl.job.admin.addresses}") private String adminAddresses; @Value("${xxl.job.accessToken}") private String accessToken; @Value("${xxl.job.executor.appname}") private String appname; @Value("${xxl.job.executor.address}") private String address; @Value("${xxl.job.executor.ip}") private String ip; @Value("${xxl.job.executor.port}") private int port; @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.setAddress(address); xxlJobSpringExecutor.setIp(ip); xxlJobSpringExecutor.setPort(port); xxlJobSpringExecutor.setAccessToken(accessToken); xxlJobSpringExecutor.setLogPath(logPath); xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays); return xxlJobSpringExecutor; } }
3. 常见问题
A. 问题一:XXL-JOB连接数据库一段时间后:com.zaxxer.hikari.pool.PoolBase - HikariCP - Failed to validate connection org.mariadb.jdbc.MariaDbConnection@250027e1 ((conn=2838498) Connection reset). Possibly consider using a shorter maxLifetime value.
原因:数据库最大连接等待时间比xxl-job里maxLifetime大
解决办法:调小maxLifeTime值,如 --spring.datasource.hikari.max-lifetime=120000。
可参考:分布式任务调度平台XXL-JOB