setup.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package pb
  4. import (
  5. fmt "fmt"
  6. "smart-site-management-gateway/parser"
  7. "time"
  8. grpc "google.golang.org/grpc"
  9. "google.golang.org/grpc/keepalive"
  10. )
  11. // 客户端集合
  12. var Project SmartSiteProjectClient
  13. var Thirdparty SmartSiteThirdpartyClient
  14. var OperationLog SmartSiteLogClient
  15. func setupSmartSiteClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  16. // 根据是否为k8s来组装targets
  17. var serviceName string
  18. if parser.Conf.K8s {
  19. serviceName = parser.Conf.Rpc.SmartSiteManagement.ServiceName
  20. } else {
  21. serviceName = parser.Conf.Rpc.SmartSiteManagement.ServiceIp
  22. }
  23. // 发起一个连接并记录连接conn,后期释放
  24. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  25. parser.Conf.Rpc.SmartSiteManagement.ServicePort),
  26. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  27. Project = NewSmartSiteProjectClient(conn)
  28. conns = append(conns, conn)
  29. } else {
  30. fmt.Println("[rpc] dial cabinet conn err", err)
  31. }
  32. if parser.Conf.K8s {
  33. serviceName = parser.Conf.Rpc.SmartThirdparty.ServiceName
  34. } else {
  35. serviceName = parser.Conf.Rpc.SmartThirdparty.ServiceIp
  36. }
  37. // 发起一个连接并记录连接conn,后期释放
  38. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  39. parser.Conf.Rpc.SmartThirdparty.ServicePort),
  40. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  41. Thirdparty = NewSmartSiteThirdpartyClient(conn)
  42. conns = append(conns, conn)
  43. } else {
  44. fmt.Println("[rpc] dial cabinet conn err", err)
  45. }
  46. if parser.Conf.K8s {
  47. serviceName = parser.Conf.Rpc.SmartLog.ServiceName
  48. } else {
  49. serviceName = parser.Conf.Rpc.SmartLog.ServiceIp
  50. }
  51. // 发起一个连接并记录连接conn,后期释放
  52. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  53. parser.Conf.Rpc.SmartLog.ServicePort),
  54. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  55. OperationLog = NewSmartSiteLogClient(conn)
  56. conns = append(conns, conn)
  57. } else {
  58. fmt.Println("[rpc] dial cabinet conn err", err)
  59. }
  60. return
  61. }
  62. // SetupClients 创建客户端
  63. func SetupClients() (conns []*grpc.ClientConn) {
  64. // 客户端配置参数
  65. var kacp = keepalive.ClientParameters{
  66. // send pings every n seconds if there is no activity
  67. Time: time.Duration(parser.Conf.Rpc.Keepalive.ClientTime) * time.Second,
  68. // wait n second for ping ack before considering the connection dead
  69. Timeout: time.Duration(parser.Conf.Rpc.Keepalive.ClientTimeout) * time.Second,
  70. // send pings even without active streams
  71. PermitWithoutStream: true,
  72. }
  73. setupSmartSiteClient(kacp, conns)
  74. return
  75. }