Moment For Technology

Experience of distributed task scheduling XXL-Job

Posted on Dec. 2, 2022, 1:36 a.m. by 林宜君
Category: The back-end Tag: distributed

Github. IO/Distributed Task Scheduling XXL-...

Introduction to the

Xxl-job is a lightweight distributed task scheduling platform. Its core design goals are rapid development, simple learning, lightweight and easy to expand. It is now open source and available to multiple companies online product lines, right out of the box.

The official documentation is perfect, so I don't need to go into detail. This article is mainly to build XXL-JOB and simple use of records.

Set up the XXL-job-admin management end

Runtime environment

  • Ubuntu 16.04 64
  • Mysql 5.7

Mysql installation

$ sudo apt-get update
$ sudo apt-get install mysql-server

Mysql  set password policy, password policy, password policy
$ mysql_secure_installation
Configure remote access
$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0 $sudo service mysql restart $sudo service mysql status ● mysql.service -mysql Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2019-06-05 13:23:41 HKT; 45s ago ...Copy the code

Creating a database

$ mysql -u root -p
mysql CREATE database if NOT EXISTS `xxl-job` default character set utf8 collate utf8_general_ci;
Copy the code

Create a user

$ mysql -u root -p
mysql CREATE USER 'xxl-job'@The '%' IDENTIFIED BY '[email protected]';
mysql GRANT ALL PRIVILEGES ON `xxl-job`.* TO 'xxl-job'@The '%';
Copy the code

Test XXL-job-admin locally

Pull the latest source code

$ git clone [email protected]:xuxueli/xxl-job.git
$ cd xxl-job
Copy the code

Import the project

I am familiar with Idea development tools, so here I use Idea Gradle project to demonstrate.

Open XXL-Job, and the project structure is as follows

Test project

Open the XXL - job - admin/resources/application properties, modify the mysql connection information

# # # XXL - job, datasource spring. The datasource. Url = JDBC: mysql: / / 192.168.32.129:3306 / XXL - job? Unicode=truecharacterEncoding=UTF-8 spring.datasource.username=xxl-job [email protected]Copy the code

Run the /xxl-job/doc/db/ tables_XXl_job. SQL command to initialize the database. The following figure shows the initialization

Ready, you can start the project, and then open the address http://localhost:8080/xxl-job-admin will see the home page

The deployment of

Packing dispatch center

$ cd/xxl-job $ mvn install ... [INFO] xxl-job ............................................ SUCCESS [s] 0.513 [INFO] XXL - job - the core... SUCCESS [s] 4.258 [INFO] XXL - job - admin... SUCCESS [s] 5.525 [INFO] XXL - job - executor - samples... SUCCESS [s] 0.016 [INFO] XXL - job - executor - sample - spring... SUCCESS [s] 2.188 [INFO] XXL - job - executor - sample - springboot... SUCCESS [s] 0.892 [INFO] XXL - job - executor - sample - jfinal... SUCCESS [s] 1.753 [INFO] XXL - job - executor - sample - nutz... SUCCESS [s] 1.316 [INFO] XXL - job - executor - sample - frameless... SUCCESS [s] 0.358 [INFO] XXL - job - executor - sample - jboot... SUCCESS [s] 1.279 [INFO] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 18.549s [INFO] Finished at: 2019-06-05T14:40:25+08:00 [INFO] ------------------------------------------------------------------------Copy the code

The jar file xxl-job-admin-2.1.0-snapshot. jar is stored in the/xxL-job/xxL-job-admin directory

Deploying to a server

$ sudo apt install openjdk-8-jdk
$ java -version
openjdk version "1.8.0 comes with _212"OpenJDK Runtime Environment (build 1.8.0_212-8u212-b03-0Ubuntu1.16.04.1-b03) OpenJDK 64-bit Server VM (build 25.212-b03,  mixed mode) $ sudo mkdir -p /data/xxl-job $ sudocd /data/xxl-job
Upload our packaged JAR to this directory and add a soft link
$ sudo ln -sXXL - job - admin - 2.1.0 - the SNAPSHOT. Jar current. The jar## Register as system service, can achieve the purpose of abnormal restart, startup and so on
$ sudo vim /etc/systemd/system/xxl-job.service
Description=xxl-job Service Daemon
After=mysql.service
[Service]
Type=simple
Environment="JAVA_OPTS= -Xmx1024m -Xms1024m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:NewRatio=3"
ExecStart=java -jar /data/xxl-job/current.jar
Restart=always
WorkingDirectory=/data/xxl-job/
[Install]
WantedBy=multi-user.target

$ sudo systemctl enableService $sudo service xxL-job start $sudo service xxL-job status ● Loaded: loaded (/etc/systemd/system/xxl-job.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2019-06-05 15:30:08 HKT; 2min 34s ago ...Copy the code

We visit http://192.168.32.129:8080/xxl-job-admin:

Test Task scheduling

Above, our task scheduling management end has been set up. Next, let's test the task scheduling.

Use the built-in SpringBoot test project xxL-job-executor-Sample-SpringBoot to test and modify the configuration file

XXL - job - executor - sample - springboot = http://192.168.32.129:8080/xxl-job-adminCopy the code

Custom Tasks

Write a simple task that prints the current sequence 100 times

package com.xxl.job.executor.service.jobhandler;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @author gaochen
 * @date2019/6/5 * /
@JobHandler(value="gcddJobHandler")
@Component
public class GcddJobHandler extends IJobHandler {
    @Override
    public ReturnTString execute(String param) throws Exception {
        for (int i = 0; i  100; i++) {
            XxlJobLogger.log("XXL-JOB, print " + i);
            TimeUnit.SECONDS.sleep(1);
        }
        returnSUCCESS; }}Copy the code

Starter actuator

Then start the executor, and when the startup is complete, we will find that the list of executors on the administration page will be more than the one we just started

Add tasks

View task execution logs

As you can see, the task has been executed successfully according to our plan, which is very convenient.

conclusion

For more details, please visit xxL-Job's official website

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.