This is a simple KV Server (Key/Value) server using raft consensus algorithm. (implement by CoreOS/etcd).
It provide a basic RPC Client/Server for K/V(Key Value) storage service.
Raft is a consensus algorithm that is designed to be easy to understand. It's equivalent to Paxos in fault-tolerance and performance. The difference is that it's decomposed into relatively independent subproblems, and it cleanly addresses all major pieces needed for practical systems. We hope Raft will make consensus available to a wider audience, and that this wider audience will be able to develop a variety of higher quality consensus-based systems than are available today. (quote from here)
- Refer code from raftexample
- Get file listener.go, kvstore.go, raft.go.
- Do your modification for your usage.
raft.transport
need an extra http port for raft message exchange. MUST add this in your code. (which is peer info in example code)
go get github.com/kkdai/raftrpc
package main
import (
"fmt"
. "github.com/kkdai/raftrpc"
)
func main() {
forever := make(chan int)
//RPC addr
rpcAddr := "127.0.0.1:1234"
srv := StartServer(rpcAddr, 1)
<-forever
}
package main
import (
"fmt"
. "github.com/kkdai/raftrpc"
)
func main() {
forever := make(chan int)
//Note there are two address and port.
//
// "127.0.0.1:1234" is RPC access point
// "http://127.0.0.1:12379" is raft message access point which use http
var raftMsgSrvList []string
raftMsgSrvList = append(raftMsgSrvList, "http://127.0.0.1:12379")
raftMsgSrvList = append(raftMsgSrvList, "http://127.0.0.1:22379")
srv1 := StartClusterServers("127.0.0.1:1234", 1, raftMsgSrvList)
srv2 := StartClusterServers("127.0.0.1:1235", 2, raftMsgSrvList)
<-forever
}
Assume a server exist on 127.0.0.1:1234
.
package main
import (
"fmt"
. "github.com/kkdai/raftrpc"
)
func main() {
client := MakeClerk("127.0.0.1:1234")
client.Put("t1", "v1")
log.Println("got:", ret)
if ret != "v1" {
log.Println("Client get error:", ret)
}
}
It is one of my project 52.
etcd is under the Apache 2.0 license. See the LICENSE file for details.