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,點個關注不迷路