go-zero/rpcx/internal/discovclient.go

36 lines
708 B
Go
Raw Normal View History

2020-08-04 11:59:39 +08:00
package internal
import (
2020-08-06 20:55:38 +08:00
"fmt"
"strings"
"zero/rpcx/internal/balancer/p2c"
2020-08-04 11:59:39 +08:00
"zero/rpcx/internal/resolver"
"google.golang.org/grpc"
)
2020-08-06 20:55:38 +08:00
func init() {
resolver.RegisterResolver()
}
2020-08-04 11:59:39 +08:00
type DiscovClient struct {
conn *grpc.ClientConn
}
2020-08-06 20:55:38 +08:00
func NewDiscovClient(endpoints []string, key string, opts ...ClientOption) (*DiscovClient, error) {
opts = append(opts, WithDialOption(grpc.WithBalancerName(p2c.Name)))
target := fmt.Sprintf("%s://%s/%s", resolver.DiscovScheme,
strings.Join(endpoints, resolver.EndpointSep), key)
conn, err := dial(target, opts...)
2020-08-04 11:59:39 +08:00
if err != nil {
return nil, err
}
2020-08-06 20:55:38 +08:00
return &DiscovClient{conn: conn}, nil
2020-08-04 11:59:39 +08:00
}
2020-08-06 20:55:38 +08:00
func (c *DiscovClient) Conn() *grpc.ClientConn {
return c.conn
2020-08-04 11:59:39 +08:00
}