Implement Raft in Our Service

We have a log that can write and read records on one computer. We want a distributed log that’s replicated on multiple computers, so let’s implement Raft in our service to get that.

Install Raft by running this command:

 $ go get github.com/hashicorp/raft@v1.1.1
 $ ​# use etcd's fork of Ben Johnson's Bolt key/value store,
 $ ​# which includes fixes for Go 1.14+
 $ go mod edit -replace github.com/hashicorp/raft-boltdb=​\
 github.com/travisjeffery/raft-boltdb@v1.0.0

In the internal/log directory, create a distributed.go file, beginning with this snippet:

 package​ log
 
 import​ (
 "bytes"
 "crypto/tls"
 "fmt"
 "io"
 "net"
 "os"
 "path/filepath" ...

Get Distributed Services with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.