当前位置:网站首页>分布式任务调度 ElasticJob demo
分布式任务调度 ElasticJob demo
2022-06-30 00:21:00 【tang_sy】
代码地址:GitHub - augtsy/springboot-elasticJob-demo: ElasticJob 分布式任务调度ElasticJob 分布式任务调度. Contribute to augtsy/springboot-elasticJob-demo development by creating an account on GitHub.
https://github.com/augtsy/springboot-elasticJob-demo
一、常见开源产品
Quartz、XXL-Job、ElasticJob等
- Quartz:该框架应用最为广泛,其完全基于 Java 实现,Quartz 对单个任务的控制基本做到了极致,以其强大功能和应用灵活性,成为开源任务调度领域的权威及同类开源产品如 Antares 的基石;
- XXL-JOB:一个轻量级分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。XXL-JOB 支持分片,支持简单任务依赖,支持子任务依赖,不支持跨平台的。
- Elastic-Job:支持任务分片(作业分片一致性),没有任务编排,不支持跨平台;
二、业务场景
公司任务中心,提供客户导入导出等定时任务处理。
三、代码实现
elastic-job依赖zookeeper,首先要安装好zookeeper。可以docker 启动镜像等。
properties配置
info.app.name=elastic-job-demo
zookeeper.servers=172.19.8.76:2181引入依赖
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-core</artifactId>
<version>2.1.5</version>
</dependency>
<!-- elastic-job-lite-spring -->
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>2.1.5</version>
</dependency>注册中心配置类
@Configuration
public class ElasticRegCenterConfig {
/**
* 配置zookeeper
*
* @param serverList
* @param namespace
* @return
*/
@Bean(initMethod = "init")
public ZookeeperRegistryCenter zookeeperRegistryCenter(
@Value("${zookeeper.servers}") final String serverList,
@Value("${info.app.name}") final String namespace) {
return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
}
}注解类
/**
* @Description: ElasticJob枚举配置
* @Auther: tsy
* @Date: 2022/06/24/2:08 下午
*/
@Inherited
@Documented
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.TYPE)
public @interface ElasticJobConfig {
String cron() default "";
String value() default "";
/**
* 分片总数
*/
int shardingTotalCount() default 1;
/**
* 分片项参数
*/
String shardingItemParameters() default "0=0";
}
SimpleJob配置类
/**
* @Description: SimpleJob 配置
* @Auther: tsy
* @Date: 2022/06/24/2:21 下午
*/
@Slf4j
@Configuration
public class SimpleJobConfig {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private ZookeeperRegistryCenter zookeeperRegistryCenter;
@PostConstruct
public void startSimpleJob() {
applicationContext.getBeansWithAnnotation(ElasticJobConfig.class).forEach((className, obj) -> {
ElasticJobConfig config = obj.getClass().getAnnotation(ElasticJobConfig.class);
// 表达式
String cron = StringUtils.defaultIfBlank(config.cron(), config.value());
// 分片总数
int shardingTotalCount = config.shardingTotalCount();
// 分片项参数
String shardingItemParameters = config.shardingItemParameters();
JobListener elasticJobListener = new JobListener();
SimpleJob simpleJob = (SimpleJob) obj;
new SpringJobScheduler(simpleJob, zookeeperRegistryCenter,
getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters),
elasticJobListener).init();
});
}
/**
* 创建简单作业配置构建器.
*
* @param jobName 作业名称
* @param cron 作业启动时间的cron表达式
* @param shardingTotalCount 作业分片总数
* @return 简单作业配置构建器
*/
private LiteJobConfiguration getLiteJobConfiguration(Class<?> jobName, String cron, int shardingTotalCount, String shardingItemParameters) {
return LiteJobConfiguration.newBuilder(
new SimpleJobConfiguration(
JobCoreConfiguration.newBuilder(jobName.getName(), cron, shardingTotalCount)
.shardingItemParameters(shardingItemParameters)
.build(),
jobName.getCanonicalName()))
.monitorExecution(true)
.overwrite(false).build();
}
}定时任务实现
/**
* @Description: 业务定时任务
* @Auther: tsy
* @Date: 2022/06/24/2:36 下午
*/
@Slf4j
@Component
@ElasticJobConfig(cron = "*/4 * * * * ?", shardingTotalCount = 5, shardingItemParameters = "0=0,1=1,2=2,3=3,4=4")
public class SimpleJobDemo implements SimpleJob {
@Override
public void execute(ShardingContext shardingContext) {
//获取分片总数
int shardingTotalCount = shardingContext.getShardingTotalCount();
//获取分片项
int shardingItem = shardingContext.getShardingItem();
//获取分片项参数
String shardingParameter = shardingContext.getShardingParameter();
log.info("分片总数:{} 分片项:{} 分片项参数:{}", shardingTotalCount, shardingItem, shardingParameter);
// 业务逻辑,ElasticJob 不提供数据处理的功能,框架只会将分片项分配至各个运行中的作业服务器,开发者需要自行处理
}
}四、测试
只启动一个实例,结果所有分片都被分配到这一个实例。

启动其他四台实例,结果5个分片被均匀分片5个节点上。

五、 注册中心节点查看

后记:才疏学浅,不吝赐教。
边栏推荐
- js中的事件
- Statistical query of SQL Server database
- Quick pow: how to quickly find power
- Web APIs 环境对象 丨黑马程序员
- Solr basic operations 14
- Embedded development: Hardware in the loop testing
- Introduction to reptiles: data capture of Betta barrage, with 11 introductory notes attached
- 爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记
- What does it mean to open an account online? In addition, is it safe to open an account online now?
- MySQL基础2
猜你喜欢

Mysql:sql overview and database system introduction | dark horse programmer
![多数元素II[求众数类之摩尔投票法]](/img/8f/5925f97c0f5f8c50c19a9ef6d7723c.png)
多数元素II[求众数类之摩尔投票法]

JVM之栈空间

Events in JS

MySQL:SQL概述及数据库系统介绍 | 黑马程序员

Can't recognize the original appearance

JS绘制极坐标颜色渐变

Summarize Flink runtime architecture in simple terms

vsftp 与 TFTP 与 samba 与 nfs 复习

视频ToneMapping(HDR转SDR)中的颜色空间转换问题(BT2020转BT709,YCbCr、YUV和RGB)
随机推荐
面试官:为什么数据库连接很消耗资源?我竟然答不上来。。一下懵了!
Getting started with qpainter: drawing the chess interface
Project 1: deploy lamp ECSHOP e-commerce platform
How to realize the spelling correction function in search engine
Table responsive layout tips for super nice
About mongodb error: connecting to: mongodb://127.0.0.1:27017/?compressors=disabled &gssapiServiceName=mongodb
C MDI open subform to remove automatically generated menu bar
swift笔记
请指教在线开户是什么意思?另外想问,现在在线开户安全么?
云呐|固定资产信息系统管理,信息化固定资产管理
Majority element ii[molar voting method for finding modes]
Code analysis platform sonarqube actual combat
有流量,但没有销售?增加网站销量的 6 个步骤
8 software engineering environment
label问题排查:打不开标注好的图像
QT learning 03 birth and essence of QT
Color space conversion in video tonemapping (HDR to SDR) (bt2020 to bt709, YCbCr, YUV and RGB)
Vulnhub target -moriartycorp
这次的PMP考试(6月25日),有人欢喜有人忧,原因就在这...
云原生爱好者周刊:炫酷的 Grafana 监控面板集合
https://github.com/augtsy/springboot-elasticJob-demo