The use of xxljob in the project

1: Download the source code

In this download the source code to local, then introduced into the idea, waiting for the completion of its construction, and then we 切换到2.0.2release subsequent operations.

2: Database operation

2.1: Database initialization

The doc/db/tables_xxl_job.sqldatabase database initialization script execution to complete work in the database.

2.2: Modify the db configuration

Modify the xxl-job-admin/src/main/resources/application.propertiesfile and modify the db configuration in it to your local information.

3: Start

This is a standard springboot program, you can xxl-job-admin/src/main/java/com/xxl/job/admin/XxlJobAdminApplication.javastart the application directly , of course, it can be run in the form of jar or war, which will be discussed later. After starting, http://127.0.0.1:8080/xxl-job-admin/access through the address , the default user password admin/123456is xxl-job-admin/src/main/resources/application.propertiesconfigured in the, after login, as shown in the figure below:
Insert picture description here

4: Define the actuator

We use springboot to define the executor, where the source code can be downloaded here .

4.1: Define the configuration file

# web port
server.port=8084
### xxl-job admin的地址,用来注册自己的信息
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
### APPName,这里需要先通过xxl-job admin手动添加
xxl.job.executor.appname=xxl-job-executor-dongshidaddy
### 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
xxl.job.executor.ip=
### 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
xxl.job.executor.port=9991
### 执行器通讯TOKEN [选填]:非空时启用;
xxl.job.accessToken=
### 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### 执行器日志保存天数 [选填] :值大于3时生效,启用执行器Log文件定期清理功能,否则不生效;
xxl.job.executor.logretentiondays=-1

Note that you xxl.job.executor.appname=xxl-job-executor-dongshidaddyneed to manually add them in the background of xxl-job admin, as follows:
Insert picture description here

4.2: Define the actuator configuration class

dongshi.daddy.config.XxlJobConfig
@Configuration
public class XxlJobConfig {
    
    
    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    @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(initMethod = "start", destroyMethod = "destroy")
    public XxlJobSpringExecutor xxlJobExecutor() {
    
    
        logger.info(">>>>>>>>>>> xxl-job config init.");
        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;
    }
}

4.3: Start

dongshi.daddy.TestDebugRemoteTomcatApplicationStart by class . After starting, check the xxl-job admin background, as follows, you can see that the online machine of the executor has been added:
Insert picture description here
by Edit Configurationsadding another application in the idea , the main function is still the same dongshi.daddy.TestDebugRemoteTomcatApplication, and then modify the configuration file In the port number server.port, xxl.job.executor.port(used for actuator communication), ensure that the port number does not conflict, and then register an actuator instance, start it after completion, as shown below, you can see that an online instance has been added:
Insert picture description here

5: Define the task

5.1: Define tasks

@JobHandler(value = "demoJobHandler")
@Component
public class DemoJobHandler extends IJobHandler {
    
    

    @Override
    public ReturnT<String> execute(String param) throws Exception {
    
    
        System.out.println("demoJobHandler run param is: " + param);
        XxlJobLogger.log("XXL-JOB, Hello World→ → → →.");

        for (int i = 0; i < 2; i++) {
    
    
            XxlJobLogger.log("beat at:" + i);
            TimeUnit.SECONDS.sleep(2);
        }
        return SUCCESS;
    }
}

5.2: Add task

As shown below:
Insert picture description here

5.3: View task execution

The following is the background call log:

demoJobHandler run param is: i a param aaaaaaa!!!!!!!
demoJobHandler run param is: i a param aaaaaaa!!!!!!!
demoJobHandler run param is: i a param aaaaaaa!!!!!!!
demoJobHandler run param is: i a param aaaaaaa!!!!!!!

View the xxl background log:
Insert picture description here
Insert picture description here

6: Run as a jar package

Type the jar package as follows:
Insert picture description here
pay attention to modify the port number here, do not conflict with the previous one, and then just java -jarrun:

xbdeMacBook-Air:temp xb$ java -jar xxl-job-admin-2.0.2.jar 

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.20.RELEASE)
 ...
 10:00:25.634 logback [main] INFO  o.s.b.c.e.t.TomcatEmbeddedServletContainer - Tomcat started on port(s): 8078 (http)
10:00:25.646 logback [main] INFO  c.x.job.admin.XxlJobAdminApplication - Started XxlJobAdminApplication in 11.077 seconds (JVM running for 12.799)

Then visit the test:
Insert picture description here

7: admin cluster deployment

Just start multiple instances directly, and then hook up nginx to be responsible for complex balance, but pay attention 要共用一个mysql数据库.

Guess you like

Origin blog.csdn.net/wang0907/article/details/112093316