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-system/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 Common CommonClient
  15. var Household HouseholdClient
  16. var Thirdparty PropertyThirdpartyClient
  17. var Company CompanyClient
  18. func setupHouseholdClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  19. // 根据是否为k8s来组装targets
  20. var serviceName string
  21. serviceName = parser.Conf.Rpc.Household.ServiceName
  22. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  23. if err != nil {
  24. panic(err)
  25. }
  26. // 发起一个连接并记录连接conn,后期释放
  27. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  28. grpc.WithResolvers(builder),
  29. grpc.WithBalancerName("round_robin"),
  30. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  31. Household = NewHouseholdClient(conn)
  32. conns = append(conns, conn)
  33. } else {
  34. fmt.Println("[rpc] dial Household conn err", err)
  35. }
  36. return
  37. }
  38. func setupGardenClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  39. // 根据是否为k8s来组装targets
  40. var serviceName string
  41. serviceName = parser.Conf.Rpc.Garden.ServiceName
  42. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  43. if err != nil {
  44. panic(err)
  45. }
  46. // 发起一个连接并记录连接conn,后期释放
  47. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  48. grpc.WithResolvers(builder),
  49. grpc.WithBalancerName("round_robin"),
  50. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  51. Garden = NewGardenClient(conn)
  52. conns = append(conns, conn)
  53. } else {
  54. fmt.Println("[rpc] dial Garden conn err", err)
  55. }
  56. return
  57. }
  58. func setupCommonClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  59. // 根据是否为k8s来组装targets
  60. var serviceName string
  61. serviceName = parser.Conf.Rpc.Common.ServiceName
  62. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  63. if err != nil {
  64. panic(err)
  65. }
  66. // 发起一个连接并记录连接conn,后期释放
  67. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  68. grpc.WithResolvers(builder),
  69. grpc.WithBalancerName("round_robin"),
  70. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  71. Common = NewCommonClient(conn)
  72. conns = append(conns, conn)
  73. } else {
  74. fmt.Println("[rpc] dial Common conn err", err)
  75. }
  76. return
  77. }
  78. func setupThirdpartyClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  79. // 根据是否为k8s来组装targets
  80. var serviceName string
  81. serviceName = parser.Conf.Rpc.Thirdparty.ServiceName
  82. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  83. if err != nil {
  84. panic(err)
  85. }
  86. // 发起一个连接并记录连接conn,后期释放
  87. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  88. grpc.WithResolvers(builder),
  89. grpc.WithBalancerName("round_robin"),
  90. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  91. Thirdparty = NewPropertyThirdpartyClient(conn)
  92. conns = append(conns, conn)
  93. } else {
  94. fmt.Println("[rpc] dial Thirdparty conn err", err)
  95. }
  96. return
  97. }
  98. func setupCompanyClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  99. // 根据是否为k8s来组装targets
  100. var serviceName string
  101. serviceName = parser.Conf.Rpc.Company.ServiceName
  102. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  103. if err != nil {
  104. panic(err)
  105. }
  106. // 发起一个连接并记录连接conn,后期释放
  107. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  108. grpc.WithResolvers(builder),
  109. grpc.WithBalancerName("round_robin"),
  110. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  111. Company = NewCompanyClient(conn)
  112. conns = append(conns, conn)
  113. } else {
  114. fmt.Println("[rpc] dial company conn err", err)
  115. }
  116. return
  117. }
  118. func setupCompanyClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  119. // 根据是否为k8s来组装targets
  120. var serviceName string
  121. if parser.Conf.K8s {
  122. serviceName = parser.Conf.Rpc.Company.ServiceName
  123. } else {
  124. serviceName = parser.Conf.Rpc.Company.ServiceIp
  125. }
  126. // 发起一个连接并记录连接conn,后期释放
  127. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  128. parser.Conf.Rpc.Company.ServicePort),
  129. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  130. Company = NewCompanyClient(conn)
  131. conns = append(conns, conn)
  132. } else {
  133. fmt.Println("[rpc] dial company conn err", err)
  134. }
  135. return
  136. }
  137. func setupGardenClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  138. // 根据是否为k8s来组装targets
  139. var serviceName string
  140. if parser.Conf.K8s {
  141. serviceName = parser.Conf.Rpc.Garden.ServiceName
  142. } else {
  143. serviceName = parser.Conf.Rpc.Garden.ServiceIp
  144. }
  145. // 发起一个连接并记录连接conn,后期释放
  146. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  147. parser.Conf.Rpc.Garden.ServicePort),
  148. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  149. Garden = NewGardenClient(conn)
  150. conns = append(conns, conn)
  151. } else {
  152. fmt.Println("[rpc] dial garden conn err", err)
  153. }
  154. return
  155. }
  156. func setupHouseholdClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  157. // 根据是否为k8s来组装targets
  158. var serviceName string
  159. if parser.Conf.K8s {
  160. serviceName = parser.Conf.Rpc.Household.ServiceName
  161. } else {
  162. serviceName = parser.Conf.Rpc.Household.ServiceIp
  163. }
  164. // 发起一个连接并记录连接conn,后期释放
  165. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  166. parser.Conf.Rpc.Household.ServicePort),
  167. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  168. Household = NewHouseholdClient(conn)
  169. conns = append(conns, conn)
  170. } else {
  171. fmt.Println("[rpc] dial household conn err", err)
  172. }
  173. return
  174. }
  175. func setupCommonClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  176. // 根据是否为k8s来组装targets
  177. var serviceName string
  178. if parser.Conf.K8s {
  179. serviceName = parser.Conf.Rpc.Common.ServiceName
  180. } else {
  181. serviceName = parser.Conf.Rpc.Common.ServiceIp
  182. }
  183. // 发起一个连接并记录连接conn,后期释放
  184. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  185. parser.Conf.Rpc.Common.ServicePort),
  186. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  187. Common = NewCommonClient(conn)
  188. conns = append(conns, conn)
  189. } else {
  190. fmt.Println("[rpc] dial common conn err", err)
  191. }
  192. return
  193. }
  194. func setupThirdpartyClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  195. // 根据是否为k8s来组装targets
  196. var serviceName string
  197. if parser.Conf.K8s {
  198. serviceName = parser.Conf.Rpc.Thirdparty.ServiceName
  199. } else {
  200. serviceName = parser.Conf.Rpc.Thirdparty.ServiceIp
  201. }
  202. // 发起一个连接并记录连接conn,后期释放
  203. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  204. parser.Conf.Rpc.Thirdparty.ServicePort),
  205. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  206. Thirdparty = NewPropertyThirdpartyClient(conn)
  207. conns = append(conns, conn)
  208. } else {
  209. fmt.Println("[rpc] dial thirdparty 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. setupCommonClient(kacp, conns)
  227. setupThirdpartyClient(kacp, conns)
  228. setupHouseholdClient(kacp, conns)
  229. setupCompanyClient(kacp, conns)
  230. return
  231. }
  232. setupGardenClient2(kacp, conns)
  233. setupCommonClient2(kacp, conns)
  234. setupThirdpartyClient2(kacp, conns)
  235. setupHouseholdClient2(kacp, conns)
  236. setupCompanyClient2(kacp, conns)
  237. return
  238. }