当前位置: 首页 > 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.lryc.cn/news/619639.html

相关文章:

  • 使用TexLive与VScode排版论文
  • 从0开始配置conda环境并在PyCharm中使用
  • Node.js浏览器引擎+Python大脑的智能爬虫系统
  • 低成本扩展方案:S7-200SMART作为S7-1500分布式IO从站的上位机配置指南
  • Linux网络性能调优终极指南:深度解析与实践
  • 初识c语言————排序方法
  • 【新手入门】Android Studio 项目结构拆解,快速理解文件作用!
  • 【Linux】常用命令(三)
  • 数据结构:用数组实现队列(Implementing Queue Using Array)
  • Python实现点云概率ICP(GICP)配准——精配准
  • 8.13打卡 DAY 41 简单CNN
  • 多模态RAG赛题实战之策略优化--Datawhale AI夏令营
  • 桌面运维如何深造
  • MySQL表约束
  • Spring Boot项目中线程池的全面教程
  • 中高级餐饮服务食品安全员考试核心知识点汇总
  • Spring Boot初级概念及自动配置原理
  • Spring Boot 3 连接池最大连接数设置建议
  • sample_kol里配置为 deep sleep mode,则系统进入 STR
  • Spring、Spring MVC、Spring Boot与Spring Cloud的扩展点全面梳理
  • Python【算法中心 03】Docker部署Django搭建的Python应用流程实例(Docker离线安装配置+Django项目Docker部署)
  • django name ‘QueryDict‘ is not defined
  • 更改webpack默认配置项
  • Git Bash
  • 导轨焊接机器人:重塑高效精准焊接的新标杆
  • VUE3中的内置 API
  • amis表单较验
  • SpringCloud(1)
  • 从“存得对”到“存得准”:MySQL 数据类型与约束全景指南
  • opencv:直方图