All Projects → wenweihu86 → Raft Java

wenweihu86 / Raft Java

Licence: apache-2.0
Raft Java implementation which is simple and easy to understand.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Raft Java

Braft
An industrial-grade C++ implementation of RAFT consensus algorithm based on brpc, widely used inside Baidu to build highly-available distributed systems.
Stars: ✭ 2,964 (+278.54%)
Mutual labels:  distributed-storage, raft, raft-consensus-algorithm
Sharkstore
distributed key - value persisted storage system
Stars: ✭ 165 (-78.93%)
Mutual labels:  distributed-storage, raft
Rafty
Implementation of RAFT consensus in .NET core
Stars: ✭ 182 (-76.76%)
Mutual labels:  raft, raft-consensus-algorithm
distmq
Distributed Message Queue based on Raft
Stars: ✭ 32 (-95.91%)
Mutual labels:  raft, distributed-storage
Cete
Cete is a distributed key value store server written in Go built on top of BadgerDB.
Stars: ✭ 153 (-80.46%)
Mutual labels:  raft, raft-consensus-algorithm
Mit 6.824 2018
Solutions to mit 6.824 2018
Stars: ✭ 158 (-79.82%)
Mutual labels:  raft, raft-consensus-algorithm
Dotnext
Next generation API for .NET
Stars: ✭ 379 (-51.6%)
Mutual labels:  raft, raft-consensus-algorithm
Copycat
A novel implementation of the Raft consensus algorithm
Stars: ✭ 551 (-29.63%)
Mutual labels:  raft, raft-consensus-algorithm
Kites
🪁 A consistency, partition tolerance completed distributed KV store, implementation of the Raft distributed consensus protocol and Kotlin.
Stars: ✭ 41 (-94.76%)
Mutual labels:  raft, raft-consensus-algorithm
distkv
Distributed KV Storage System based on Raft and RocksDB, can be use to store small files, like images.
Stars: ✭ 50 (-93.61%)
Mutual labels:  raft, distributed-storage
Dragonboat Example
Examples for Dragonboat
Stars: ✭ 104 (-86.72%)
Mutual labels:  raft, raft-consensus-algorithm
Dragonboat
Dragonboat is a high performance multi-group Raft consensus library in pure Go.
Stars: ✭ 3,983 (+408.68%)
Mutual labels:  distributed-storage, raft
Leto
A key value storage example powered by hashicorp raft and BadgerDB
Stars: ✭ 73 (-90.68%)
Mutual labels:  raft, raft-consensus-algorithm
Atomix
A reactive Java framework for building fault-tolerant distributed systems
Stars: ✭ 2,182 (+178.67%)
Mutual labels:  raft, raft-consensus-algorithm
6.824 2018
MIT 6.824 2018 lab. MIT6.824分布式系统(2018秋)
Stars: ✭ 59 (-92.46%)
Mutual labels:  raft, raft-consensus-algorithm
huffleraft
Replicated key-value store driven by the raft consensus protocol 🚵
Stars: ✭ 32 (-95.91%)
Mutual labels:  raft, raft-consensus-algorithm
raft
raft is a golang library that provides a simple, clean, and idiomatic implementation of the Raft consensus protocol
Stars: ✭ 35 (-95.53%)
Mutual labels:  raft, raft-consensus-algorithm
Ra
A Raft implementation for Erlang and Elixir that strives to be efficient and make it easier to use multiple Raft clusters in a single system.
Stars: ✭ 478 (-38.95%)
Mutual labels:  raft, raft-consensus-algorithm
Cortx
CORTX Community Object Storage is 100% open source object storage uniquely optimized for mass capacity storage devices.
Stars: ✭ 426 (-45.59%)
Mutual labels:  distributed-storage
Awesome Distributed Systems
Awesome list of distributed systems resources
Stars: ✭ 512 (-34.61%)
Mutual labels:  distributed-storage

raft-java

