setup.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package pb
  4. import (
  5. "access-control-monitor/parser"
  6. fmt "fmt"
  7. "time"
  8. grpc "google.golang.org/grpc"
  9. "google.golang.org/grpc/keepalive"
  10. )
  11. // 客户端集合
  12. var Auth SmartAuthClient
  13. var Alarm SmartAlarmClient
  14. var Thirdparty SmartSiteThirdpartyClient
  15. var Provincial SmartProvincialClient
  16. func setupSmartAuthClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  17. // 根据是否为k8s来组装targets
  18. var serviceName string
  19. if parser.Conf.K8s {
  20. serviceName = parser.Conf.Rpc.SmartAuth.ServiceName
  21. } else {
  22. serviceName = parser.Conf.Rpc.SmartAuth.ServiceIp
  23. }
  24. // 发起一个连接并记录连接conn,后期释放
  25. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  26. parser.Conf.Rpc.SmartAuth.ServicePort),
  27. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  28. Auth = NewSmartAuthClient(conn)
  29. conns = append(conns, conn)
  30. } else {
  31. fmt.Println("[rpc] dial cabinet conn err", err)
  32. }
  33. return
  34. }
  35. func setupSmartAlarmClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  36. // 根据是否为k8s来组装targets
  37. var serviceName string
  38. if parser.Conf.K8s {
  39. serviceName = parser.Conf.Rpc.SmartAlarm.ServiceName
  40. } else {
  41. serviceName = parser.Conf.Rpc.SmartAlarm.ServiceIp
  42. }
  43. // 发起一个连接并记录连接conn,后期释放
  44. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  45. parser.Conf.Rpc.SmartAlarm.ServicePort),
  46. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  47. Alarm = NewSmartAlarmClient(conn)
  48. conns = append(conns, conn)
  49. } else {
  50. fmt.Println("[rpc] dial cabinet conn err", err)
  51. }
  52. return
  53. }
  54. func setupSmartThirdpartyClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  55. // 根据是否为k8s来组装targets
  56. var serviceName string
  57. if parser.Conf.K8s {
  58. serviceName = parser.Conf.Rpc.SmartThirdparty.ServiceName
  59. } else {
  60. serviceName = parser.Conf.Rpc.SmartThirdparty.ServiceIp
  61. }
  62. // 发起一个连接并记录连接conn,后期释放
  63. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  64. parser.Conf.Rpc.SmartThirdparty.ServicePort),
  65. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  66. Thirdparty = NewSmartSiteThirdpartyClient(conn)
  67. conns = append(conns, conn)
  68. } else {
  69. fmt.Println("[rpc] dial thirdparty conn err", err)
  70. }
  71. return
  72. }
  73. func setupSmartProvincialClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  74. // 根据是否为k8s来组装targets
  75. var serviceName string
  76. if parser.Conf.K8s {
  77. serviceName = parser.Conf.Rpc.SmartProvincial.ServiceName
  78. } else {
  79. serviceName = parser.Conf.Rpc.SmartProvincial.ServiceIp
  80. }
  81. // 发起一个连接并记录连接conn,后期释放
  82. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  83. parser.Conf.Rpc.SmartProvincial.ServicePort),
  84. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  85. Provincial = NewSmartProvincialClient(conn)
  86. conns = append(conns, conn)
  87. } else {
  88. fmt.Println("[rpc] dial provincial conn err", err)
  89. }
  90. return
  91. }
  92. // SetupClients 创建客户端
  93. func SetupClients() (conns []*grpc.ClientConn) {
  94. // 客户端配置参数
  95. var kacp = keepalive.ClientParameters{
  96. // send pings every n seconds if there is no activity
  97. Time: time.Duration(parser.Conf.Rpc.Keepalive.ClientTime) * time.Second,
  98. // wait n second for ping ack before considering the connection dead
  99. Timeout: time.Duration(parser.Conf.Rpc.Keepalive.ClientTimeout) * time.Second,
  100. // send pings even without active streams
  101. PermitWithoutStream: true,
  102. }
  103. setupSmartAuthClient(kacp, conns)
  104. setupSmartAlarmClient(kacp, conns)
  105. setupSmartThirdpartyClient(kacp, conns)
  106. setupSmartProvincialClient(kacp, conns)
  107. return
  108. }