setup.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "time"
  7. "smart-enterprise-management-gateway/parser"
  8. grpc "google.golang.org/grpc"
  9. "google.golang.org/grpc/keepalive"
  10. )
  11. // 客户端集合
  12. var Enterprise SmartSiteEnterpriseClient
  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.SmartEnterpriseManagement.ServiceName
  20. } else {
  21. serviceName = parser.Conf.Rpc.SmartEnterpriseManagement.ServiceIp
  22. }
  23. // 发起一个连接并记录连接conn,后期释放
  24. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  25. parser.Conf.Rpc.SmartEnterpriseManagement.ServicePort),
  26. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  27. Enterprise = NewSmartSiteEnterpriseClient(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. }