brief introduction
XXL-JOB Is a lightweight distributed task scheduling platform, its core design goal is to develop quickly, learn simple, lightweight, easy to expand. Open source code and access to a number of companies online product line, out of the box.
The official documents are very complete, so there is no need to elaborate. This paper mainly builds XXL-JOB and simple use records.
Build the XXL job admin management end
Running environment
- Ubuntu 16.04 64 bit
- Mysql 5.7
Install Mysql
$ sudo apt-get update $ sudo apt-get install mysql-server ## Set up mysql, mainly in terms of security, password policy, etc $ mysql_secure_installation ## reconfigure 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 ...
Create database
$ mysql -u root -p mysql> CREATE database if NOT EXISTS `xxl-job` default character set utf8 collate utf8_general_ci;
Create user
$ mysql -u root -p mysql> CREATE USER 'xxl-job'@'%' IDENTIFIED BY 'xxlJob2019@'; mysql> GRANT ALL PRIVILEGES ON `xxl-job`.* TO 'xxl-job'@'%';
Local test XXL job admin
Pull the latest source code
$ git clone git@github.com:xuxueli/xxl-job.git $ cd xxl-job
Import project
I am familiar with Idea development tools, so I use Idea's Gradle project for demonstration.
Open the XXL job. The project structure is as follows
Test items
Open XXL job admin / resources / application.properties, and modify mysql connection information
### xxl-job, datasource spring.datasource.url=jdbc:mysql://192.168.32.129:3306/xxl-job?Unicode=true&characterEncoding=UTF-8 spring.datasource.username=xxl-job spring.datasource.password=xxlJob2019@
Initialize the database with / XXL job / Doc / db / tables ﹣ XXL job.sql. The initialization should be as follows
When you are ready, you can start the project, and then open the address http: / / localhost: 8080 / XXL job admin to see the home page
deploy
Package dispatch center
$ cd /xxl-job $ mvn install ... [INFO] xxl-job ............................................ SUCCESS [ 0.513 s] [INFO] xxl-job-core ....................................... SUCCESS [ 4.258 s] [INFO] xxl-job-admin ...................................... SUCCESS [ 5.525 s] [INFO] xxl-job-executor-samples ........................... SUCCESS [ 0.016 s] [INFO] xxl-job-executor-sample-spring ..................... SUCCESS [ 2.188 s] [INFO] xxl-job-executor-sample-springboot ................. SUCCESS [ 0.892 s] [INFO] xxl-job-executor-sample-jfinal ..................... SUCCESS [ 1.753 s] [INFO] xxl-job-executor-sample-nutz ....................... SUCCESS [ 1.316 s] [INFO] xxl-job-executor-sample-frameless .................. SUCCESS [ 0.358 s] [INFO] xxl-job-executor-sample-jboot ...................... SUCCESS [ 1.279 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 18.549 s [INFO] Finished at: 2019-06-05T14:40:25+08:00 [INFO] ------------------------------------------------------------------------
Seeing the above information indicates that our package is successful. There will be a jar file in the directory of / XXL job / XXL job admin: XXL job admin-2.1.0-snapshot.jar
Deploy to server
$ sudo apt install openjdk-8-jdk $ java -version openjdk version "1.8.0_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 $ sudo cd /data/xxl-job ## Upload our packed jar to this directory, and add a soft connection $ sudo ln -s xxl-job-admin-2.1.0-SNAPSHOT.jar current.jar ## Register as system service, which can achieve the purpose of abnormal restart, power on and self start, etc $ sudo vim /etc/systemd/system/xxl-job.service [Unit] Description=xxl-job Service Daemon After=mysql.service [Service] Environment="JAVA_OPTS= -Xmx1024m -Xms1024m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:NewRatio=3 -Dserver.port=8081" # java to write absolute path ExecStart=/usr/local/jdk/bin/java -jar /data/xxl-job/current.jar Restart=always WorkingDirectory=/data/xxl-job/ [Install] WantedBy=multi-user.target $ sudo systemctl enable xxl-job.service $ sudo service xxl-job start $ sudo service xxl-job status ● xxl-job.service - xxl-job Service Daemon Loaded: loaded (/etc/systemd/system/xxl-job.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2019-07-18 18:19:08 CST; 2min 19s ago Main PID: 27572 (java) CGroup: /system.slice/xxl-job.service └─27572 /usr/local/jdk/bin/java -jar /data/xxl-job/current.jar
Let's 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.
Directly use the built-in spring boot test project XXL job executor sample spring boot to test and modify the configuration file
xxl-job-executor-sample-springboot=http://192.168.32.129:8080/xxl-job-admin
Custom tasks
Write a simple task to print 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 * @date 2019/6/5 */ @JobHandler(value="gcddJobHandler") @Component public class GcddJobHandler extends IJobHandler { @Override public ReturnT<String> execute(String param) throws Exception { for (int i = 0; i < 100; i++) { XxlJobLogger.log("XXL-JOB, print " + i); TimeUnit.SECONDS.sleep(1); } return SUCCESS; } }
Start actuator
Then start the actuator. After starting, we will find that the actuator list of the management page will be more than the one we just started
Add tasks
View task execution log
It can be seen that the task has been successfully executed according to our plan, which is very convenient.
epilogue
To learn more, visit Official website of XXL job