您的位置:首页 > 财经 > 产业 > SpringBoot教程(二十二) | SpringBoot实现分布式定时任务之elastic-job

SpringBoot教程(二十二) | SpringBoot实现分布式定时任务之elastic-job

2024/10/6 8:30:36 来源:https://blog.csdn.net/qq_20236937/article/details/141226495  浏览:    关键词:SpringBoot教程(二十二) | SpringBoot实现分布式定时任务之elastic-job

SpringBoot教程(二十二) | SpringBoot实现分布式定时任务之elastic-job

  • 简介
  • 前置条件:需要ZooKeeper配合
  • 1、引入相关依赖
  • 2、application.yml中配置注册中心和作业调度
  • 巨坑(配置修改无效)
  • 3、job实例
  • 4、ElasticJob-UI监控平台 (相当于管理端页面)

参考文章:
【1】SpringBoot整合分布式任务调度Elastic-Job
【2】ElasticJob3.0整合SpringBoot,ElasticJob-Lite【ElasticJob入门篇】

简介

  • elastic-job是当当网基于quartz 二次开发的弹性分布式任务调度系统,功能丰富强大,采用zookeeper实现分布式协调,实现任务高可用以及分片。
  • Elastic-Job是一个分布式调度的解决方案,由当当网开源,它由两个相互独立的子项目Elastic-Job-Lite和Elastic-Job-Cloud组成。
  • 使用Elastic-Job可以快速实现分布式任务调度。
  • ElasticJob 已于 2020 年 5 月 28 日成为 Apache ShardingSphere 的子项目。
  • 官方文档:https://shardingsphere.apache.org/elasticjob/index_zh.html

前置条件:需要ZooKeeper配合

zk在elastic-job的作用

Elastic-Job依赖ZooKeeper完成对执行任务信息的存储(如任务名称、任务参与实例、任务执行策略等);
Elastic-Job依赖ZooKeeper实现选举机制,在任务执行实例数量变化时(如在快速上手中的启动新实例或停止实例),会触发选举机制来决定让哪个实例去执行该任务。

我这边主要讲SpringBoot,所以肯定会采用场景启动器starter的

什么是场景启动器?
也就是spring-boot-starter- 开头的依赖,
以下是一些常见的Spring Boot场景启动器示例:
(1)spring-boot-starter-data-jpa:包含Spring Data JPA和Hibernate的依赖,用于简化数据库访问和JPA(Java Persistence API)的使用。
(2)spring-boot-starter-data-mongodb:包含Spring Data MongoDB的依赖,用于简化MongoDB数据库的访问。
(3)spring-boot-starter-security:包含Spring Security的依赖,用于添加安全功能,如用户认证和授权。
(4)… … … …

使用Spring Boot的场景启动器确实可以让您避免手动添加所需的依赖,因为它已经为您预先定义并包含了这些依赖。这样,您就可以更专注于业务逻辑的实现,而不是花费大量时间在依赖管理和配置上了。

1、引入相关依赖

我本地的SpringBoot为2.6.4版本,zk版本为3.7版本
此处的版本elasticjob就使用了3.0.1 ,我之前尝试换了3.0.2 发现项目启动报错了(具体原因暂未发现)

<dependency><groupId>org.apache.shardingsphere.elasticjob</groupId><artifactId>elasticjob-lite-spring-boot-starter</artifactId><version>3.0.1</version>
</dependency>

2、application.yml中配置注册中心和作业调度

server:port: 9999#elasticjob配置
elasticjob:# 注册中心配置reg-center:# 连接 ZooKeeper 服务器的列表, 包括 IP 地址和端口号,多个地址用逗号分隔server-lists: 127.0.0.1:2188# ZooKeeper 的命名空间namespace: elastic-job-spring# 等待重试的间隔时间的初始毫秒数base-sleep-time-milliseconds: 1000# 等待重试的间隔时间的最大毫秒数maxSleepTimeMilliseconds: 3000# 最大重试次数maxRetries: 3# 会话超时毫秒数sessionTimeoutMilliseconds: 60000# 连接超时毫秒数connectionTimeoutMilliseconds: 15000# 作业配置, 更多的配置参考官网jobs:# 自定义的job名(可随便取名)springJob: #定时任务类的全路径名elasticJobClass: com.example.reactboot.utils.job.SpringBootJobcron: 0/5 * * * * ?shardingTotalCount: 1shardingItemParameters: 0=Beijingoverwrite: true #想修改有效,必须加# 自定义的job名(可随便取名)springTestJob:#定时任务类的全路径名elasticJobClass: com.example.reactboot.utils.job.SpringRunnerJobcron: 0/10 * * * * ?shardingTotalCount: 2shardingItemParameters: 0=Beijing,1=Shanghaioverwrite: true #想修改有效,必须加

