# SpringBoo集成xxl-job

# 1、xxl-job官网

https://www.xuxueli.com/xxl-job/ (opens new window)

# 2、部署

# 2.1 本地部署

# 1、拉取代码

GitHub仓库:https://github.com/xuxueli/xxl-job (opens new window) 下载压缩包,解压到本地打开。

# 2、部署环境

使用xxl-job需要连接数据库,存储日志和任务队列相关的数据 GitHub仓库地址:https://github.com/xuxueli/xxl-job/blob/master/doc/db/tables_xxl_job.sql (opens new window)

# 3、修改配置
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
1
2
3
4
5
# 4、 SpringBoot集成
# 4.1 引入依赖
<dependency>
    <groupId>com.xuxueli</groupId>
    <artifactId>xxl-job-core</artifactId>
    <version>2.5.0</version>
</dependency>
1
2
3
4
5
# 4.2 配置
# xxl-job
xxl.job.admin.addresses=http://xxx.xxx.xxx.xxx:8080/xxl-job-admin
#accessToken需要本地和xxl-job-admin中的accessToken一致即可
xxl.job.accessToken=default_token
xxl.job.executor.appname=test
xxl.job.executor.address=
xxl.job.executor.ip=
xxl.job.executor.port=9999
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
xxl.job.executor.logretentiondays=30
1
2
3
4
5
6
7
8
9
10
# 4.3 测试
# 4.3.1配置类

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;

@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;

    @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() {
        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;
    }
}
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 4.3.2本地编写测试代码
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class SampleXxlJob {
    //ReturnT<String> 只是自己封装的数据类型,你也可以自己直接return String
    @XxlJob("demoJobHandler")
    public ReturnT<String> demoJobHandler(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);
            System.out.println("Heelo world");
        }
        
        return ReturnT.FAIL;
    }
    
    @XxlJob("demoJobHandler2")
    public ReturnT<String> demoJobHandler2(String param) throws Exception {
        // 业务逻辑处理
        return ReturnT.SUCCESS;
    }
    
    
}
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
# 4.3.3在控制台配置任务

image

# 4.3.4在执行器配置自己服务的地址

例如:http://127.0.0.1:9999 (opens new window) 9999端口需要和application.yml中的端口一致

xxl.job.executor.port=9999
1

image

# 2.2 Docker部署

# 2.2.1 拉取镜像(如果报错,需要设置镜像源,参考Docker入门教程中的配置)
docker pull xuxueli/xxl-job-admin:2.5.0
1

运行镜像 --net host 使用主机网络 --xxl.job.alarm.email=false 不使用发送邮件的功能,有需要的话去掉这个参数

 docker run -d --net host --name xxl-job -p 8080:8080 -e PARAMS="--spring.datasource.url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai \
--spring.datasource.username=root \
--spring.datasource.password=password \
 --xxl.job.alarm.email=false" 
 docker.1ms.run/xuxueli/xxl-job-admin:2.5.0
1
2
3
4
5

运行完命令,打开http://xxx.xxx.xx.xxx:8080/xxl-job-admin (opens new window) 即可。

# 2.3 配置邮箱

# 2.3.1 本地配置邮箱(本次以网易邮箱为例)

需要将username 和 from修改为你的邮箱配置 from表示如果有异常,将使用这个邮箱发送地址 但是需要注意的是,psssword并不是你的邮箱密码,而是授权密码管理

spring.mail.host=smtp.163.com
spring.mail.port=465
spring.mail.username=xxx@163.com
spring.mail.from=xxx@163.com
spring.mail.password=xxxx
1
2
3
4
5

获取教程如下: 1、进入网页的网易邮箱: 2、进入设置的POP3/SMTP/MAP image

3、开启IMAP/SMTP服务 POP3/SMTP服务 image

4、新增授权码 image

# 2.4NAT-APP

1、如果你的xxl-job是部署在云服务器,而执行任务的代码是在本地,那么任务是调度不到本地的,是因为你本地的ip没办法被xxl-job访问到。这时候就需要使用内网穿透,将你本地的ip映射到公网上边 地址:https://natapp.cn/member/dashborad (opens new window)

2、购买隧道 image

3、配置 image

4、下载客户端 网址:https://natapp.cn/#download (opens new window) 根据需要下载: image

5、使用 打开config.ini文件,将authtoken更换为自己的tokne,双击exe文件打开,就可以通过公网访问你本地的项目了。 image

6、在xxl-job使用 这时候http://xxx.natapp1.cc 已经是你本地的项目+9999端口的服务了,这时候只需要将http://xxx.natapp1.cc 配置到xxl-job的执行器管理中的OnLine 机器地址即可。 image image

最近更新: 8/29/2025, 4:14:44 PM