当前位置: 首页 > news >正文

Spring cloud集成ElastictJob分布式定时任务完整攻略(含snakeyaml报错处理方法)


ElasticJob 是一款轻量级、可扩展的分布式定时任务解决方案,基于 Quartz 二次开发,支持任务分片、失效转移、任务追踪等功能,非常适合在 Spring Cloud 微服务场景中使用。

我将带你完成 Spring Cloud 集成 ElasticJob 的全过程,并分享一个 SnakeYAML 报错 的实际解决方案。


1. 环境准备

在开始前,你需要准备:

  • Spring Cloud 项目(Spring Boot 2.x / 3.x 均可)
  • Zookeeper 作为注册中心(建议本地启动一个 localhost:2181
  • ElasticJob 最新版本(支持 3.x)
  • Java 8+

2. 引入依赖

pom.xml 中添加:

<dependencies><!-- ElasticJob Lite 核心依赖 --><dependency><groupId>org.apache.shardingsphere.elasticjob</groupId><artifactId>elasticjob-lite-spring-boot-starter</artifactId><version>3.0.0</version></dependency><!-- Lombok(可选) --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency>
</dependencies>

3. 核心代码实现

这里我们使用 Java 代码方式 启动定时任务,避免复杂的 XML 配置。

3.1 创建 Job 实现类

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.simple.job.SimpleJob;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;@Slf4j
public class NewJob implements SimpleJob {@Overridepublic void execute(ShardingContext shardingContext) {int item = shardingContext.getShardingItem();log.info("当前分片:{}", item);}
}

3.2 注册中心 & 任务配置

import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;public class ElasticJobConfig {public void schedule() {new ScheduleJobBootstrap(createRegistryCenter(),new NewJob(),createJobConfiguration()).schedule();}public CoordinatorRegistryCenter createRegistryCenter() {CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration("localhost:2181", "my-job"));regCenter.init();return regCenter;}public JobConfiguration createJobConfiguration() {return JobConfiguration.newBuilder("test", 1) // 任务名 & 分片数.jobParameter("hello").cron("0/5 * * * * ?") // 每 5 秒执行一次.build();}
}

在 Spring Boot 启动类中调用:

@SpringBootApplication
public class JobApplication {public static void main(String[] args) {SpringApplication.run(JobApplication.class, args);new ElasticJobConfig().schedule();}
}

4. 运行效果

启动后,日志将每 5 秒输出一次当前分片 ID:

INFO  当前分片:0

5. SnakeYAML 报错问题及解决方案

在 Spring Boot 3.x / SnakeYAML 高版本环境中,ElasticJob 启动时可能会遇到 org.yaml.snakeyaml.error 相关异常,比如:

java.lang.NoSuchMethodError: 'org.yaml.snakeyaml.nodes.Tag'

原因是 ElasticJob 内部使用了旧版本 SnakeYAML API,而 Spring Boot 自带了较新版本,导致 API 变更 报错。

5.1 解决思路

办法有两个:

  1. 统一 SnakeYAML 版本(推荐)
    强制 pom.xml 中使用最新版本,并排除 Spring Boot 中的旧版本。

  2. 重写 Representer(你提供的成功方法)
    通过继承 org.yaml.snakeyaml.representer.Representer 并适配新 API。


5.2 重写 Representer 示例


public class CustomRepresenter extends Representer {
//加入新的无参构造public Representer() {super(new DumperOptions());this.representers.put(null, new org.yaml.snakeyaml.representer.Representer.RepresentJavaBean());}//其它代码不变
}

6. 总结

  • ElasticJob 适合分布式场景,Zookeeper 是其注册中心核心组件。
  • 推荐 Java API 启动任务,方便在 Spring Boot 中集成。
  • SnakeYAML 报错可通过 版本统一自定义 Representer 解决。

我是PXM,点个关注不迷路

http://www.dtcms.com/a/328876.html

相关文章:

  • 移动端网页调试实战,触摸事件穿透与点击冲突问题的定位与优化
  • C++中的`auto`与`std::any`:功能、区别与选择建议
  • CV 医学影像分类、分割、目标检测,之【肝脏分割】项目拆解
  • 数据挖掘常用公开数据集
  • [爬虫实战] 基于半自动化的cookie池更新逻辑讲解
  • 数据分析总结
  • MyBatis 中 XML 与 DAO 接口的位置关系及扫描机制详解
  • 把 Linux 装进“小盒子”——边缘计算场景下的 Linux 裁剪、启动与远程运维全景指南
  • 关于Google Pixel,或者安卓16,状态栏颜色无法修改的解决方案
  • 双屏加固笔记本电脑C156-2:坚固与高效的完美融合
  • FPGA+护理:跨学科发展的探索(四)
  • 在CentOS 7上配置Android USB网络共享方式的方法
  • MacOS字体看起来比在 Windows 上更好?
  • HTTPS与CA证书:安全通信全解析
  • CA+https+动态WEB页面部署
  • JavaWeb核心:HttpServletRequest与HttpServletResponse详解
  • Linux 服务部署:自签 CA 证书构建 HTTPS 及动态 Web 集成
  • OpenBMC中观察者模式架构与实现全解析
  • http与https协议区别;vue3本地连接https地址接口报500
  • 解惑rust中的 Send/Sync(译)
  • 什么是费曼学习法?
  • 机器学习-Cluster
  • Jenkins一直无法启动,怎么办?
  • C# winform 调用 OPC UA C# WinForm 的批量订阅方法
  • Java19 Integer 位操作精解:compress与expand《Hacker‘s Delight》(第二版,7.4节)
  • 向长波红外成像图注入非均匀噪声
  • 【嵌入式电机控制#31】FOC:霍尔安装误差的补偿
  • Unity:GUI笔记(二)——工具栏和选择网格、滚动列表和分组、窗口、自定义皮肤样式、自动布局
  • Linux系统有何特点?linux系统组成如何?
  • NTUSER.DAT是什么文件