Client-side service discovery

In client‑side discovery, the client is responsible for determining the endpoints of available service instances and then routes requests to them. The client queries a service registry, which is a database of available service instances, and then routes requests to an instance that it feels is the best option. This could be as simple as a round-robin load balancing algorithm or a more complex one, which takes things like server instance network round-trip-time and so on. Every server instance connects to the service registry on startup. It also periodically updates its health status with the registry. The architecture is described in the following diagram:

As you can see, we need a special registry-aware HTTP client, which can manage connections to multiple server instances and route requests.

Netflix Eureka is a service registry in the Netflix OSS stack, and Netflix Ribbon is an IPC client, written in Java, that works with Eureka to load balance requests across service instances. Non-Java apps can interact with Eureka using its REST API. In Golang, https://github.com/hudl/fargo is a client that does this. It can register an instance with Eureka, enable check instance health (using heartbeats), and also query Eureka for the set of instances for an application. The load balancing algorithm currently implemented is random. To connect to Eureka and get a list of apps and instances, use the following:

c = fargo.NewConn("http://127.0.0.1:8080/eureka/v2") 
c.GetApps() // returns a map[String]fargo.Application 

A service instance can register its help with Eureka using the following:

c.UpdateApp(&app) 

Consul (https://www.consul.io/) is another open source service discovery platform. It organizes services in a service catalog and provides a DNS and HTTP API interface on top of it. It monitors registered service instances and manages a healthy set for each service. The Catalog.Services method (https://godoc.org/github.com/hashicorp/consul/api#Catalog.Services) can be used to query the instances for a service as described here: https://www.consul.io/api/catalog.html. The QueryOption parameter can be used to tweak the nodes the client is interested in, for example using QueryOptions{Near: "_agent"} will result in Consul returning the closest node to the client first (closed in terms of least network latency).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset