Resolve the Servers
Distributed Services with Go — by Travis Jeffery (65 / 84)
👈 Make Servers Discoverable | TOC | Route and Balance Requests with Pickers 👉
The gRPC resolver we’ll write in this section will call the GetServers endpoint we made and pass its information to gRPC so that the picker knows what servers it can route requests to.
To start, create a new package for our resolver and picker code by running $ mkdir internal/loadbalance.
gRPC uses the builder pattern for resolvers and pickers, so each has a builder interface and an implementation interface. Because the builder interfaces have one simple method — Build — we’ll implement both interfaces with one type. Create a file named resolver.go in internal/loadbalance that begins with this code:
ClientSideServiceDiscovery/internal/loadbalance/resolver.go
package loadbalance
import (
"context"
"fmt"
"sync"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/attributes"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/serviceconfig"
api "github.com/travisjeffery/proglog/api/v1"
)
type Resolver struct {
mu sync.Mutex
clientConn resolver.ClientConn
resolverConn *grpc.ClientConn
serviceConfig *serviceconfig.ParseResult
logger *zap.Logger
}