setup.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. "property-task/parser"
  7. "time"
  8. "go.etcd.io/etcd/client/v3/naming/resolver"
  9. grpc "google.golang.org/grpc"
  10. "google.golang.org/grpc/keepalive"
  11. )
  12. // 客户端集合
  13. var System SystemClient
  14. var Garden GardenClient
  15. var Thirdparty PropertyThirdpartyClient
  16. func setupSystemClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  17. // 根据是否为k8s来组装targets
  18. var serviceName string
  19. if parser.Conf.K8s {
  20. serviceName = parser.Conf.Rpc.System.ServiceName
  21. } else {
  22. serviceName = parser.Conf.Rpc.System.ServiceIp
  23. }
  24. // 发起一个连接并记录连接conn,后期释放
  25. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  26. parser.Conf.Rpc.System.ServicePort),
  27. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  28. System = NewSystemClient(conn)
  29. conns = append(conns, conn)
  30. } else {
  31. fmt.Println("[rpc] dial system conn err", err)
  32. }
  33. return
  34. }
  35. func setupGardenClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  36. // 根据是否为k8s来组装targets
  37. var serviceName string
  38. if parser.Conf.K8s {
  39. serviceName = parser.Conf.Rpc.Garden.ServiceName
  40. } else {
  41. serviceName = parser.Conf.Rpc.Garden.ServiceIp
  42. }
  43. // 发起一个连接并记录连接conn,后期释放
  44. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  45. parser.Conf.Rpc.Garden.ServicePort),
  46. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  47. Garden = NewGardenClient(conn)
  48. conns = append(conns, conn)
  49. } else {
  50. fmt.Println("[rpc] dial Garden conn err", err)
  51. }
  52. return
  53. }
  54. func setupThirdpartyClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  55. // 根据是否为k8s来组装targets
  56. var serviceName string
  57. if parser.Conf.K8s {
  58. serviceName = parser.Conf.Rpc.Thirdparty.ServiceName
  59. } else {
  60. serviceName = parser.Conf.Rpc.Thirdparty.ServiceIp
  61. }
  62. // 发起一个连接并记录连接conn,后期释放
  63. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  64. parser.Conf.Rpc.Thirdparty.ServicePort),
  65. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  66. Thirdparty = NewPropertyThirdpartyClient(conn)
  67. conns = append(conns, conn)
  68. } else {
  69. fmt.Println("[rpc] dial thirdparty conn err", err)
  70. }
  71. return
  72. }
  73. func setupSystemClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  74. // 根据是否为k8s来组装targets
  75. var serviceName string
  76. serviceName = parser.Conf.Rpc.System.ServiceName
  77. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  78. if err != nil {
  79. panic(err)
  80. }
  81. // 发起一个连接并记录连接conn,后期释放
  82. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  83. grpc.WithResolvers(builder),
  84. grpc.WithBalancerName("round_robin"),
  85. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  86. System = NewSystemClient(conn)
  87. conns = append(conns, conn)
  88. } else {
  89. fmt.Println("[rpc] dial system conn err", err)
  90. }
  91. return
  92. }
  93. func setupGardenClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  94. // 根据是否为k8s来组装targets
  95. var serviceName string
  96. serviceName = parser.Conf.Rpc.Garden.ServiceName
  97. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  98. if err != nil {
  99. panic(err)
  100. }
  101. // 发起一个连接并记录连接conn,后期释放
  102. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  103. grpc.WithResolvers(builder),
  104. grpc.WithBalancerName("round_robin"),
  105. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  106. Garden = NewGardenClient(conn)
  107. conns = append(conns, conn)
  108. } else {
  109. fmt.Println("[rpc] dial Garden conn err", err)
  110. }
  111. return
  112. }
  113. func setupThirdpartyClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  114. // 根据是否为k8s来组装targets
  115. var serviceName string
  116. serviceName = parser.Conf.Rpc.Thirdparty.ServiceName
  117. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  118. if err != nil {
  119. panic(err)
  120. }
  121. // 发起一个连接并记录连接conn,后期释放
  122. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  123. grpc.WithResolvers(builder),
  124. grpc.WithBalancerName("round_robin"),
  125. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  126. Thirdparty = NewPropertyThirdpartyClient(conn)
  127. conns = append(conns, conn)
  128. } else {
  129. fmt.Println("[rpc] dial Thirdparty conn err", err)
  130. }
  131. return
  132. }
  133. // SetupClients 创建客户端
  134. func SetupClients() (conns []*grpc.ClientConn) {
  135. // 客户端配置参数
  136. var kacp = keepalive.ClientParameters{
  137. // send pings every n seconds if there is no activity
  138. Time: time.Duration(parser.Conf.Rpc.Keepalive.ClientTime) * time.Second,
  139. // wait n second for ping ack before considering the connection dead
  140. Timeout: time.Duration(parser.Conf.Rpc.Keepalive.ClientTimeout) * time.Second,
  141. // send pings even without active streams
  142. PermitWithoutStream: true,
  143. }
  144. if len(parser.Conf.EtcdAddrs) == 0 {
  145. setupSystemClient(kacp, conns)
  146. setupThirdpartyClient(kacp, conns)
  147. setupGardenClient(kacp, conns)
  148. return
  149. }
  150. setupSystemClient2(kacp, conns)
  151. setupThirdpartyClient2(kacp, conns)
  152. setupGardenClient2(kacp, conns)
  153. return
  154. }