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