Elastic-job-lite整合spring-boot與監控平臺搭建

1、本文目的

本篇文章會對Elastic-job與spring-·boot進行整合,同時搭建任務監控平臺經過elastic-job-console以便對任務進行管理,至於涉及到的分片sharding及配置項等核心概念能夠參考官網文檔,本文主要是快速搭建工程,讓定時任務先跑起來,文末附源碼地址。php

2、Elastic-job相關

Elastic-job官網中文 elasticjob.io/index_zh.ht…html

github地址 github.com/elasticjob/…java

3、搭建定時任務工程

  1. 經過Spring Initializr建立boot工程,只需添加web依賴便可,而後添加elastic-job-lite-core核心包
<dependency>
	<groupId>com.dangdang</groupId>
	<artifactId>elastic-job-lite-core</artifactId>
	<version>2.1.5</version>
</dependency>
複製代碼
  1. 與spring整合添加elastic-job-lite-spring包
<dependency>
	<groupId>com.dangdang</groupId>
	<artifactId>elastic-job-lite-spring</artifactId>
	<version>2.1.5</version>
</dependency>
複製代碼
  1. 編寫定時任務類MyElasticJob,實現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. 任務監聽類 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. 編寫spring-elastic-job.xml文件,放到resources文件下
<?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. 啓動類引入xml配置文件
@SpringBootApplication
@ImportResource(locations = {"classpath:spring-elastic-job.xml"})
public class MorningShiroApplication {

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

}
複製代碼
  1. zk查看任務節點

4、控制檯搭建

經過elastic-job-console工程來進行監控任務執行, github.com/elasticjob/…git

下載工程,導入Idea啓動elastic-job-lite-console工程,啓動以前修改父pom文件中的spring框架版本爲5.1.9.RELEASE,而後啓動,瀏覽器輸入localhost:8899便可進入控制檯。github

4.1 控制檯界面,配置zk服務地址 web

4.2 查看任務類表 spring

4.3 查看服務列表 api

4.4 修改任務 瀏覽器


關於zk集羣搭建查看: juejin.im/post/5cee0d…bash

源碼地址:github.com/alwyngo/mor…

相關文章
相關標籤/搜索