Spring Boot 整合 ElasticJob 分布式任务调度教程

精心整理了最新的面试资料和简历模板,有需要的可以自行获取

点击前往百度网盘获取
点击前往夸克网盘获取


Spring Boot 整合 ElasticJob 分布式任务调度教程

一、ElasticJob 简介

ElasticJob 是当当网开源的分布式任务调度解决方案,支持:

  • 分布式调度:自动分片、负载均衡
  • 弹性调度:服务器宕机自动故障转移
  • 定时任务:支持Cron表达式
  • 任务监控:运维控制台可视化

二、环境准备

  • JDK 1.8+
  • Maven 3.6+
  • Spring Boot 2.7.x
  • ElasticJob 3.0.3
  • ZooKeeper 3.8.1(用于注册中心)

三、整合步骤

1. 创建Spring Boot项目

通过 start.spring.io 创建基础项目,选择 Web 依赖

2. 添加依赖

<!-- ElasticJob 核心依赖 -->
<dependency>
    <groupId>org.apache.shardingsphere.elasticjob</groupId>
    <artifactId>elasticjob-lite-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>

<!-- ZooKeeper 客户端 -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>5.4.0</version>
</dependency>

3. 配置ElasticJob

application.yml 配置:

elasticjob:
  reg-center:
    server-lists: localhost:2181  # ZooKeeper地址
    namespace: elasticjob-demo   # 命名空间

4. 编写定时任务

import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.stereotype.Component;

@Component
public class MyElasticJob implements SimpleJob {

    @Override
    public void execute(ShardingContext context) {
        System.out.println("------ 任务执行开始 ------");
        System.out.println("分片总数: " + context.getShardingTotalCount());
        System.out.println("当前分片: " + context.getShardingItem());
        System.out.println("分片参数: " + context.getShardingParameter());
        // 添加业务逻辑
    }
}

5. 任务配置类

import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticJobConfig {

    @Bean
    public JobConfiguration myJobConfig() {
        return JobConfiguration.newBuilder("myJob", 3)  // 任务名称和分片数
                .cron("0/5 * * * * ?")  // 每5秒执行一次
                .shardingItemParameters("0=A,1=B,2=C")  // 分片参数
                .overwrite(true)  // 本地配置覆盖注册中心
                .build();
    }
}

四、ZooKeeper配置

  1. 下载并启动ZooKeeper:
# 解压后修改配置
cp conf/zoo_sample.cfg conf/zoo.cfg
# 启动服务
bin/zkServer.sh start

五、启动与测试

  1. 启动Spring Boot应用
  2. 查看控制台输出:
------ 任务执行开始 ------
分片总数: 3
当前分片: 0
分片参数: A

------ 任务执行开始 ------
分片总数: 3
当前分片: 1
分片参数: B

------ 任务执行开始 ------
分片总数: 3
当前分片: 2
分片参数: C

六、高级配置

1. 任务事件追踪

elasticjob:
  tracing:
    type: RDB  # 使用数据库存储日志
    rdb:
      url: jdbc:mysql://localhost:3306/ej_log
      username: root
      password: root

2. 任务监听器

public class MyJobListener implements ElasticJobListener {

    @Override
    public void beforeJobExecuted(ShardingContext context) {
        System.out.println("任务开始执行");
    }

    @Override
    public void afterJobExecuted(ShardingContext context) {
        System.out.println("任务执行结束");
    }
}

// 配置类中添加
@Bean
public ElasticJobListener myJobListener() {
    return new MyJobListener();
}

七、常见问题

1. ZooKeeper连接失败

  • 检查ZooKeeper服务是否启动
  • 确认防火墙开放2181端口

2. 分片不生效

  • 确保启动多个实例(分布式环境)
  • 检查分片参数配置格式

3. 任务重复执行

  • 检查namespace配置是否唯一
  • 确认注册中心没有残留旧配置

八、总结

通过整合Spring Boot和ElasticJob,我们可以轻松实现:

  • 分布式环境下的任务分片
  • 高可用故障转移
  • 动态扩缩容
  • 完善的监控体系

注意事项

  1. 生产环境建议使用独立ZooKeeper集群
  2. 注意ElasticJob版本与Spring Boot的兼容性
  3. 复杂任务建议使用DataflowJob类型
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘵奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值