// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package pb import ( fmt "fmt" "time" "adm-gateway/etcd" "adm-gateway/parser" "go.etcd.io/etcd/client/v3/naming/resolver" grpc "google.golang.org/grpc" "google.golang.org/grpc/keepalive" ) // 客户端集合 var VehicleStyle VehicleStyleClient var Management ManagementClient func setupVehicleStyleClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) { // 根据是否为k8s来组装targets serviceName := parser.Conf.Rpc.ADMVehicleStyle.ServiceName builder, err := resolver.NewBuilder(etcd.Get()) if err != nil { panic(err) } // 发起一个连接并记录连接conn,后期释放 if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName, grpc.WithResolvers(builder), grpc.WithBalancerName("round_robin"), grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil { VehicleStyle = NewVehicleStyleClient(conn) conns = append(conns, conn) } else { fmt.Println("[rpc] dial cabinet conn err", err) } return } func setupManagementClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) { // 根据是否为k8s来组装targets serviceName := parser.Conf.Rpc.AdmManagement.ServiceName builder, err := resolver.NewBuilder(etcd.Get()) if err != nil { panic(err) } // 发起一个连接并记录连接conn,后期释放 if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName, grpc.WithResolvers(builder), grpc.WithBalancerName("round_robin"), grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil { Management = NewManagementClient(conn) conns = append(conns, conn) } else { fmt.Println("[rpc] dial cabinet conn err", err) } return } // SetupClients 创建客户端 func SetupClients() (conns []*grpc.ClientConn) { // 客户端配置参数 kacp := keepalive.ClientParameters{ // send pings every n seconds if there is no activity Time: time.Duration(parser.Conf.Rpc.Keepalive.ClientTime) * time.Second, // wait n second for ping ack before considering the connection dead Timeout: time.Duration(parser.Conf.Rpc.Keepalive.ClientTimeout) * time.Second, // send pings even without active streams PermitWithoutStream: true, } setupVehicleStyleClient(kacp, conns) setupManagementClient(kacp, conns) return }