在spring boot中3分钟上手分布式任务调度系统xxl-job

xxl-job在鼎鼎大名的quartz任务调度系统上进行了二次封装,变得更好用了 项目地址:github.com/xuxueli/xxl… 官方文档:www.xuxueli.com/xxl-job/#/java

虽然官方文档也很全,很详细,但须要多花些时间上手,如下是我根据官方文档整理出的快速上手步骤,能够减小上手须要花费的时间和一些生产使用须要注意的点mysql

在mysql中执行如下sql tables_xxl_job.sqlgit

这个sql建立xxl-job的数据库和调度任务的表 记下mysql的地址,用户名和密码github

使用docker一键启动调度中心spring

docker run -d --rm \
    -e PARAMS="--spring.datasource.url=jdbc:mysql://你的mysql数据库ip:3306/xxl-job?Unicode=true&characterEncoding=UTF-8 --spring.datasource.username=你的mysql数据库用户名 --spring.datasource.password=你的mysql数据库密码" \
    -p 8680:8080 \
    --name xxl-job-admin xuxueli/xxl-job-admin:2.0.2
复制代码

在浏览器中使用默认用户名和密码 admin 123456 登录查看效果 sql

将一个spring boot项目变成一个xxl-job任务的执行器

在现有项目的pom.xml加上xxl-job core依赖docker

<!-- xxl-job-core -->
<dependency>
    <groupId>com.xuxueli</groupId>
    <artifactId>xxl-job-core</artifactId>
    <version>2.0.2</version>
</dependency>
复制代码

在现有的配置文件src/main/resources/application.properties文件中添加xxl-job调度中心的配置shell

### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://127.0.0.1:8680/xxl-job-admin

### xxl-job executor address
xxl.job.executor.appname=xxl-job-executor-sample
xxl.job.executor.ip=
xxl.job.executor.port=9999

### xxl-job, access token
xxl.job.accessToken=

### xxl-job log path
xxl.job.executor.logpath=logs/xxl-job/jobhandler
### xxl-job log retention days
xxl.job.executor.logretentiondays=-1
复制代码

而后在spring boot中把xxl job注册成服务,参考: XxlJobConfig.java 核心代码以下:数据库

@Bean(initMethod = "start", destroyMethod = "destroy")
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 boot了,通常都会在任务中调用spring boot项目的资源,好比 jpa, service, util等

在spring boot项目中建立一个测试任务,好比 TestJobHandler.java 核心代码以下

@JobHandler(value="testJobHandler")
@Component
public class TestJobHandler extends IJobHandler {

    private final InService inService;

    @Autowired
    public TestJobHandler(InService inService) {
        this.inService = inService;
    }

    @Override
    public ReturnT<String> execute(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);
        }

        inService.xxl();
        return SUCCESS;
    }
}
复制代码

其中inService即是spring boot中的一个普通的service

执行任务

而后在调度中心建立任务运行咱们刚才建立的任务

点击启动任务,在任务执行代码中断个点查看效果

任务触发成功!

同时能够在调度中心看到执行的记录和日志

一些注意的点

调度中心的JobHandler必须和代码里的JobHandler的值一致才能匹配到执行的任务

生产环节使用要保证任务执行客户端的高可用,开启高可用参考: 执行器集群

调度中心也须要高可用,参考:调度中心集群

通常微服务都在容器里部署,容器部署须要特别注意ip地址,参考:XxlJobConfig.java 的配置

相关文章
相关标签/搜索