setup.go 10 KB

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