XXL-JOB Usage Summary


title: XXL-JOB use summary
date: 2023-07-14 11:35:19
tags:

  • XXL-JOB
  • Server
    categories:
  • Development technology and framework
    cover: https://cover.png
    feature: false

1. Initialize the scheduling database

Official website and documents: Distributed task scheduling platform XXL-JOB

project address:

There is a database initialization script in the project doc/db directory, execute the script to initialize the database

2. Configure the deployment dispatch center

Clone the project locally, including three modules

  • xxl-job-admin: scheduling center
  • xxl-job-core: public dependencies
  • xxl-job-executor-samples: Executor Samples (select the appropriate version of the executor, which can be used directly, or refer to it and transform the existing project into an executor)
    • xxl-job-executor-sample-springboot: SpringBoot version, manage the executor through SpringBoot, recommended
    • xxl-job-executor-sample-frameless: frameless version

Modify the dispatch center configuration file, the application.properties file under the project root path resources, and the log path of the logback.xml file, which can be modified according to personal choice

The content of the configuration file is described as follows, and can be modified according to the situation

### 调度中心JDBC链接:链接地址请保持和所创建的调度数据库的地址一致
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
### 报警邮箱
spring.mail.host=smtp.qq.com
spring.mail.port=25
[email protected]
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

Then start XxlJobAdmin and visit http://localhost:8080/xxl-job-adminthe management page. The specific port and path are subject to personal configuration. This address is the callback address of the executor

The default username and password are admin/123456, and the main interface after login is as follows

So far the dispatch center project has been successfully run

3. Configure the deployment executor project

The project introduces xxl-job-core dependency

<dependency>
      <groupId>com.xuxueli</groupId>
      <artifactId>xxl-job-core</artifactId>
      <version>2.4.0</version>
</dependency>

The configuration file is as follows:

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

Add configuration class XxlJobConfig

@Configuration
public class XxlJobConfig {
    
    

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${xxl.job.executor.appname}")
    private String appname;

    @Value("${xxl.job.executor.ip}")
    private String ip;

    @Value("${xxl.job.executor.port}")
    private int port;

    @Value("${xxl.job.accessToken}")
    private String accessToken;

    @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.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
        return xxlJobSpringExecutor;
    }
}

Add annotations to the methods that need to be executed regularly @XxlJob("JobHandler"), and the value here is the value of the subsequent scheduled task JobHandler

@RestController
public class TestController {
    
    

    @GetMapping("/test")
    @XxlJob("test")
    public String test() {
    
    
        System.out.println("test");
        return "test";
    }
}

then start the project

4. Execute scheduled tasks

First, you need to add the executor. AppName is the appname value of the executor configuration file. You can choose automatic registration or manual entry for the machine address. Here, choose automatic registration

Wait for a while after the addition is complete, and you can see that the machine address has been automatically registered. If it is always empty, it means that there is a problem with the automatic registration. You need to check whether the address and port of the dispatch center in the actuator configuration file are correct.

Then add a scheduled task, select the executor we just created, where the value of JobHandler is the previous @XxlJob("JobHandler")value

After saving, click Execute once to test

In the scheduling log, you can see that the execution was successful

5. Deploy to server

Deployment project see: Fan's Web

It should be noted here that after the dispatch center is deployed to the server, although the local executor can be registered in the dispatch center, it fails to execute the scheduled task, and the connection to the local address times out. Deploy the local project to the server to execute successfully

Guess you like

Origin blog.csdn.net/ACE_U_005A/article/details/131769433
Recommended