setup.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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-company-gateway/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 Company CompanyClient
  14. var Garden GardenClient
  15. var Common CommonClient
  16. var System SystemClient
  17. var Thirdparty PropertyThirdpartyClient
  18. var PropertyLog PropertyLogClient
  19. func setupCompanyClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  20. // 根据是否为k8s来组装targets
  21. var serviceName string
  22. if parser.Conf.K8s {
  23. serviceName = parser.Conf.Rpc.Company.ServiceName
  24. } else {
  25. serviceName = parser.Conf.Rpc.Company.ServiceIp
  26. }
  27. // 发起一个连接并记录连接conn,后期释放
  28. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  29. parser.Conf.Rpc.Company.ServicePort),
  30. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  31. Company = NewCompanyClient(conn)
  32. conns = append(conns, conn)
  33. } else {
  34. fmt.Println("[rpc] dial company conn err", err)
  35. }
  36. return
  37. }
  38. func setupThirdpartyClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  39. // 根据是否为k8s来组装targets
  40. var serviceName string
  41. if parser.Conf.K8s {
  42. serviceName = parser.Conf.Rpc.Thirdparty.ServiceName
  43. } else {
  44. serviceName = parser.Conf.Rpc.Thirdparty.ServiceIp
  45. }
  46. // 发起一个连接并记录连接conn,后期释放
  47. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  48. parser.Conf.Rpc.Thirdparty.ServicePort),
  49. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  50. Thirdparty = NewPropertyThirdpartyClient(conn)
  51. conns = append(conns, conn)
  52. } else {
  53. fmt.Println("[rpc] dial thirdparty conn err", err)
  54. }
  55. return
  56. }
  57. func setupGardenClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  58. // 根据是否为k8s来组装targets
  59. var serviceName string
  60. if parser.Conf.K8s {
  61. serviceName = parser.Conf.Rpc.Garden.ServiceName
  62. } else {
  63. serviceName = parser.Conf.Rpc.Garden.ServiceIp
  64. }
  65. // 发起一个连接并记录连接conn,后期释放
  66. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  67. parser.Conf.Rpc.Garden.ServicePort),
  68. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  69. Garden = NewGardenClient(conn)
  70. conns = append(conns, conn)
  71. } else {
  72. fmt.Println("[rpc] dial garden conn err", err)
  73. }
  74. return
  75. }
  76. func setupCommonClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  77. // 根据是否为k8s来组装targets
  78. var serviceName string
  79. if parser.Conf.K8s {
  80. serviceName = parser.Conf.Rpc.Common.ServiceName
  81. } else {
  82. serviceName = parser.Conf.Rpc.Common.ServiceIp
  83. }
  84. // 发起一个连接并记录连接conn,后期释放
  85. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  86. parser.Conf.Rpc.Common.ServicePort),
  87. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  88. Common = NewCommonClient(conn)
  89. conns = append(conns, conn)
  90. } else {
  91. fmt.Println("[rpc] dial common conn err", err)
  92. }
  93. return
  94. }
  95. func setupSystemClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  96. // 根据是否为k8s来组装targets
  97. var serviceName string
  98. if parser.Conf.K8s {
  99. serviceName = parser.Conf.Rpc.System.ServiceName
  100. } else {
  101. serviceName = parser.Conf.Rpc.System.ServiceIp
  102. }
  103. // 发起一个连接并记录连接conn,后期释放
  104. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  105. parser.Conf.Rpc.System.ServicePort),
  106. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  107. System = NewSystemClient(conn)
  108. conns = append(conns, conn)
  109. } else {
  110. fmt.Println("[rpc] dial system conn err", err)
  111. }
  112. return
  113. }
  114. func setupPropertyLogClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  115. // 根据是否为k8s来组装targets
  116. var serviceName string
  117. if parser.Conf.K8s {
  118. serviceName = parser.Conf.Rpc.PropertyLog.ServiceName
  119. } else {
  120. serviceName = parser.Conf.Rpc.PropertyLog.ServiceIp
  121. }
  122. // 发起一个连接并记录连接conn,后期释放
  123. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  124. parser.Conf.Rpc.PropertyLog.ServicePort),
  125. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  126. PropertyLog = NewPropertyLogClient(conn)
  127. conns = append(conns, conn)
  128. } else {
  129. fmt.Println("[rpc] dial property conn err", err)
  130. }
  131. return
  132. }
  133. func setupSystemClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  134. // 根据是否为k8s来组装targets
  135. var serviceName string
  136. serviceName = parser.Conf.Rpc.System.ServiceName
  137. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  138. if err != nil {
  139. panic(err)
  140. }
  141. // 发起一个连接并记录连接conn,后期释放
  142. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  143. grpc.WithResolvers(builder),
  144. grpc.WithBalancerName("round_robin"),
  145. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  146. System = NewSystemClient(conn)
  147. conns = append(conns, conn)
  148. } else {
  149. fmt.Println("[rpc] dial system conn err", err)
  150. }
  151. return
  152. }
  153. func setupPropertyLogClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  154. // 根据是否为k8s来组装targets
  155. var serviceName string
  156. serviceName = parser.Conf.Rpc.PropertyLog.ServiceName
  157. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  158. if err != nil {
  159. panic(err)
  160. }
  161. // 发起一个连接并记录连接conn,后期释放
  162. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  163. grpc.WithResolvers(builder),
  164. grpc.WithBalancerName("round_robin"),
  165. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  166. PropertyLog = NewPropertyLogClient(conn)
  167. conns = append(conns, conn)
  168. } else {
  169. fmt.Println("[rpc] dial PropertyLog conn err", err)
  170. }
  171. return
  172. }
  173. func setupGardenClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  174. // 根据是否为k8s来组装targets
  175. var serviceName string
  176. serviceName = parser.Conf.Rpc.Garden.ServiceName
  177. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  178. if err != nil {
  179. panic(err)
  180. }
  181. // 发起一个连接并记录连接conn,后期释放
  182. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  183. grpc.WithResolvers(builder),
  184. grpc.WithBalancerName("round_robin"),
  185. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  186. Garden = NewGardenClient(conn)
  187. conns = append(conns, conn)
  188. } else {
  189. fmt.Println("[rpc] dial Garden conn err", err)
  190. }
  191. return
  192. }
  193. func setupCommonClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  194. // 根据是否为k8s来组装targets
  195. var serviceName string
  196. serviceName = parser.Conf.Rpc.Common.ServiceName
  197. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  198. if err != nil {
  199. panic(err)
  200. }
  201. // 发起一个连接并记录连接conn,后期释放
  202. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  203. grpc.WithResolvers(builder),
  204. grpc.WithBalancerName("round_robin"),
  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. func setupThirdpartyClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  214. // 根据是否为k8s来组装targets
  215. var serviceName string
  216. serviceName = parser.Conf.Rpc.Thirdparty.ServiceName
  217. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  218. if err != nil {
  219. panic(err)
  220. }
  221. // 发起一个连接并记录连接conn,后期释放
  222. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  223. grpc.WithResolvers(builder),
  224. grpc.WithBalancerName("round_robin"),
  225. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  226. Thirdparty = NewPropertyThirdpartyClient(conn)
  227. conns = append(conns, conn)
  228. } else {
  229. fmt.Println("[rpc] dial Thirdparty conn err", err)
  230. }
  231. return
  232. }
  233. func setupCompanyClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  234. // 根据是否为k8s来组装targets
  235. var serviceName string
  236. serviceName = parser.Conf.Rpc.Company.ServiceName
  237. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  238. if err != nil {
  239. panic(err)
  240. }
  241. // 发起一个连接并记录连接conn,后期释放
  242. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  243. grpc.WithResolvers(builder),
  244. grpc.WithBalancerName("round_robin"),
  245. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  246. Company = NewCompanyClient(conn)
  247. conns = append(conns, conn)
  248. } else {
  249. fmt.Println("[rpc] dial company conn err", err)
  250. }
  251. return
  252. }
  253. // SetupClients 创建客户端
  254. func SetupClients() (conns []*grpc.ClientConn) {
  255. // 客户端配置参数
  256. var kacp = keepalive.ClientParameters{
  257. // send pings every n seconds if there is no activity
  258. Time: time.Duration(parser.Conf.Rpc.Keepalive.ClientTime) * time.Second,
  259. // wait n second for ping ack before considering the connection dead
  260. Timeout: time.Duration(parser.Conf.Rpc.Keepalive.ClientTimeout) * time.Second,
  261. // send pings even without active streams
  262. PermitWithoutStream: true,
  263. }
  264. if len(parser.Conf.EtcdAddrs) == 0 {
  265. setupCompanyClient(kacp, conns)
  266. setupGardenClient(kacp, conns)
  267. setupCommonClient(kacp, conns)
  268. setupSystemClient(kacp, conns)
  269. setupPropertyLogClient(kacp, conns)
  270. setupThirdpartyClient(kacp, conns)
  271. return
  272. }
  273. setupCompanyClient2(kacp, conns)
  274. setupGardenClient2(kacp, conns)
  275. setupCommonClient2(kacp, conns)
  276. setupSystemClient2(kacp, conns)
  277. setupPropertyLogClient2(kacp, conns)
  278. setupThirdpartyClient2(kacp, conns)
  279. return
  280. }