Understanding clients
In Chapter 2, Getting Started with Microservices, we discussed the client library principle of microservices. A microservice that talks to another microservice ideally utilizes a client library that's exposed through an interface. Go kit provides excellent support and guidelines for writing such client libraries. The using microservice simply receives an interface. It is actually totally agnostic to the fact it is talking to another service. For (almost) all intents and purposes, the remote service could be running in the same process. This is excellent for testing or for refactoring services and breaking a slightly too large service into two separate services.
Go kit has client endpoints that are similar to service endpoints but work in the opposite direction. Service endpoints decode requests, delegate work to the service, and encode responses. Client endpoints encode requests, invoke the remote service over the network, and decode the response.
Here is what the Follow() method of the client looks like:
func (s EndpointSet) Follow(followed string, follower string) (err error) {
resp, err := s.FollowEndpoint(context.Background(), FollowRequest{Followed: followed, Follower: follower})
if err != nil {
return err
}
response := resp.(SimpleResponse)
if response.Err != "" {
err = errors.New(response.Err)
}
return
}