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 解决思路
办法有两个:
-
统一 SnakeYAML 版本(推荐)
强制pom.xml
中使用最新版本,并排除 Spring Boot 中的旧版本。 -
重写 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,点个关注不迷路