12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package pb
- import (
- fmt "fmt"
- "time"
- "smart-enterprise-management-gateway/parser"
- grpc "google.golang.org/grpc"
- "google.golang.org/grpc/keepalive"
- )
- // 客户端集合
- var Enterprise SmartSiteEnterpriseClient
- var Thirdparty SmartSiteThirdpartyClient
- var OperationLog SmartSiteLogClient
- func setupSmartSiteClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.SmartEnterpriseManagement.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.SmartEnterpriseManagement.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.SmartEnterpriseManagement.ServicePort),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- Enterprise = NewSmartSiteEnterpriseClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial cabinet conn err", err)
- }
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.SmartThirdparty.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.SmartThirdparty.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.SmartThirdparty.ServicePort),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- Thirdparty = NewSmartSiteThirdpartyClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial cabinet conn err", err)
- }
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.SmartLog.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.SmartLog.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.SmartLog.ServicePort),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- OperationLog = NewSmartSiteLogClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial cabinet conn err", err)
- }
- return
- }
- // SetupClients 创建客户端
- func SetupClients() (conns []*grpc.ClientConn) {
- // 客户端配置参数
- var 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,
- }
- setupSmartSiteClient(kacp, conns)
- return
- }
|