Moment For Technology

Still writing task scheduling codes by hand? Try this visual distributed scheduling framework!

Posted on Aug. 8, 2022, 7:21 p.m. by 刁冠宇
Category: The back-end Tag: The back-end java

SpringBoot e-commerce project mall (40K + STAR) address:


Task scheduling scenarios are often used in microservice systems. For example, periodically synchronize data every day, periodically generate service reports, and periodically clear logs. Today I recommend a distributed scheduling framework to help you easily complete the task scheduling work!

PowerJob profile

PowerJob is a new generation of distributed scheduling and computing framework, which enables you to easily complete task scheduling and distributed computing of complex tasks.

Main features:

  • Easy to use: Provides a front-end Web interface, allowing developers to visually manage scheduling tasks and view task running status and logs.
  • Improved timing policies: Supports CRON expression, fixed frequency, fixed delay, and API timing policies.
  • Multiple execution modes: Supports single machine, broadcast, Map, and MapReduce execution modes.
  • Dependency thin: minimum dependence only on relational databases (MySQL, etc.), extended dependence to MongoDB (for storing large online logs).

Why is there a dispatch center

We usually use QuartZ or Spring Task to implement scheduled tasks in applications, but in microservices architecture, it is not appropriate for many applications to be riddled with this kind of Task scheduling code. A reasonable solution is that the execution methods of tasks exist in the application, and we have a scheduling center that is responsible for scheduling these methods. We only need to configure tasks in the scheduling center. PowerJob is just such a distributed scheduling framework.

Installation preparation

PowerJob's scheduling center (PowerJob-Server) needs to use MySQL to store data and MongoDB to store logs, so we install and start these two services first.

  • Start MySQL service in Docker container;
docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \ -v /mydata/mysql/data:/var/lib/mysql \ -v /mydata/mysql/conf:/etc/mysql \ -e MYSQL_ROOT_PASSWORD=root \ -d Mysql: 5.7Copy the code
  • Create a database for PowerJob in MySQLpowerjob-product;
Copy the code
  • Start the MongoDB service in the Docker container.
Docker run - p - 27017-27017 the name mongo \ - v/mydata/mongo/db: / data/db \ - d mongo: 4.2.5Copy the code

Installing the Dispatch Center

Installing the dispatch center for PowerJob in Docker environment is very simple and can be done in minutes.

  • Download mirrorpowerjob-serverDocker image:
docker pull tjqq/powerjob-server:latest
Copy the code
  • Run in a Docker containerpowerjob-serverServices:
docker run -p 7700:7700 -p 10086:10086 --name powerjob-server \
--link mysql:db \
--link mongo:mongo \
-e TZ="Asia/Shanghai" \
-e PARAMS=" --spring.datasource.core.jdbc-url=jdbc:mysql://db:3306/powerjob-product? useUnicode=truecharacterEncoding=UTF-8 --spring.datasource.core.username=root --spring.datasource.core.password=root" \
-v ~/docker/powerjob-server:/mydata/powerjob/powerjob-server \
-v ~/.m2:/mydata/powerjob/.m2 \
-d tjqq/powerjob-server:latest
Copy the code
  • It can be accessed after running successfullypowerjob-serverThe Linux firewall needs to be open7700and10086Two ports, access address:

Initializes the actuator in the application

After installing the dispatch center, we need to initialize the PowerJob executor (PowerJob-worker) in the SpringBoot application.

  • First of all inpom.xmladdpowerjob-workerIs dependent on:
Copy the code
  • After theapplication.ymlAdded to the configuration filepowerjob-workerNote Related configuration;
    akka-port: 27777 # akka working port
    app-name: mall-tiny-powerjob Access application name, used for group isolation
    server-address: 192.1683.101.: 7700 Address of the dispatch server
    store-strategy: disk # Persistence
Copy the code
  • Add a stand-alone processorStandaloneProcessor, just inheritBasicProcessorInterface and implementationprocessMethod can;
package com.macro.mall.tiny.job;

public class StandaloneProcessor implements BasicProcessor {

    public ProcessResult process(TaskContext context){
        //OmsLogger can directly report logs to powerJob-server
        OmsLogger omsLogger = context.getOmsLogger();"StandaloneProcessor start process,context is {}.", context);"jobParams is {}", context.getJobParams());
        return new ProcessResult(true."Process success!"); }}Copy the code
  • After the image is packaged and uploaded, run the SpringBoot application service in the Docker container. Ensure that the time zone is configured in accordance with the scheduling center.
docker run -p 8080:8080 --name mall-tiny-powerjob \
--link mysql:db \
-v /etc/localtime:/etc/localtime \
-v /mydata/app/mall-tiny-powerjob/logs:/var/logs \
-e TZ="Asia/Shanghai"\ d mall - tiny/mall - tiny - powerjob: 1.0 the SNAPSHOTCopy the code

Task configuration and execution

With the actuator and the scheduling center, we only need to configure the task in the scheduling center to realize the task scheduling.

  • First we need to register the application (integration actuator) in the dispatch center, the application name isapplication.ymlIn, which is used heremall-tiny-powerjob:123456;

  • Then we can see a machine information on the home page;

  • After theTask managementAdd a task to the function, here we useCRONsetEvery 20 secondsExecute the processing method in the executor;

  • Click on the task listrunStart to perform tasks;

  • Click on the task listMore - Run recordsYou can view task run logs.

  • Click on theThe logYou can view the logs reported in the processor.jobParamsIs the parameter we set when we created the task earlier;

  • Click on thedetailsYou can view the result of the triggered task, which is that we areProcessResultInformation returned from the.

The resources

Official documentation:

Project source code address

In this paper, making Already included, welcome everyone Star!

About (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.