XXL-JOB是一個輕量級分佈式任務調度平臺,其核心設計目標是開發迅速、學習簡單、輕量級、易擴展。現已開放源代碼並接入多家公司線上產品線,開箱即用。java
官方文檔很完善,很少贅述。本文主要是搭建XXL-JOB
和簡單使用的記錄。mysql
$ sudo apt-get update $ sudo apt-get install mysql-server ## 設置mysql,主要是安全方面的,密碼策略等 $ mysql_secure_installation ## 配置遠程訪問 $ 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 ...
$ mysql -u root -p mysql> CREATE database if NOT EXISTS `xxl-job` default character set utf8 collate utf8_general_ci;
$ mysql -u root -p mysql> CREATE USER 'xxl-job'@'%' IDENTIFIED BY 'xxlJob2019@'; mysql> GRANT ALL PRIVILEGES ON `xxl-job`.* TO 'xxl-job'@'%';
$ git clone git@github.com:xuxueli/xxl-job.git $ cd xxl-job
我比較熟悉Idea
開發工具,因此這裏使用Idea
的Gradle
項目進行演示。git
打開xxl-job
,項目結構以下github
打開xxl-job-admin/resources/application.properties
,修改mysql鏈接信息spring
### 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@
使用/xxl-job/doc/db/tables_xxl_job.sql
初始化數據庫,初始化完應該以下圖sql
準備就緒後,就能夠啓動項目了,而後打開地址http://localhost:8080/xxl-job-admin將會看到首頁數據庫
$ 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] ------------------------------------------------------------------------
看到以上信息,說明咱們打包成功了,在/xxl-job/xxl-job-admin
目錄下會存在jar文件:xxl-job-admin-2.1.0-SNAPSHOT.jar
ubuntu
$ 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 ## 上傳咱們打包好的jar至此目錄,並添加軟鏈接 $ sudo ln -s xxl-job-admin-2.1.0-SNAPSHOT.jar current.jar ## 註冊爲system服務,能夠達到異常重啓,開機自啓等目的 $ 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要寫絕對路徑 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
咱們訪問一下http://192.168.32.129:8080/xxl-job-admin:vim
以上,咱們的任務調度管理端已經搭建完成,接下來,讓咱們測試下任務調度。安全
直接使用自帶的SpringBoot
測試項目xxl-job-executor-sample-springboot
進行測試,修改配置文件
xxl-job-executor-sample-springboot=http://192.168.32.129:8080/xxl-job-admin
編寫一個簡單的任務,打印100次當前序列
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; } }
而後啓動執行器,啓動完成後,咱們會發現管理頁面的執行器列表會多出咱們剛纔啓動的執行器
能夠看到,任務已經按照咱們的規劃執行成功了,很是的方便。
想要了解更詳細的內容,請訪問xxl-job官網