当前位置:网站首页>分布式任务调度 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个节点上。

五、 注册中心节点查看

后记:才疏学浅,不吝赐教。
边栏推荐
- 云呐|如何利用系统管理固定资产?如何进行固定资产管理?
- DOM 知识点总结
- This PMP Exam (June 25), some people are happy and others are worried. That's why
- Statistical query of SQL Server database
- Zhongkang holdings opens the offering: it plans to raise HK $395million net, and it is expected to be listed on July 12
- Solr basic operations 12
- [advanced C language] special user-defined type
- 8软件工程环境
- 俞敏洪:我的退与进;架构师必须了解的5种最佳软件架构模式;Redis夺命52连问|码农周刊VIP会员专属邮件周报 Vol.096
- 关联性——典型相关分析
猜你喜欢

New CorelDRAW technical suite2022 latest detailed function introduction

MySQL高级篇2

《性能之巅第2版》阅读笔记(四)--Memory监测

爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记

Review of vsftp, TFTP, samba and NFS

Statistical query of SQL Server database
![[advanced C language] special user-defined type](/img/f3/c01b8b10e02860afc88a216ff8dbce.png)
[advanced C language] special user-defined type

After 8 years of polishing, "dream factory of game design" released an epic update!

代码分析平台 SonarQube 实战
![克隆無向圖[bfs訪問每條邊而不止節點]](/img/34/2a1b737b6095293f868ec6aec0ceeb.png)
克隆無向圖[bfs訪問每條邊而不止節點]
随机推荐
Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
[review and Book delivery] 6 interesting R language projects for beginners
Five key elements of the data center
MySQL高级篇2
This PMP Exam (June 25), some people are happy and others are worried. That's why
Rotating colored clover
面试官:为什么数据库连接很消耗资源?我竟然答不上来。。一下懵了!
QT learning 01 GUI program principle analysis
Vulnhub靶机-MoriartyCorp
Vulnhub target -moriartycorp
[advanced C language] address book implementation
Activity invitation | the Apache Doris community essay and speech solicitation activity has begun!
Solr基础操作9
Summarize Flink runtime architecture in simple terms
Solr基础操作8
视频ToneMapping(HDR转SDR)中的颜色空间转换问题(BT2020转BT709,YCbCr、YUV和RGB)
Findbugs modification summary
请指教在线开户是什么意思?另外想问,现在在线开户安全么?
Zhongkang holdings opens the offering: it plans to raise HK $395million net, and it is expected to be listed on July 12
About mongodb error: connecting to: mongodb://127.0.0.1:27017/?compressors=disabled &gssapiServiceName=mongodb
https://github.com/augtsy/springboot-elasticJob-demo