作业调度参数讲解

属性名含义类型是否必填
elasticJobClass定时任务类的全路径名String必填
cron定时任务执行的cron表达式String必填
shardingTotalCount作业分片总数,一般情况下设置为1足够了int必填
shardingItemParameters分片序列号和参数用等号分隔,多个键值对用逗号分隔。
分片序列号从0开始,不可大于或等于作业分片总数。 如:0=a,1=b,2=c
String非必填
overwrite每次启动作业都以本地配置为准布尔类型非必填(建议设置为true)

巨坑(配置修改无效)

修改了jobs里面的配置(比如cron、shardingTotalCount之类的)没有作用,跑的还是之前的旧配置

最开始的解决方案
windows环境下我的的解决方法是上zookeeper使用命令删除那个命名空间才行

1.先双击zkServer.cmd启动 zookeeper 服务器
2.再双击zkCli.cmd启动 zookeeper客户端
3.然后在客户端里面使用 查命令: ls /
4.接着 执行 删除命令: deleteall /elastic-job-spring

最终解决方案
想了半天,发现不可能这么大的问题网上没有人发现吗?
不可能,于是去官网看了一下,发现有一个参数overwrite(每次启动作业都以本地配置为准。)
于是我把它配置上去,最终解决这个巨坑

3、job实例

第一个job:

package com.example.reactboot.utils.job;import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.stereotype.Component;import java.time.LocalDate;
import java.time.LocalDateTime;@Component
public class SpringBootJob implements SimpleJob {@Overridepublic void execute(ShardingContext shardingContext) {System.out.println(this.getClass() + ":分片总数是: [{" + shardingContext.getShardingTotalCount() + "}]; 当前分片项是: [{" + shardingContext.getShardingItem() + "}]");//分片参数,(0=text,1=image,2=video,参数就是text、image...)String shardingParameter = shardingContext.getShardingParameter();//任务参数, 配置是name=test就是name=testString jobParameter = shardingContext.getJobParameter();System.out.println("current sharding item " + shardingContext.getShardingItem() + ", shardingParameter = " + shardingParameter + ", jobParameter:" + jobParameter);}
}

第二个job:

package com.example.reactboot.utils.job;import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.stereotype.Component;import java.time.LocalDate;
import java.time.LocalDateTime;@Component
public class SpringRunnerJob implements SimpleJob {@Overridepublic void execute(ShardingContext shardingContext) {System.out.println(this.getClass() + ":分片总数是: [{" + shardingContext.getShardingTotalCount() + "}]; 当前分片项是: [{" + shardingContext.getShardingItem() + "}]");//分片参数,(0=text,1=image,2=video,参数就是text、image...)String shardingParameter = shardingContext.getShardingParameter();//任务参数String jobParameter = shardingContext.getJobParameter();System.out.println("current sharding item " + shardingContext.getShardingItem() + ", shardingParameter = " + shardingParameter + ", jobParameter:" + jobParameter);}
}

启动项目,可以看到相关的日志。

4、ElasticJob-UI监控平台 (相当于管理端页面)

适用于【场景启动器starter】方式开发的elastic-job。

下载ElasticJob-UI

https://shardingsphere.apache.org/elasticjob/current/cn/downloads/

在这里插入图片描述
解压后在bin目录双击启动
(本人是在windows环境)
在这里插入图片描述

访问控制台

启动后游览器访问(默认端口是8088):http://127.0.0.1:8088/#/login 用户名/密码 root/root
登录成功后,链接上注册中心,链接成功后便可以进行任务的管理。
在这里插入图片描述
根据工程elasticjob配置文件里面的信息进行填写
在这里插入图片描述
填完以后,点击一下,检测是否能够成功链接
在这里插入图片描述
然后再去 “作业操作 /作业维度” 里面就可以看到 配置文件的定时任务job信息已经在这里显示出来了
在这里插入图片描述
再看一下项目控制台,定时任务已被触发(这边是设置了3个作业分片,3秒一次)
在这里插入图片描述
完美收工

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com