配置手册:
http://elasticjob.io/docs/elastic-job-lite/02-guide/config-manual/
1、ZK注册中心配置(后面继续分析)
2、作业配置(从底层往上层:Core——Type——Lite)
配置级别 | 配置类 | 配置内容 |
---|---|---|
Core | JobCoreConfiguration | 用于提供作业核心配置信息,如:作业名称、CRON 表达式、分片总数等。 |
Type | JobTypeConfiguration | 有3 个子类分别对应SIMPLE, DATAFLOW 和SCRIPT 类型作业,提供3 种作 业需要的不同配置,如:DATAFLOW 类型是否流式处理或SCRIPT 类型的命 令行等。Simple 和DataFlow 需要指定任务类的路径。 |
Root | JobRootConfiguration | 有2 个子类分别对应Lite 和Cloud 部署类型,提供不同部署类型所需的配 置,如:Lite 类型的是否需要覆盖本地配置或Cloud 占用CPU 或Memory 数量等。 可以定义分片策略。 http://elasticjob.io/docs/elastic-job-lite/02-guide/job-sharding-strategy/ |
public class SimpleJobTest {
// TODO 如果修改了代码,跑之前清空ZK
public static void main(String[] args) {
// ZK注册中心
CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration("localhost:2181", "ejob-standalone"));
regCenter.init();
// 数据源,使用DBCP
/* BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/elastic_job_log");
dataSource.setUsername("root");
dataSource.setPassword("123456");
JobEventConfiguration jobEventConfig = new JobEventRdbConfiguration(dataSource);*/
// 定义作业核心配置
// TODO 如果修改了代码,跑之前清空ZK
JobCoreConfiguration coreConfig = JobCoreConfiguration.newBuilder("MySimpleJob", "0/2 * * * * ?", 4).shardingItemParameters("0=RDP, 1=CORE, 2=SIMS, 3=ECIF").failover(true).build();
// 定义SIMPLE类型配置
SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(coreConfig, MySimpleJob.class.getCanonicalName());
// 作业分片策略
// 基于平均分配算法的分片策略
String jobShardingStrategyClass = AverageAllocationJobShardingStrategy.class.getCanonicalName();
// 定义Lite作业根配置
// LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).jobShardingStrategyClass(jobShardingStrategyClass).build();
LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).build();
// 构建Job
new JobScheduler(regCenter, simpleJobRootConfig).init();
// new JobScheduler(regCenter, simpleJobRootConfig, jobEventConfig).init();
}
}
作业配置分为 3级,分别是 JobCoreConfiguration,JobTypeConfiguration和LiteJobConfiguration。 LiteJobConfiguration使用 JobTypeConfiguration,JobTypeConfiguration使用 Job CoreConfiguration,层层嵌套。
JobTypeConfiguration根据不同实现类型分为 SimpleJobConfiguration,DataflowJobConfiguration和ScriptJobConfiguration。
E-Job使用 ZK来做分布式协调,所有的配置都会写入到 ZK节点。