Raft implementation library for Java.
参考自Raft论文和Raft作者的开源实现LogCabin

支持的功能

  • leader选举
  • 日志复制
  • snapshot
  • 集群成员动态更变

Quick Start

在本地单机上部署一套3实例的raft集群,执行如下脚本:
cd raft-java-example && sh deploy.sh
该脚本会在raft-java-example/env目录部署三个实例example1、example2、example3;
同时会创建一个client目录,用于测试raft集群读写功能。
部署成功后,测试写操作,通过如下脚本: cd env/client
./bin/run_client.sh "list://127.0.0.1:8051,127.0.0.1:8052,127.0.0.1:8053" hello world
测试读操作命令:
./bin/run_client.sh "list://127.0.0.1:8051,127.0.0.1:8052,127.0.0.1:8053" hello

使用方法

下面介绍如何在代码中使用raft-java依赖库来实现一套分布式存储系统。

配置依赖(暂未发不到maven中央仓库,需要手动install到本地)

<dependency>
    <groupId>com.github.wenweihu86.raft</groupId>
    <artifactId>raft-java-core</artifactId>
    <version>1.9.0</version>
</dependency>

定义数据写入和读取接口

message SetRequest {
    string key = 1;
    string value = 2;
}
message SetResponse {
    bool success = 1;
}
message GetRequest {
    string key = 1;
}
message GetResponse {
    string value = 1;
}
public interface ExampleService {
    Example.SetResponse set(Example.SetRequest request);
    Example.GetResponse get(Example.GetRequest request);
}

服务端使用方法

  1. 实现状态机StateMachine接口实现类
// 该接口三个方法主要是给Raft内部调用
public interface StateMachine {
    /**
     * 对状态机中数据进行snapshot,每个节点本地定时调用
     * @param snapshotDir snapshot数据输出目录
     */
    void writeSnapshot(String snapshotDir);
    /**
     * 读取snapshot到状态机,节点启动时调用
     * @param snapshotDir snapshot数据目录
     */
    void readSnapshot(String snapshotDir);
    /**
     * 将数据应用到状态机
     * @param dataBytes 数据二进制
     */
    void apply(byte[] dataBytes);
}
  1. 实现数据写入和读取接口
// ExampleService实现类中需要包含以下成员
private RaftNode raftNode;
private ExampleStateMachine stateMachine;
// 数据写入主要逻辑
byte[] data = request.toByteArray();
// 数据同步写入raft集群
boolean success = raftNode.replicate(data, Raft.EntryType.ENTRY_TYPE_DATA);
Example.SetResponse response = Example.SetResponse.newBuilder().setSuccess(success).build();
// 数据读取主要逻辑,由具体应用状态机实现
Example.GetResponse response = stateMachine.get(request);
  1. 服务端启动逻辑
// 初始化RPCServer
RPCServer server = new RPCServer(localServer.getEndPoint().getPort());
// 应用状态机
ExampleStateMachine stateMachine = new ExampleStateMachine();
// 设置Raft选项,比如:
RaftOptions.snapshotMinLogSize = 10 * 1024;
RaftOptions.snapshotPeriodSeconds = 30;
RaftOptions.maxSegmentFileSize = 1024 * 1024;
// 初始化RaftNode
RaftNode raftNode = new RaftNode(serverList, localServer, stateMachine);
// 注册Raft节点之间相互调用的服务
RaftConsensusService raftConsensusService = new RaftConsensusServiceImpl(raftNode);
server.registerService(raftConsensusService);
// 注册给Client调用的Raft服务
RaftClientService raftClientService = new RaftClientServiceImpl(raftNode);
server.registerService(raftClientService);
// 注册应用自己提供的服务
ExampleService exampleService = new ExampleServiceImpl(raftNode, stateMachine);
server.registerService(exampleService);
// 启动RPCServer,初始化Raft节点
server.start();
raftNode.init();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].