123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- // Copyright 2019 autocareai.com. All rights reserved.
- // Use of this source code is governed by autocareai.com.
- package pb
- import (
- "gadm-ods/common.in/clinit"
- "gadm-ods/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
- }
- func setupAdmTaskK8sClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%s", config.Conf.Rpc.AdmTask.ServiceIp,
- config.Conf.Rpc.AdmTask.ServicePort),
- 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(discoverType string) (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,
- }
- if discoverType == "etcd"{
- setupAdmTaskClient(kacp, conns)
- }else{
- setupAdmTaskK8sClient(kacp, conns)
- }
- return
- }
|