Elastic-job-lite integrated spring-boot and monitoring platform to build

First, the purpose of this paper

This article will Elastic-job with spring- · boot integration, while monitoring platform in order to build a task management tasks by elastic-job-console, as to relate to the 分片shardingcore concepts and configuration items, etc. can refer to the official website of the documents, this paper are quickly build project, let them start and regular tasks, the end of the text with source address.

Two, Elastic-job related

Elastic-job official website of the Chinese elasticjob.io/index_zh.ht...

github address github.com/elasticjob/...

Third, the project set up regular tasks

  1. Create a boot project by Spring Initializr, simply add the web can rely on, and then add the elastic-job-lite-core core package
<dependency>
	<groupId>com.dangdang</groupId>
	<artifactId>elastic-job-lite-core</artifactId>
	<version>2.1.5</version>
</dependency>
复制代码
  1. Add to integrate with spring elastic-job-lite-spring package
<dependency>
	<groupId>com.dangdang</groupId>
	<artifactId>elastic-job-lite-spring</artifactId>
	<version>2.1.5</version>
</dependency>
复制代码
  1. Write regular tasks like MyElasticJob, implement the interface SimpleJob
package com.morning.morningshiro.jobs;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.morning.morningshiro.dao.entity.UserEntity;

import javax.annotation.Resource;
import java.util.List;

public class MyElasticJob implements SimpleJob {

    @Override
    public void execute(ShardingContext context) {
        // 根据分片项执行任务
        System.out.println(context.toString());
        switch (context.getShardingItem()) {
            case 0:
                break;
            case 1:
                // do something by sharding item 1
                break;
            case 2:
                // do something by sharding item 2
                break;
            // case n: ...
        }
    }
}
复制代码
  1. Task listener class JobListener
package com.morning.morningshiro.jobs;

import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;

public class JobListener implements ElasticJobListener {
    @Override
    public void beforeJobExecuted(ShardingContexts shardingContexts) {
        System.out.println(shardingContexts.toString());
    }

    @Override
    public void afterJobExecuted(ShardingContexts shardingContexts) {
        System.out.println(shardingContexts.toString());
    }
}
复制代码
  1. Preparation of spring-elastic-job.xml file into the resourcesfile
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:reg="http://www.dangdang.com/schema/ddframe/reg"
       xmlns:job="http://www.dangdang.com/schema/ddframe/job"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.dangdang.com/schema/ddframe/reg
                        http://www.dangdang.com/schema/ddframe/reg/reg.xsd
                        http://www.dangdang.com/schema/ddframe/job
                        http://www.dangdang.com/schema/ddframe/job/job.xsd
                        ">
    <!--配置作业注册中心 -->
    <!--namespace为注册到zk的名称-->
    <reg:zookeeper id="regCenter" server-lists="127.0.0.1:2181" namespace="dd-job" base-sleep-time-milliseconds="1000"
                   max-sleep-time-milliseconds="3000" max-retries="3"/>

    <bean id="myElasticJob" class="com.morning.morningshiro.jobs.MyElasticJob"/>

    <!-- 配置作业-->
    <!--overwrite 修改后覆盖原有定时任务-->
    <!--id 任务ID-->
    <job:simple id="job1" job-ref="myElasticJob" overwrite="true" registry-center-ref="regCenter"
                cron="0/30 * * * * ?" sharding-total-count="4" sharding-item-parameters="0=USER,1=TWO" description="test">
        <job:listener class="com.morning.morningshiro.jobs.JobListener"/>
    </job:simple>
</beans>
复制代码
  1. Start class to introduce xml configuration file
@SpringBootApplication
@ImportResource(locations = {"classpath:spring-elastic-job.xml"})
public class MorningShiroApplication {

	public static void main(String[] args) {
		SpringApplication.run(MorningShiroApplication.class, args);
	}

}
复制代码
  1. View task node zk

Fourth, set up the console

To carry out monitoring tasks performed by the elastic-job-console project, github.com/elasticjob/...

Download engineering, import Idea started elastic-job-lite-console project, modify the spring version of the framework is the parent pom file before you start 5.1.9.RELEASE, then start your browser and enter localhost:8899to enter the console.

4.1 console interface, configure the service address zk

4.2 View Task class table

4.3 Service List View

4.4 modify the task


About zk Cluster Setup View: juejin.im/post/5cee0d...

Source Address: github.com/alwyngo/mor...

Guess you like

Origin juejin.im/post/5d6924d5e51d4561df7805d8