xxl-job 入门使用

第1步:下载源码

源码下载地址:

https://github.com/xuxueli/xxl-job
https://gitee.com/xuxueli0323/xxl-job

这里版本选择:xxl-job-2.3.1.zip


第2步:初始化调度数据库

解压xxl-job-2.3.1.zip源码包,将解压后文件夹"xxl-job-2.3.1"更名为"xxl-job"

xxl-job\doc\db\tables_xxl_job.sql

在数据库执行脚本,初始化数据库

第3步:编译源码 - 导入IDE,保证源码不报错

将更名为的源码解压包"xxl-job"导入IDE,进行配置,然后编译打包

在pom中添加:

    <repositories>

        <!--( aliyun repository) -->
        <repository>
            <id>aliyun</id>
            <name>aliyun-repo</name>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
            </snapshots>
        </repository>
        <!-- central -->
        <repository>
            <id>central</id>
            <name>central-repo</name>
            <url>https://repo1.maven.org/maven2/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </releases>
            <snapshots>
                <!-- 由于snapshot快照版本不稳定,一般建议禁用:false -->
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>

        <repository>
            <id>io.spring.repo.maven.release</id>
            <url>https://repo.spring.io/release/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

        <repository>
            <id>io.spring.repo.maven.milestone</id>
            <url>https://repo.spring.io/milestone/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

    </repositories>


    <pluginRepositories>

        <pluginRepository>
            <id>central</id>
            <name>central repo</name>
            <url>https://repo1.maven.org/maven2/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>

        <pluginRepository>
            <id>aliyun-plugin</id>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>

    </pluginRepositories>


xxl-job-admin:调度中心
xxl-job-core:公共依赖
xxl-job-executor-samples:执行器Sample示例(选择合适的版本执行器,可直接使用,也可以参考其并将现有项目改造成执行器)
     |-- :xxl-job-executor-sample-springboot:Springboot版本,通过Springboot管理执行器,推荐这种方式;
     |-- :xxl-job-executor-sample-frameless:无框架版本;


第4步:调度中心(xxl-job-admin)配置


修改xxl-job-admin项目的配置文件application.properties,主要是修改配置成自己或公司的"数据库账号密码"

调度中心配置内容说明:主要修改端口 与 数据库配置

### web
server.port=8080
server.servlet.context-path=/xxl-job-admin


### 调度中心JDBC链接:链接地址请保持和 2.1章节 所创建的调度数据库的地址一致
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root_pwd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


第5步:部署调度中心(xxl-job-admin)


运行xxl-job-admin服务下的:com.xxl.job.admin.XxlJobAdminApplication

访问调度中心页面:http://localhost:8080/xxl-job-admin/
默认登录账号 “admin/123456”


---------------------------------------------------------------------------------------------------------

【配置部署 - "执行器项目" - 即各个具体应用服务中整合xxl-job】

官网说明:
“执行器”项目:xxl-job-executor-sample-springboot (提供多种版本执行器供选择,现以 springboot 版本为例,可直接使用,
也可以参考其并将现有项目改造成执行器)
作用:负责接收“调度中心”的调度并执行;可直接部署执行器,也可以将执行器集成到现有业务项目中。


1.参考官方示例demo(xxl-job-executor-sample-springboot),新建服务,如:xxl-job-biz-executor

# 导入基本依赖&xxl-job核心依赖包:

        <dependency>
            <groupId>com.xuxueli</groupId>
            <artifactId>xxl-job-core</artifactId>
            <version>2.3.1</version>
        </dependency>
        
2.配置xxl-job-biz-executor: application.properties

# 添加/修改如下配置
server.port=8081
# no web
#spring.main.web-environment=false

# log config
logging.config=classpath:logback.xml

### 调度中心部署根地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。
# 执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
### 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


### 执行器通讯TOKEN [选填]:非空时启用;
### xxl-job, access token
xxl.job.accessToken=default_token

### 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册,示例:xxl-job-biz-executor
### xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-sample

### 执行器注册 [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址。
# 从而更灵活的支持容器类型执行器动态IP和动态映射端口问题。
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=

### 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;
# 地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
### xxl-job executor server-info
xxl.job.executor.ip=127.0.0.1
### 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
xxl.job.executor.port=9999
### 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
### xxl-job executor log-path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler

### 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30


3.编写配置类:XxlJobConfig


@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; // 执行器通讯TOKEN
    @Value("${xxl.job.executor.appname}")
    private String appname; // 执行器AppName
    @Value("${xxl.job.executor.address}")
    private String address; // 执行器注册 [选填]:优先使用该配置作为注册地址
    @Value("${xxl.job.executor.ip}")
    private String ip; // 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定I
    @Value("${xxl.job.executor.port}")
    private int port; // 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
    @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;
    }

    /**
     * 针对多网卡、容器内部署等情况,可借助 "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();
     */

}


4.编写任务处理类 - 实际的任务:具体的业务逻辑代码


@Component
public class SimpleXxlJob {
    @XxlJob("demoJobHandler")
    public void demoJobHandler() throws Exception {
        
        System.out.println("执行定时任务:[关闭过期未支付订单],执行时间:"+new Date());
    }
}

5.启动服务

6.在调度中心(xxl-job-admin)配置页面进行任务配置&触发执行


任务管理 -> 新增任务 -> 配置界面配置任务信息  -> 保存  -> 操作  -> 启动

核心配置-执行周期示例:0 0/2 * * * ?  - 表示每2分钟执行一次

7.观察任务执行情况,任务正常执行则应用整合 xxl-job 成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值