preface
Hello everyone, MY name is Jack Xu. Today, I would like to share xxL-Job with you. XXL is the initials of Daishen Xu Xuili, a programmer of Dianping who created an open source task scheduling system in his spare time. In addition to xxL-job, the authors have opened source many other components, and there are now 11 open source projects, forming the XXL family. Up to now, there are hundreds of companies registered on xxL-job’s official website. In fact, many of them have not registered, so there are far more than these hundreds of companies actually using XXL-job. The latest version is 2.2.0.
The shortage of the Quartz
Before I introduce Xl-Job, Quartz is almost 20 years old, the scheduling model is mature and easy to integrate into Spring, making it a good choice for doing business. But it also faces some problems, such as:
1. Scheduling logic (Scheduler) and task class are coupled in the same project. As the number of scheduling tasks increases and the scheduling task logic becomes heavier, the overall performance of the scheduling system will be greatly affected.
2. The load between nodes of Quartz cluster is random. Whoever grabs the database lock will execute the task, which may lead to the situation of drought death and flood death, which can not play the performance of the machine.
3. Quartz itself does not provide dynamic scheduling and management interface functions, so you need to develop according to the API.
4. Quartz’s logging, statistics, and monitoring are not perfect.
Both xxl-Job and Elastice-Job encapsulate Quartz, making it easier and more powerful to use.
XXL – job characteristics
Xxl-job has more features than Quartz. Generally, it can be divided into three categories:
1. Performance improvement: More tasks can be scheduled.
2. Reliability improvement: handling task timeout, failure and failover.
3. More convenient operation and maintenance: provide operation interface, have user rights, detailed log, provide notification configuration, automatically generate reports and so on.
XXL – job architecture
The following is the architecture diagram of XXL-job on the official website. The latest one is V2.1.0, but it is not very different. In V2.2.0, the self-developed RPC(XXL-RPC) marked in the red box has been replaced with restful HTTP request mode.I have simplified the above diagram so that the scheduler and the business execution of xxl-job are separate. The scheduler determines the scheduling of tasks and invokes the executor interface to execute tasks via HTTP.
Install the XXL – job
Download the source code
Download address:Github.com/xuxueli/xxl… Pay attention to
Instead of cloning the latest master branch, which is under development, click on the Release page to download the stable version (2.2.0).
- Doc: Documentation, including “scheduling database” table building scripts
- Xxl-job-core: public Jar dependencies
- Xxl-job-admin: scheduling center, project source, spring Boot project, can be directly started
- Xxl-job-executor-samples: executor, sample sample project, where the Spring Boot project can be launched directly. You can develop on this project, or you can retrofit an existing project into an executor project.
Initializing the database
Database script in doc/db directory:Execute this SQL locally and generate 8 tables with the following functions:
The name of the table | role |
---|---|
xxl_job_group | Executor information table: Maintains task executor information |
xxl_job_info | Scheduling extended information table: Used to save extended information about XXL-job scheduling tasks, such as task group, task name, machine address, actuator, execution input parameter, alarm email, and so on |
xxl_job_lock | Task scheduling lock table |
xxl_job_log | Scheduling log table: Used to save historical information about xxL-job scheduling tasks, such as scheduling results, execution results, scheduling input parameters, scheduling machines, and actuators |
xxl_job_log_report | Scheduling log report: a report that stores xxl-job task scheduling logs, which is used on the report function page of the scheduling center |
xxl_job_logglue | Task GLUE log: Saves the GLUE update history and supports the GLUE version tracing function |
xxl_job_registry | An executor registry that maintains online executor and dispatch center machine address information |
xxl_job_user | System user table |
Installing the Dispatch Center
Admin and Executor projects can be copied separately (two Spring Boot projects), and poM files can be modified. In fact, the development and operation of the time is certainly an independent project.
Let’s copy xxl-job-admin to a separate project
Xxl-job-core is referenced in the POM file
Then modify the values in the application. Properties configuration file, mainly for port and database configuration
### xxl-job, datasource
spring.datasource.url=JDBC: mysql: / / 127.0.0.1:3306 / xxl_job? useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Copy the code
Explicitly add the account and password for logging in to the dispatch center
xxl.job.login.username=admin
xxl.job.login.password=123456
Copy the code
Ok, then start the project, spirng boot project should be everyone will start ha, input your account passwordLog in, done!To ensure availability, the scheduling center can be used for cluster deployment. The following conditions must be met:
- Connect to the same database
- Clock consistency in a cluster (Neglected in a single-node cluster)
- You are advised to use domain names for access and Nginx for load balancing. Access management interface, executor registration, all with domain names.
Create executor
The executor is equivalent to the group leader, responsible for the specific execution of the task, and it assigns threads (members) to perform the task. Actuators need to be registered with the scheduler so that the scheduler knows how to select actuators or route them. The execution result of the actuator must also be reported to the scheduling center through callback.
There are six executable demos in xxl-job-executor-samples of the source code that show you how to create actuators in some of the major frameworks, even without them. An executor can be a single project or integrated within a business project (with one more port to start), hence the name business instance.
The spring Boot project is selected as an example. Copy a project from the source code. If you are integrating in a business project, you can also refer to this sample and add xxl-job-core dependencies to the project.
Also modify the configuration file application.properties as follows:
### Register with the tuning center
xxl.job.admin.addresses=http://127.0.0.1:7391/xxl-job-admin
### Log file path
xxl.job.executor.logpath=D:/xxl-job/jobhandler
### Because we want to simulate cluster deployment of actuators, pack and click to run multiple times, and set up random ports for the service
server.port=The ${random. Int [10000199] 99}
### actuator port
xxl.job.executor.port=${random. Int [9000100]}
In cluster deployment, the two configurations must be consistent
xxl.job.executor.appname=xxl-job-executor-jackxu
xxl.job.executor.address=
Copy the code
To start the project, check this box when starting multiple clusters of actuators locally, because we have configured random ports, so no errors will be reported.Finally, we added an executor to the dispatch center – Executor ManagerRefresh. The two actuators I started here are registered
Test XXL – job
Now that xxL-job is installed, let’s create a task to test it
Create a task
Here I’ve created a new SimpleJobHandlerThere are several points to note when creating a new task:
1. In the Spring Bean instance (@Component annotation), develop the Job method. The method format is “public ReturnT < String > demoJobHandler(String param)”, the return value and parameter format is fixed, this cannot be used, the only can be used is the method name.
@xxlJob = JobHandler; @xxlJob = JobHandler; @xxlJob = JobHandler; The other init corresponds to the JobHandler initialization method and destory to the JobHandler destruction method. These two methods are created in the task class.
3. Execution log: You need to print the execution log through “xxljoblogger. log” and write it to the specified log file.
This is a fixed specification, the development of any task to implement this specification, as well as the above code on the development steps, this is Xu Xue-li God in the source code to write, I copy it over, we can see!
Configuration tasks
Let’s configure the above code in the dispatch center, in the task management – a new one, there are some options you do not know what it means, this is ok, I will write a later article to introduce the shard task and other functions, you first according to my configuration configuration.Click the startOpen the folder and take a look. The path is the one I created in application.properties. The task is to write hello Word five times in the previous blogXxl-job quick Start guide I believe you have mastered, you can follow my tutorial to do it again, about the advanced functions of XXL-job AFTER I will write another article,Original is not easy, if you feel good, please click a like!
The resources
XXL – job website www.xuxueli.com/xxl-job/
Download the code github.com/xuxueli/xxl…