123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package pb
- import (
- "dust-monitor/parser"
- "fmt"
- "time"
- grpc "google.golang.org/grpc"
- "google.golang.org/grpc/keepalive"
- )
- // 客户端集合
- var SmartAlarm SmartAlarmClient
- var SmartAuth SmartAuthClient
- var SmartThirdparty SmartSiteThirdpartyClient
- var SmartProvincial SmartProvincialClient
- func setupSmartAlarmClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.SmartAlarm.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.SmartAlarm.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.SmartAlarm.ServicePort),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- SmartAlarm = NewSmartAlarmClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial cabinet conn err", err)
- }
- return
- }
- func setupSmartAuthClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.SmartAuth.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.SmartAuth.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.SmartAuth.ServicePort),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- SmartAuth = NewSmartAuthClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial cabinet conn err", err)
- }
- return
- }
- func setupSmartThirdpartyClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- 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 {
- SmartThirdparty = NewSmartSiteThirdpartyClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial cabinet conn err", err)
- }
- return
- }
- func setupSmartProvincialClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.SmartProvincial.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.SmartProvincial.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.SmartProvincial.ServicePort),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- SmartProvincial = NewSmartProvincialClient(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,
- }
- setupSmartAlarmClient(kacp, conns)
- setupSmartAuthClient(kacp, conns)
- setupSmartThirdpartyClient(kacp, conns)
- setupSmartProvincialClient(kacp, conns)
- return
- }
|