• 猿创征文|Spring Boot 整合分布式调度框架:xxl-job


    01.前述

    定时任务需求

    02.调度中心服务部署(调度中心)

    1.下载项目:

    • 项目地址:https://gitee.com/xuxueli0323/xxl-job
    • 中文文档:https://www.xuxueli.com/xxl-job/

    2.项目结构如下:

    xxl-job-admin:调度中心
    xxl-job-core:公共依赖
    xxl-job-executor-samples:执行器Sample示例(选择合适的版本执行器,可直接使用,也可以参考其并将现有项目改造成执行器)
        :xxl-job-executor-sample-springboot:Springboot版本,通过Springboot管理执行器,推荐这种方式;
        :xxl-job-executor-sample-frameless:无框架版本;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    调度中心作用:统一管理任务调度平台上调度任务,负责触发调度执行,并且提供任务管理平台。
    
    • 1

    3.创建数据库:

    • 数据库脚本存放位置:/xxl-job/doc/db/tables_xxl_job.sql
      在这里插入图片描述

    4.修改 xxl-job-admin 项目配置文件:

    • 主要配置
      # 项目访问地址配置(我们也可以自由配置):localhost:8080/xxl-job-admin
      server.port=8080
      server.servlet.context-path=/xxl-job-admin
      # 主要修改数据库相关配置,其他先默认
      spring.datasource.url=jdbc:mysql://localhost:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
      spring.datasource.username=root
      spring.datasource.password=12345678
      spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
    • 选择配置
      # 报警邮箱
      spring.mail.host=smtp.qq.com
      spring.mail.port=25
      spring.mail.username=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
      
      ### 调度中心通讯TOKEN [选填]:非空时启用;
      xxl.job.accessToken=
      
      ### 调度中心国际化配置 [必填]: 默认为 "zh_CN"/中文简体, 可选范围为 "zh_CN"/中文简体, "zh_TC"/中文繁体 and "en"/英文;
      xxl.job.i18n=zh_CN
      
      ## 调度线程池最大线程配置【必填】
      xxl.job.triggerpool.fast.max=200
      xxl.job.triggerpool.slow.max=100
      
      ### 调度中心日志表数据保存天数 [必填]:过期日志自动清理;限制大于等于7时生效,否则, 如-1,关闭自动清理功能;
      xxl.job.logretentiondays=30
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22

    4.启动 xxl-job-admin 项目:在idea编译器,使用侧边栏的Maven工具进行打包

    • 项目打包:
      1.xxl-job父工程:Lifecycle——>clean
      2.xxl-job父工程先:Lifecycle——>install
      3.xxl-job-admin:Lifecycle——>install
      
      • 1
      • 2
      • 3
    • 打包成功以后,在xxl-job-admin/target文件夹下会出现一个jar包:
      xxl-job-core-2.4.0-SNAPSHOT.jar
      
      • 1

    5.项目部署

    • 在jar包所在目录打开终端执行下面的命令即可启动项目:

      java -jar xxl-job-core-2.4.0-SNAPSHOT.jar
      
      • 1

      在这里插入图片描述

    6.项目的访问:

    • 访问地址:http://localhost:8080/xxl-job-admin
    • 默认账户: admin
    • 密码:123456
      在这里插入图片描述

    03.Spring Boot 整合 xxl-job(执行器)

    执行器项目作用:负责接收调度中心的调度并执行,执行器也就是我们的项目(可以使用Springboot搭建)。

    1.依赖

    <dependency>
        <groupId>com.xuxueli</groupId>
        <artifactId>xxl-job-core</artifactId>
        <version>2.3.0</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.修改项目配置文件:添加xxl-job的配置

    # 调度中心部署根地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔,执行器将会使用该地址进行执行器心跳注册和任务结果回调,为空则关闭自动注册;
    xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
    
    # 执行器通讯TOKEN [选填]:非空时启用;
    xxl.job.accessToken=default_token
    
    # 执行器AppName(自定义后面会用) [选填]:执行器心跳注册分组依据;为空则关闭自动注册
    xxl.job.executor.appname=test-job
    # 执行器注册(端口号不要和服务端口重了) [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址。从而更灵活的支持容器类型执行器动态IP和动态映射端口问题。
    xxl.job.executor.address=http://localhost:8725
    # 执行器IP(一般为我们的项目地址) [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
    xxl.job.executor.ip=localhost
    # 执行器端口号(一般为我们的项目端口号) [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
    xxl.job.executor.port=8725
    # 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
    xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
    # 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
    xxl.job.executor.logretentiondays=30
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3.配置类

    @Configuration
    @ComponentScan(basePackages = "com.aismall.helloworld")
    public class XxlJobConfig {
        @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() {
            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;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    4.Job

    @Slf4j
    @Component
    // 2.0.1:使用下面的注解
    //@JobHandler(value = "testJob")
    public class ScheduledTask {
    
        /**
         * 每1分钟扫描一次,扫描到就执行一次
         */
        @XxlJob("testJob")
        public ReturnT<String> testJob() {
            log.info("测试Job执行成功");
            return ReturnT.SUCCESS;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    5.启动项目

    04.调度中心配置

    在调度中心配置执行器

    • 执行器管理 -> 新增 -> 录入执行器数据 -> 保存
      在这里插入图片描述
      在这里插入图片描述

    在调度中心新增任务

    • 任务管理 -> 新增 -> 录入任务数据 -> 保存
      在这里插入图片描述

    在调度中心上执行任务:

    • 开启任务(RUNNING)
      在这里插入图片描述

    执行结果:
    在这里插入图片描述

    05.总结

    Job的执行流程:

    • 任务调度中心——RPC——》执行器(执行模块:我们的服务)(appname)——》具体任务(@Jobhandler或者@XxxJob:版本不一样,注解使用的方式不同)

    调度中心之所以能调度到我们的服务是因为,我们在执行器服务中配置了调度中心的地址,调度中心中也配置了我们的服务地址。

    一个执行器中可以有多个任务,每个任务对应一个XxxJob。

  • 相关阅读:
    [附源码]SSM计算机毕业设计商场日常维修管理系统JAVA
    C++与C语言中的字符串
    如何做好建筑翻译呢
    腾讯在智能机器人领域的探索与研究(张正友)
    【C语言】2.数组与指针
    云原生|kubernetes|多集群管理之kubeconfig文件配置和使用(定义,使用方法,合并管理多集群)
    【Mysql】InnoDB 中的聚簇索引、二级索引、联合索引
    使用容器快速在阿里云 ECS 多节点上搭建 Citus 12.1 集群
    LM13丨形态量化-动量周期分析
    SVN创建分支与合并分支
  • 原文地址:https://blog.csdn.net/weixin_45583303/article/details/126568506