1.源码下载地址:
https://github.com/xuxueli/xxl-job.git;
2.将源码clone到IDEA中:
3.初始化数据库脚本: /doc/db/tables_xxl_job.sql ;
4.xxl-job-admin的配置文件application.properties:
### web ### 1、调度中心项目的端口号以及访问路径 server.port=8081 server.servlet.context-path=/xxl-job-admin ### actuator management.server.servlet.context-path=/actuator management.health.mail.enabled=false ### resources spring.mvc.servlet.load-on-startup=0 spring.mvc.static-path-pattern=/static/** spring.resources.static-locations=classpath:/static/ ### freemarker spring.freemarker.templateLoaderPath=classpath:/templates/ spring.freemarker.suffix=.ftl spring.freemarker.charset=UTF-8 spring.freemarker.request-context-attribute=request spring.freemarker.settings.number_format=0.########## ### mybatis mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml #mybatis.type-aliases-package=com.xxl.job.admin.core.model ### xxl-job, datasource ### 5、配置数据库的地址 spring.datasource.url=jdbc:mysql://10.0.204.214:8080/b2b_provider?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai spring.datasource.username=dev spring.datasource.password=D@_uat_0715 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ### datasource-pool spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.hikari.minimum-idle=10 spring.datasource.hikari.maximum-pool-size=30 spring.datasource.hikari.auto-commit=true spring.datasource.hikari.idle-timeout=30000 spring.datasource.hikari.pool-name=HikariCP spring.datasource.hikari.max-lifetime=900000 spring.datasource.hikari.connection-timeout=10000 spring.datasource.hikari.connection-test-query=SELECT 1 ### xxl-job, email spring.mail.host=smtp.qq.com spring.mail.port=25 spring.mail.username=xxx@qq.com spring.mail.from=xxx@qq.com spring.mail.password=xxx spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory ### xxl-job, access token xxl.job.accessToken= ### xxl-job, i18n (default is zh_CN, and you can choose "zh_CN", "zh_TC" and "en") xxl.job.i18n=zh_CN ## xxl-job, triggerpool max size xxl.job.triggerpool.fast.max=200 xxl.job.triggerpool.slow.max=100 ### xxl-job, log retention days xxl.job.logretentiondays=30
5.部署调度中心
该工程是一个springboot项目,我们只需要在IDEA中执行 XxlJobAdminApplication 类即可运行该工程;
6.在浏览器输入 http://localhost:8080/xxl-job-admin 然后输入用户名和密码(admin/123456);
二、创建自己项目的执行器:
1.引入jar包:
<dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>2.1.2</version> </dependency>
注:如果不是maven项目,则需要在idea中运行maven-》install 打出jar包,将jar包引入;
2.把xxl-job执行器的xxl-job-executor.properties文件copy至项目的resources文件夹下
3.配置xxl-job-executor.properties文件
# 调度中心地址 xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin #执行器"AppName"和地址信息配置:AppName执行器心跳注册分组依据;地址信息用于"调度中心请求并触发任务"和"执行器注册"。执行器默认端口为9999,执行器IP默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用。单机部署多个执行器时,注意要配置不同执行器端口; xxl.job.executor.appname=task-demo02 xxl.job.executor.ip= xxl.job.executor.port=9998 #执行器通讯TOKEN,非空时启用 xxl.job.accessToken= #xxl-job log path执行器运行日志文件存储的磁盘位置,需要对该路径拥有读写权限 xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler #xxl-job log retention days执行器Log文件定期清理功能,指定日志保存天数,日志文件过期自动删除。限制至少保持3天,否则功能不生效; xxl.job.executor.logretentiondays=30
4.配置applicationContext.xml文件
5.载入配置文件,在项目中创建 XxlJobConfig.class 文件:
package com.taskDemo.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.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; } /** * 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP; * * 1、引入依赖: * <dependency> * <groupId>org.springframework.cloud</groupId> * <artifactId>spring-cloud-commons</artifactId> * <version>${version}</version> * </dependency> * * 2、配置文件,或者容器启动变量 * spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.' * * 3、获取IP * String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); */ }
6.创建任务JobHandler
package com.taskDemo.service.jobhandler; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.log.XxlJobLogger; import org.springframework.stereotype.Component; /** * @Description 调度任务类 * @Date Created in 下午8:19 2020/2/15 * @Version 1.0 */ @Component public class TaskHandler extends IJobHandler { @XxlJob("taskDemoHandler") @Override public ReturnT<String> execute(String param) throws Exception { XxlJobLogger.log("接受参数" + param +" 执行任务"); return SUCCESS; } }
7.在调度任务中心配置执行器
点击执行器管理-->新增执行器 注意AppName为执行器项目应用名,即上面参数xxl.job.executor.appname=task-demo02
8.创建任务
点击任务管理-->新增 执行器下拉列表选择刚新增的执行器 注意JobHandler为上面调度任务类@JobHandler(value="taskDemoHandler")中value值