代码拉取完成,页面将自动刷新
package micro
import (
"context"
"fmt"
"gitee.com/mosache/YFrame/micro/registry"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"time"
)
type Client struct {
r registry.Registry
insecure bool
balancer string // lb name
}
type ClientOption func(c *Client)
func ClientWithInsecure(insecure bool) ClientOption {
return func(c *Client) {
c.insecure = insecure
}
}
func ClientWithRegistry(r registry.Registry) ClientOption {
return func(c *Client) {
c.r = r
}
}
func ClientWithBalancer(balancer string) ClientOption {
return func(c *Client) {
c.balancer = balancer
}
}
func NewClient(opts ...ClientOption) (*Client, error) {
c := &Client{insecure: false}
for _, opt := range opts {
opt(c)
}
return c, nil
}
func (c *Client) Dial(ctx context.Context, serviceName string) (*grpc.ClientConn, error) {
dialOpts := make([]grpc.DialOption, 0)
if c.r != nil {
builder := NewRegisterBuilder(c.r, GrpcResolverBuilderWithTimeOut(time.Second*3))
dialOpts = append(dialOpts, grpc.WithResolvers(builder))
}
if c.insecure {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
/// 用了lb
if len(c.balancer) > 0 {
dialOpts = append(dialOpts, grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy":"%s"}`, c.balancer)))
}
grpcClient, err := grpc.DialContext(ctx, fmt.Sprintf("discovery:///%s", serviceName),
dialOpts...)
if err != nil {
return nil, err
}
return grpcClient, err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。