123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package clientImpl
- import (
- "../constants"
- "google.golang.org/grpc/resolver"
- )
- // resolver工厂实现类
- type ExampleResolverBuilder struct{}
- func (*ExampleResolverBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
- r := &exampleResolver{
- target: target,
- cc: cc,
- addressStore: map[string][]string{
- constants.ExampleServiceName: constants.ExampleEndpoints, // 服务命名与实际服务终结点列表的对应关系 "go-example": ["localhost:50051", "localhost:50052"]
- },
- }
- r.start()
- return r, nil
- }
- func (*ExampleResolverBuilder) Scheme() string { return constants.ExampleScheme } // "grpc"
- // resolver实现类
- type exampleResolver struct {
- target resolver.Target
- cc resolver.ClientConn
- addressStore map[string][]string
- }
- func (r *exampleResolver) start() {
- addrList := r.addressStore[r.target.Endpoint]
- addresses := make([]resolver.Address, len(addrList))
- for i, s := range addrList {
- addresses[i] = resolver.Address{Addr: s}
- }
- r.cc.UpdateState(resolver.State{Addresses: addresses})
- }
- func (*exampleResolver) ResolveNow(resolver.ResolveNowOptions) {}
- func (*exampleResolver) Close() {}
|