栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Springboot 分布式定时任务 ElasticJob-Lite 3.0 配置和整合

Springboot 分布式定时任务 ElasticJob-Lite 3.0 配置和整合

Springboot 分布式定时任务 ElasticJob-Lite 3.0 配置和使用

ElasticJob用于解决springboot的定时任务 @EnableScheduling 集群部署产生的冲突

本文基于yml配置文件和注解启动分布式定时任务

中文官方文档

中文官方文档

版本依赖

JAVA

请使用 Java 8 及其以上版本。

Maven

请使用 Maven 3.5.0 及其以上版本。

ZooKeeper

请使用 ZooKeeper 3.6.0 及其以上版本。

控制台页面

3.0 以后使用全新的控制台页面

下载地址
下载页面
下载超链接

使用方法

1.解压缩

// windows解压工具启动会报错,新版本win10可以直接使用tar命令
tar zxvf apache-shardingsphere-elasticjob-3.0.1-lite-ui-bin.tar.gz

2.改配置文件

登录等其他配置

控制台提供两种账户:管理员及访客。 管理员拥有全部操作权限,访客仅拥有察看权限。 默认管理员用户名和密码是 root/root,访客用户名和密码是 guest/guest,可通过 confapplication.properties 修改管理员及访客用户名及密码。

auth.root_username=root
auth.root_password=root
auth.guest_username=guest
auth.guest_password=guest

3.启动

bin目录下的启动文件

windows运行start.bat

linux执行start.sh, 8899 为默认端口号,可通过启动脚本输入 -p 自定义端口号。

官方文档
更多信息参考官方文档

Springboot整合ElasticJob-Lite POM文件引入依赖

	org.apache.shardingsphere.elasticjob
	elasticjob-lite-core
	3.0.1


// 后面基于yml配置需要starter

	org.apache.shardingsphere.elasticjob
	elasticjob-lite-spring-boot-starter
	3.0.1

针对spring-boot-starter-parent版本(1.x)较低的,需要指定curator的版本

		
			org.apache.curator
			curator-recipes
			5.1.0
		
		
			org.apache.curator
			curator-framework
			5.1.0
		
【作业开发】编写定时任务(job)

基于yml配置文件启动的必须注解@Component,执行业务时也需要自动注入

@Slf4j
@Component
public class TestJob implements SimpleJob {

    @Override
    public void execute(ShardingContext shardingContext) {
        log.info("TestJob开始作业: {}", shardingContext);
        // 不分片时将分片数设为1,直接在此写业务代码
        // 多分片时
        switch (shardingContext.getShardingItem()) {
            case 0:
                log.info("TestJob第一个分片,参数: {}", shardingContext.getShardingParameter());
                break;
            case 1:
                log.info("TestJob第二个分片,参数: {}", shardingContext.getShardingParameter());
                break;
            // ..........
        }
    }

}

更多作业类型参考官方文档作业开发

【作业监听器】

在多个分片的情况,在每个分片执行完后要进行一个数据汇总,这时可以使用作业监听器

yml文件配置

用一个yml文件去专门管理当前服务的job,利用spring.profiles.include=job引入application-job.yml配置文件

application.yml //主配置文件
application-dev.yml //本地测试环境配置文件
application-prod.yml //生产测试环境配置文件
application-job.yml //job配置文件
配置Zookeeper
elasticjob:
  reg-center:
    server-lists: localhost:2181
    namespace: demo
配置job
elasticjob:
  jobs:
    testJob: #自定义名称
      elasticJobClass: com.example.demo.job.TestJob #类名
      cron: 0/5 * * * * ? #运行cron
      sharding-total-count: 1 #总分片数
      shardingItemParameters: 0=TestJob #分片参数

更多配置信息

全部yml配置

application.yml

spring:
  application:
    name: demo
  profiles:
    active: dev
    include: job

server:
  port: 8232

application-dev.yml

spring:
  redis:
    database: 2
    host: 127.0.0.1
    port: 6379
elasticjob:
  reg-center:
    server-lists: 127.0.0.1:2181
    namespace: demo

application-job.yml

elasticjob:
  jobs:
    testJob:
      elasticJobClass: com.example.demo.job.TestJob
      cron: 0/5 * * * * ?
      sharding-total-count: 1
      shardingItemParameters: 0=TestJob
    demoJob:
      elasticJobClass: com.example.demo.job.DemoJob
      cron: 0/5 * * * * ?
      sharding-total-count: 1
      shardingItemParameters: 0=DemoJob
运行结果

实例1运行分片为1的DemoJob和分片为2的TestJob第一个分片

实例2运行分片为2的TestJob第二个分片

实例1

DemoJob开始作业: ShardingContext(jobName=demoJob, taskId=demoJob@-@0@-@READY@-@172.26.112.1@-@24164, shardingTotalCount=1, jobParameter=, shardingItem=0, shardingParameter=DemoJob)
TestJob开始作业: ShardingContext(jobName=testJob, taskId=testJob@-@0@-@READY@-@172.26.112.1@-@24164, shardingTotalCount=2, jobParameter=, shardingItem=0, shardingParameter=CPU)
TestJob第一个分片,参数: CPU

实例2

TestJob开始作业: ShardingContext(jobName=testJob, taskId=testJob@-@1@-@READY@-@172.26.112.1@-@17112, shardingTotalCount=2, jobParameter=, shardingItem=1, shardingParameter=内存)
TestJob第二个分片,参数: 内存

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/746538.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号