ElasticJob 通过弹性调度、资源管控、以及作业治理的功能,打造一个适用于互联网场景的分布式调度解决方案,并通过开放的架构设计,提供多元化的作业生态。
1 项目集成
1.1 springboot项目配置
导入依赖:
<!--mysql连接基本配置--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.8</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.3.0</version></dependency><!-- MySql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency>
启动类设置
1.2 设计使用Elastic-job
作业调度:创建config类,注册zookeeper
作业配置:丰富config类,设置调度执行周期
createLiteJob创建作业
public LiteJobConfiguration createLiteJob(Class clazz,String cron,int cutNumber,String shardingParam){JobCoreConfiguration.Builder jobBuilder = JobCoreConfiguration.newBuilder(clazz.getSimpleName(),cron,cutNumber);JobCoreConfiguration simpleCoreConfig = jobBuilder.build();SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, clazz.getCanonicalName());LiteJobConfiguration simpleRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build();return simpleRootConfig;
}
作业开发:FileCustomElasticJob类,实现具体任务
@Component
public class FileCustomElasticJob implements SimpleJob {@Autowiredprivate FileCustomServiceImpl fileCustomService;@Overridepublic void execute(ShardingContext shardingContext) {dowWork();}private void dowWork() {//查询备份状态为0的任务List<FileCustom> fileCustoms = fileCustomService.selectAll();//获取该任务信息for (FileCustom fileCustom : fileCustoms) {Long id = backImg(fileCustom);if(id != -1){fileCustomService.changeStage(id,1);try {TimeUnit.SECONDS.sleep(1);System.out.println("备份完成");} catch (InterruptedException e) {e.printStackTrace();}}}}private Long backImg(FileCustom fileCustom) {Long id = fileCustom.getId();System.out.println("检查备份信息>>>>");System.out.println("备份方法名:" + fileCustom.getName());System.out.println("备份类型:" + fileCustom.getType());if(fileCustom.getType() == "img"){System.out.println("检查备份信息为img,进行备份<<<<");return id;}else {return Long.valueOf(-1);}}
}
1.3 数据库操作
实体类
1.4 运行结果
启动zookeeper,Apache ZooKeeper
运行springboot
2 认识分片
定义分片规则:
text 0
video 1
img 2
radio 3
单机器分片任务处理效果如下:
1)修改config代码,更改作业配置,加上分片
public LiteJobConfiguration createLiteJob(Class clazz,String cron,int cutNumber,String shardingParam){JobCoreConfiguration.Builder jobBuilder = JobCoreConfiguration.newBuilder(clazz.getSimpleName(),cron,cutNumber);if(!StringUtils.isEmpty(shardingParam)){jobBuilder.shardingItemParameters(shardingParam);}JobCoreConfiguration simpleCoreConfig = jobBuilder.build();SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, clazz.getCanonicalName());LiteJobConfiguration simpleRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build();return simpleRootConfig;
}@Bean(initMethod = "init")
public SpringJobScheduler fileCustomSchedule(FileCustomElasticJob fileCustomElasticJob, CoordinatorRegistryCenter coordinatorRegistryCenter){LiteJobConfiguration configuration = createLiteJob(fileCustomElasticJob.getClass(),"0/10 * * * * ?",4,"0=text,1=img,2=video,3=radio");return new SpringJobScheduler(fileCustomElasticJob,coordinatorRegistryCenter,configuration);
}
2) 修改作业任务
@Component
public class FileCustomElasticJob implements SimpleJob {@Autowiredprivate FileCustomServiceImpl fileCustomService;@Overridepublic void execute(ShardingContext shardingContext) {System.out.println("线程ID: " + Thread.currentThread().getId() + ";分片索引号:" + shardingContext.getShardingItem() + ";分片参数:" + shardingContext.getShardingParameter());dowWork(shardingContext.getShardingParameter());}private void dowWork(String shardingParameter) {QueryWrapper<FileCustom> query = new QueryWrapper<>();query.lambda().eq(FileCustom::getType,shardingParameter);List<FileCustom> fileCustomList = fileCustomService.list(query);for (FileCustom fileCustom : fileCustomList) {backWork(fileCustom);try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}}private void backWork(FileCustom fileCustom) {System.out.println("开始备份>>>>>");System.out.println("备份ID:" + fileCustom.getId());System.out.println("备份方法名:" + fileCustom.getName());System.out.println("备份类型:" + fileCustom.getType());System.out.println("备份结束<<<<<");}
}
运行结果:
3 DataFlow任务调度
4 任务管理
自动生成两张表