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

五、 注册中心节点查看

后记:才疏学浅,不吝赐教。
边栏推荐
- MySQL:SQL概述及数据库系统介绍 | 黑马程序员
- Fine grained identification, classification, retrieval data set collation
- 如何实现搜索引擎中的拼写纠错功能——思路
- [advanced C language] user defined type
- Some specifications based on zfoo development project
- Summary of DOM knowledge points
- How to write controller layer code gracefully?
- JS绘制极坐标颜色渐变
- gyctf_2020_document
- QT learning 02 GUI program example analysis
猜你喜欢

Fine grained identification, classification, retrieval data set collation

爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记
![[review and Book delivery] 6 interesting R language projects for beginners](/img/d9/b785c92f503b78977b47a7feb88776.jpg)
[review and Book delivery] 6 interesting R language projects for beginners

Automatic integration: yescomusa, an overseas cross-border e-commerce, cooperates with cloud expansion to realize one-stop Automation Service

QT learning 07 coordinate system in QT

EB-5 immigration in the United States reappears to be positive, and the reauthorization policy of the regional center is suspended

ThinkPad VMware installation virtual machine: this host supports Intel VT-x, but Intel VT-x is disabled (problem resolution)
![[advanced C language] user defined type](/img/0d/50924ef21f07ca8188855c2b78d929.png)
[advanced C language] user defined type

vsftp 与 TFTP 与 samba 与 nfs 复习

New CorelDRAW technical suite2022 latest detailed function introduction
随机推荐
Mysql:sql overview and database system introduction | dark horse programmer
Color space conversion in video tonemapping (HDR to SDR) (bt2020 to bt709, YCbCr, YUV and RGB)
JS的初步语法
New CorelDRAW technical suite2022 latest detailed function introduction
QT learning 01 GUI program principle analysis
Can't recognize the original appearance
Basic operations such as MySQL startup under Windows platform
Table responsive layout tips for super nice
[graduation season 𞓜 advanced technology Er] employees who have worked for seven years do not want you to take another detour
QT learning 02 GUI program example analysis
Solr基础操作6
[review and Book delivery] 6 interesting R language projects for beginners
How to view the CPU cores and threads in win11? Win11 view the tutorial of how many cores and threads the CPU is
面试官:为什么数据库连接很消耗资源?我竟然答不上来。。一下懵了!
Review of vsftp, TFTP, samba and NFS
QT learning 03 birth and essence of QT
自动融合,驰骋海外丨跨境电商YescomUSA携手云扩实现一站式自动化服务
[advanced C language] dynamic memory management
ThinkPad VMware installation virtual machine: this host supports Intel VT-x, but Intel VT-x is disabled (problem resolution)
传统微服务框架如何无缝过渡到服务网格 ASM
https://github.com/augtsy/springboot-elasticJob-demo