setup.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package pb
  4. import (
  5. fmt "fmt"
  6. "property-household/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 Garden GardenClient
  14. var Thirdparty PropertyThirdpartyClient
  15. var System SystemClient
  16. var Device DeviceClient
  17. var Common CommonClient
  18. func setupThirdpartyClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  19. // 根据是否为k8s来组装targets
  20. var serviceName string
  21. if parser.Conf.K8s {
  22. serviceName = parser.Conf.Rpc.Thirdparty.ServiceName
  23. } else {
  24. serviceName = parser.Conf.Rpc.Thirdparty.ServiceIp
  25. }
  26. // 发起一个连接并记录连接conn,后期释放
  27. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  28. parser.Conf.Rpc.Thirdparty.ServicePort),
  29. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  30. Thirdparty = NewPropertyThirdpartyClient(conn)
  31. conns = append(conns, conn)
  32. } else {
  33. fmt.Println("[rpc] dial thirdparty conn err", err)
  34. }
  35. return
  36. }
  37. func setupSystemClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  38. // 根据是否为k8s来组装targets
  39. var serviceName string
  40. if parser.Conf.K8s {
  41. serviceName = parser.Conf.Rpc.System.ServiceName
  42. } else {
  43. serviceName = parser.Conf.Rpc.System.ServiceIp
  44. }
  45. // 发起一个连接并记录连接conn,后期释放
  46. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  47. parser.Conf.Rpc.System.ServicePort),
  48. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  49. System = NewSystemClient(conn)
  50. conns = append(conns, conn)
  51. } else {
  52. fmt.Println("[rpc] dial system conn err", err)
  53. }
  54. return
  55. }
  56. func setupGardenClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  57. // 根据是否为k8s来组装targets
  58. var serviceName string
  59. if parser.Conf.K8s {
  60. serviceName = parser.Conf.Rpc.Garden.ServiceName
  61. } else {
  62. serviceName = parser.Conf.Rpc.Garden.ServiceIp
  63. }
  64. // 发起一个连接并记录连接conn,后期释放
  65. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  66. parser.Conf.Rpc.Garden.ServicePort),
  67. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  68. Garden = NewGardenClient(conn)
  69. conns = append(conns, conn)
  70. } else {
  71. fmt.Println("[rpc] dial garden conn err", err)
  72. }
  73. return
  74. }
  75. func setupSystemClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  76. // 根据是否为k8s来组装targets
  77. var serviceName string
  78. serviceName = parser.Conf.Rpc.System.ServiceName
  79. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  80. if err != nil {
  81. panic(err)
  82. }
  83. // 发起一个连接并记录连接conn,后期释放
  84. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  85. grpc.WithResolvers(builder),
  86. grpc.WithBalancerName("round_robin"),
  87. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  88. System = NewSystemClient(conn)
  89. conns = append(conns, conn)
  90. } else {
  91. fmt.Println("[rpc] dial system conn err", err)
  92. }
  93. return
  94. }
  95. func setupDeviceClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  96. // 根据是否为k8s来组装targets
  97. var serviceName string
  98. serviceName = parser.Conf.Rpc.Device.ServiceName
  99. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  100. if err != nil {
  101. panic(err)
  102. }
  103. // 发起一个连接并记录连接conn,后期释放
  104. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  105. grpc.WithResolvers(builder),
  106. grpc.WithBalancerName("round_robin"),
  107. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  108. Device = NewDeviceClient(conn)
  109. conns = append(conns, conn)
  110. } else {
  111. fmt.Println("[rpc] dial Device conn err", err)
  112. }
  113. return
  114. }
  115. func setupGardenClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  116. // 根据是否为k8s来组装targets
  117. var serviceName string
  118. serviceName = parser.Conf.Rpc.Garden.ServiceName
  119. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  120. if err != nil {
  121. panic(err)
  122. }
  123. // 发起一个连接并记录连接conn,后期释放
  124. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  125. grpc.WithResolvers(builder),
  126. grpc.WithBalancerName("round_robin"),
  127. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  128. Garden = NewGardenClient(conn)
  129. conns = append(conns, conn)
  130. } else {
  131. fmt.Println("[rpc] dial Garden conn err", err)
  132. }
  133. return
  134. }
  135. func setupThirdpartyClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  136. // 根据是否为k8s来组装targets
  137. var serviceName string
  138. serviceName = parser.Conf.Rpc.Thirdparty.ServiceName
  139. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  140. if err != nil {
  141. panic(err)
  142. }
  143. // 发起一个连接并记录连接conn,后期释放
  144. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  145. grpc.WithResolvers(builder),
  146. grpc.WithBalancerName("round_robin"),
  147. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  148. Thirdparty = NewPropertyThirdpartyClient(conn)
  149. conns = append(conns, conn)
  150. } else {
  151. fmt.Println("[rpc] dial Thirdparty conn err", err)
  152. }
  153. return
  154. }
  155. func setupCommonClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  156. // 根据是否为k8s来组装targets
  157. var serviceName string
  158. serviceName = parser.Conf.Rpc.Common.ServiceName
  159. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  160. if err != nil {
  161. panic(err)
  162. }
  163. // 发起一个连接并记录连接conn,后期释放
  164. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  165. grpc.WithResolvers(builder),
  166. grpc.WithBalancerName("round_robin"),
  167. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  168. Common = NewCommonClient(conn)
  169. conns = append(conns, conn)
  170. } else {
  171. fmt.Println("[rpc] dial common conn err", err)
  172. }
  173. return
  174. }
  175. func setupDeviceClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  176. // 根据是否为k8s来组装targets
  177. var serviceName string
  178. if parser.Conf.K8s {
  179. serviceName = parser.Conf.Rpc.Device.ServiceName
  180. } else {
  181. serviceName = parser.Conf.Rpc.Device.ServiceIp
  182. }
  183. // 发起一个连接并记录连接conn,后期释放
  184. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  185. parser.Conf.Rpc.Device.ServicePort),
  186. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  187. Device = NewDeviceClient(conn)
  188. conns = append(conns, conn)
  189. } else {
  190. fmt.Println("[rpc] dial device conn err", err)
  191. }
  192. return
  193. }
  194. func setupCommonClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  195. // 根据是否为k8s来组装targets
  196. var serviceName string
  197. if parser.Conf.K8s {
  198. serviceName = parser.Conf.Rpc.Common.ServiceName
  199. } else {
  200. serviceName = parser.Conf.Rpc.Common.ServiceIp
  201. }
  202. // 发起一个连接并记录连接conn,后期释放
  203. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  204. parser.Conf.Rpc.Common.ServicePort),
  205. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  206. Common = NewCommonClient(conn)
  207. conns = append(conns, conn)
  208. } else {
  209. fmt.Println("[rpc] dial Common conn err", err)
  210. }
  211. return
  212. }
  213. // SetupClients 创建客户端
  214. func SetupClients() (conns []*grpc.ClientConn) {
  215. // 客户端配置参数
  216. var kacp = keepalive.ClientParameters{
  217. // send pings every n seconds if there is no activity
  218. Time: time.Duration(parser.Conf.Rpc.Keepalive.ClientTime) * time.Second,
  219. // wait n second for ping ack before considering the connection dead
  220. Timeout: time.Duration(parser.Conf.Rpc.Keepalive.ClientTimeout) * time.Second,
  221. // send pings even without active streams
  222. PermitWithoutStream: true,
  223. }
  224. if len(parser.Conf.EtcdAddrs) == 0 {
  225. setupGardenClient(kacp, conns)
  226. setupThirdpartyClient(kacp, conns)
  227. setupSystemClient(kacp, conns)
  228. setupDeviceClient(kacp, conns)
  229. setupCommonClient(kacp, conns)
  230. return
  231. }
  232. setupGardenClient2(kacp, conns)
  233. setupThirdpartyClient2(kacp, conns)
  234. setupSystemClient2(kacp, conns)
  235. setupDeviceClient2(kacp, conns)
  236. setupCommonClient2(kacp, conns)
  237. return
  238. }