高可用分布式调度框架 Mossrose

码农软件 · 软件分类 · 作业/任务调度 · 2019-09-04 13:27:16

软件介绍

Mossrose,高可用分布式调度框架

环境要求:

  • Zookeeper

  • Java 8

安装

<dependency>
  <groupId>com.jiuxian</groupId>
  <artifactId>mossrose</artifactId>
  <version>1.1.0-RELEASE</version>
</dependency>

核心概念

  • SimpleJob

    • 简单任务

  • DistributedJob

    • 分布式任务,通过slice()方法将作业分隔成多个子任务,子任务在集群内分布执行

  • MossroseProcess

    • 多个MossroseProcess组成集群,集群保证有且只有一个节点竞选成为主节点,主节点负责触发作业;所有节点都是工作节点,主节点触发的任务会在所有工作节点上分布执行

  • MossroseConfig

    • Mossrose配置,包括集群元信息和任务元信息

快速上手

实现一个简单任务

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);
    }
}

本文地址:https://codercto.com/soft/d/13858.html

Just My Type

Just My Type

Simon Garfield / Profile Books / 2010-10-21 / GBP 14.99

What's your type? Suddenly everyone's obsessed with fonts. Whether you're enraged by Ikea's Verdanagate, want to know what the Beach Boys have in common with easy Jet or why it's okay to like Comic Sa......一起来看看 《Just My Type》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具