Discover Services with Serf

Distributed Services with Go — by Travis Jeffery (52 / 84)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Embed Service Discovery | TOC | Request Discovered Services and Re plicate Logs 👉

Serf maintains cluster membership by using an efficient, lightweight gossip protocol to communicate between the service’s nodes. Unlike service registry projects like ZooKeeper and Consul, Serf doesn’t have a central-registry architectural style. Instead, each instance of your service in the cluster runs as a Serf node. These nodes exchange messages with each other in the same way a zombie apocalypse might occur: one infected zombie soon spreads to infect everyone else. With Serf, instead of a spreading zombie virus, you’re spreading information about the nodes in your cluster. You listen to Serf for messages about changes in the cluster and then handle them accordingly.

To implement service discovery with Serf we need to:

  1. Create a Serf node on each server.
  2. Configure each Serf node with an address to listen on and accept connections from other Serf nodes.
  3. Configure each Serf node with addresses of other Serf nodes and join their cluster.
  4. Handle Serf’s cluster discovery events, such as when a node joins or fails in the cluster.

Let’s get coding.

Serf is a lightweight tool that you can use for infinite use cases, but its API can be verbose when you have a specific problem to solve…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.