目录

chen 的个人博客

VX:ZzzChChen
Phone:13403656751
Email:zxydczzs@gmail.com

X

分布式任务调度平台XXL-JOB

一、下载源码

github 地址:https://github.com/xuxueli/xxl-job/releases

gitee 地址:http://gitee.com/xuxueli0323/xxl-job/releases

文档地址:https://www.xuxueli.com/xxl-job/

二、初始化“调度数据库”

image.png

image.png

三、运行 xxl-job-admin

先修改 properties 文件中的数据库密码,改成自己的。

image.png

运行

image.png

image.png

四、打开管理后台

网址输入 http://localhost:8080/xxl-job-admin/

image.png

五、创建执行器

image.png

六、创建任务

image.png

七、项目中使用

  1. pom.xml 添加依赖

    1        <!-- https://mvnrepository.com/artifact/com.xuxueli/xxl-job-core -->
    2        <dependency>
    3            <groupId>com.xuxueli</groupId>
    4            <artifactId>xxl-job-core</artifactId>
    5            <version>2.3.0</version>
    6        </dependency>
    
  2. yml 文件添加配置

     1xxl:
     2  job:
     3    admin:
     4      addresses: http://127.0.0.1:8080/xxl-job-admin
     5    accessToken:
     6    executor:
     7      appname: study-executor
     8      address:
     9      ip: 127.0.0.1
    10      port: 9999
    11      logpath: E:/ws/xxl-job-2.3.0/data/applogs/xxl-job/jobhandler
    12      logretentiondays: 3000
    
  3. 创建配置文件

     1package com.zxy.demo.config;
     2
     3import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
     4import org.slf4j.Logger;
     5import org.slf4j.LoggerFactory;
     6import org.springframework.beans.factory.annotation.Value;
     7import org.springframework.context.annotation.Bean;
     8import org.springframework.context.annotation.Configuration;
     9
    10/**
    11 * xxl-job config
    12 *
    13 * @author xuxueli 2017-04-28
    14 */
    15@Configuration
    16public class XxlJobConfig {
    17    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
    18
    19    @Value("${xxl.job.admin.addresses}")
    20    private String adminAddresses;
    21
    22    @Value("${xxl.job.accessToken}")
    23    private String accessToken;
    24
    25    @Value("${xxl.job.executor.appname}")
    26    private String appname;
    27
    28    @Value("${xxl.job.executor.address}")
    29    private String address;
    30
    31    @Value("${xxl.job.executor.ip}")
    32    private String ip;
    33
    34    @Value("${xxl.job.executor.port}")
    35    private int port;
    36
    37    @Value("${xxl.job.executor.logpath}")
    38    private String logPath;
    39
    40    @Value("${xxl.job.executor.logretentiondays}")
    41    private int logRetentionDays;
    42
    43
    44    @Bean
    45    public XxlJobSpringExecutor xxlJobExecutor() {
    46        logger.info(">>>>>>>>>>> xxl-job config init.");
    47        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
    48        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
    49        xxlJobSpringExecutor.setAppname(appname);
    50        xxlJobSpringExecutor.setAddress(address);
    51        xxlJobSpringExecutor.setIp(ip);
    52        xxlJobSpringExecutor.setPort(port);
    53        xxlJobSpringExecutor.setAccessToken(accessToken);
    54        xxlJobSpringExecutor.setLogPath(logPath);
    55        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
    56
    57        return xxlJobSpringExecutor;
    58    }
    59
    60    /**
    61     * 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
    62     *
    63     *      1、引入依赖:
    64     *          <dependency>
    65     *             <groupId>org.springframework.cloud</groupId>
    66     *             <artifactId>spring-cloud-commons</artifactId>
    67     *             <version>${version}</version>
    68     *         </dependency>
    69     *
    70     *      2、配置文件,或者容器启动变量
    71     *          spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
    72     *
    73     *      3、获取IP
    74     *          String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
    75     */
    76
    77
    78}
    
  4. 创建任务,注意:@XxlJob 注解中的值需与自己创建的执行器名称一致

     1package com.zxy.demo.job;
     2
     3import com.xxl.job.core.biz.model.ReturnT;
     4import com.xxl.job.core.handler.annotation.XxlJob;
     5import org.springframework.stereotype.Component;
     6
     7/**
     8 * @description: study
     9 * 模块名称:
    10 * 说明:
    11 * 作者(@author): zxy
    12 * 创建日期: 2022年3月2日16:52:16
    13 */
    14
    15@Component
    16public class StudyTestJobHandler {
    17
    18    @XxlJob("studyTestJobHandler")
    19    public ReturnT<String> shardingJobHandler() {
    20
    21        System.out.println("定时任务执行。。。。。");
    22        return ReturnT.SUCCESS;
    23    }
    24}
    25
    

八、启动要执行的项目

image.png

九、执行任务

image.png

十、结果

image.png


标题:分布式任务调度平台XXL-JOB
作者:zzzzchen
地址:https://www.dczzs.com/articles/2022/03/02/1646213068627.html