Mossrose, 高可用分布式调度框架
环境要求:
Zookeeper
Java 8
安装
<dependency> <groupId>com.jiuxian</groupId> <artifactId>mossrose</artifactId> <version>1.1.0-RELEASE</version> </dependency>
核心概念
SimpleJob
DistributedJob
MossroseProcess
MossroseConfig
快速上手
实现一个简单任务
public class SomeJob implements SimpleJob { @Override public void execute() { System.out.println("SimpleJob: " + UUID.randomUUID()); } }
配置任务 - mossrose.yaml
# Mossrose config info --- cluster: name: mossrose-example # 集群命名空间 loadBalancingMode: ROUND_ROBIN # 集群负载均衡策略,可选:ROUND_ROBIN/RANDOM jobs: - id: 1 # 作业ID group: test # 作业分组(可选) cron: 0/5 * * * * ? # 作业cron表达式 runInCluster: true # 是否在集群中分布执行,如果为false,则只在主节点上执行 main: com.jiuxian.mossrose.test.SomeJob # 作业类全名
运行mossrose主类
public class MainTest { @Test public void test() throws Exception { String zks = "localhost"; // zookeeper集群地址 try (MossroseProcess process = new MossroseProcess( MossroseConfigFactory.fromClasspathYamlFile("mossrose.yaml"), new ZookeeperClusterDiscovery("/mossrose/jobtest", zks), zks)) { process.run(); try { // Block the unit test Thread.sleep(60 * 60 * 1000); } catch (InterruptedException e) { } } } }
分布式任务
实现一个分布式任务
public class SomeDistributedJob implements DistributedJob { @Override public List slice() { return Splitter.on(" ").splitToList("This is a test on the mossrose distributed job, how are you feeling?"); } @Override public void execute(String item) { System.out.println(Thread.currentThread() + " DistributedJob: " + item); } }