XXL job distributed scheduled task

Today's topic: XXL job distributed scheduled tasks

reference resources: https://blog.csdn.net/huangjinjin520/article/details/106880276/

XXL-JOB is a lightweight distributed task scheduling framework. Its core design goal is rapid development, simple learning, lightweight and easy to expand. General microservices use this framework for scheduled tasks.

It happened that this thing was used in the company's new project, and then I was given the technical choice of the distributed scheduled task, including integrating this technology into the project. Today, let's record how to integrate it.

1 download XXL job

First, we need to download the project

git clone https://github.com/xuxueli/xxl-job.git

After downloading these modules, you can directly start the admin module to enter the background

xxl-job-admin: Dispatching center

xxl-job-core: Public dependence

xxl-job-executor: Actuator Sample Example (select the appropriate version of actuator, which can be used directly, or refer to it and transform the existing project into an actuator)
xxl-job-executor-sample-spring: Spring Version, via Spring Container management actuator, which is more general, is recommended;
xxl-job-executor-sample-springboot: Springboot Version, via Springboot Management actuator;
xxl-job-executor-sample-jfinal: JFinal Version, via JFinal Management actuator;
xxl-job-executor-sample-nutz: Nutz Version, via Nutz Management actuator;

I'll just choose the spring boot actuator

2 import script

Type the script in the doc directory into the database

3. Modify database account and password

Insert picture description here

4. Add Scheduled Task business class

Insert picture description here

5. The actuator name shall correspond to

Add corresponding actuator management in actuator management

The machine address can be registered automatically without writing.

If there is no actuator, add an actuator

6 add scheduled tasks

Add scheduled task

Execute it and find that it can succeed.

OK, this completes the simple startup.

7 integration in the project

The official XXL job executor sample jboot is actually a module in our microservice, so we can introduce dependencies in our own projects:

  <!--xxj-job Core dependency-->

In fact, I can create a public module of XXL job and create a configuration class:

public class XxlJobConfig {
    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    private String adminAddresses;

    private String accessToken;

    private String appname;

    private String address;

    private String ip;

    private int port;

    private String logPath;

    private int logRetentionDays;

    public XxlJobSpringExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        //Offset 1000 ports to the right based on the project ports, which can be compatible with the projects of all modules

        return xxlJobSpringExecutor;

     * For multiple network cards and internal deployment of containers, the registered IP can be flexibly customized with the help of the "InetUtils" component provided by "spring cloud commons";
     *      1,Import dependency:
     *          <dependency>
     *             <groupId>org.springframework.cloud</groupId>
     *             <artifactId>spring-cloud-commons</artifactId>
     *             <version>${version}</version>
     *         </dependency>
     *      2,Configuration file, or container startup variable
     *          spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
     *      3,Get IP
     *          String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();


Introduce dependencies in this module:

 <!--xxj-job Core dependency-->

Then we can introduce the XXL job module into our own module

Just create a task class, introduce @ XXL job, configure a Handler, and add it in the background.

nacos configuration:

# xxl-job
  config: classpath:logback.xml
      accessToken: ""
      address: ""
      appname: ${spring.application.name}
      ip: ""
      logpath: /data/applogs/xxl-job/jobhandler
      logretentiondays: 30
      port: ${server.port}

That is, on the official website document of XXL job, we only need the admin module.

Added by stone on Thu, 11 Nov 2021 05:29:25 +0200