setup.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. grpc "google.golang.org/grpc"
  9. "google.golang.org/grpc/keepalive"
  10. "go.etcd.io/etcd/client/v3/naming/resolver"
  11. )
  12. // 客户端集合
  13. var System SystemClient
  14. var Garden GardenClient
  15. var Thirdparty PropertyThirdpartyClient
  16. func setupSystemClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  17. // 根据是否为k8s来组装targets
  18. var serviceName string
  19. serviceName = parser.Conf.Rpc.System.ServiceName
  20. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  21. if err != nil {
  22. panic(err)
  23. }
  24. // 发起一个连接并记录连接conn,后期释放
  25. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  26. grpc.WithResolvers(builder),
  27. grpc.WithBalancerName("round_robin"),
  28. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  29. System = NewSystemClient(conn)
  30. conns = append(conns, conn)
  31. } else {
  32. fmt.Println("[rpc] dial system conn err", err)
  33. }
  34. return
  35. }
  36. func setupGardenClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  37. // 根据是否为k8s来组装targets
  38. var serviceName string
  39. serviceName = parser.Conf.Rpc.Garden.ServiceName
  40. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  41. if err != nil {
  42. panic(err)
  43. }
  44. // 发起一个连接并记录连接conn,后期释放
  45. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  46. grpc.WithResolvers(builder),
  47. grpc.WithBalancerName("round_robin"),
  48. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  49. Garden = NewGardenClient(conn)
  50. conns = append(conns, conn)
  51. } else {
  52. fmt.Println("[rpc] dial Garden conn err", err)
  53. }
  54. return
  55. }
  56. func setupThirdpartyClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  57. // 根据是否为k8s来组装targets
  58. var serviceName string
  59. serviceName = parser.Conf.Rpc.Thirdparty.ServiceName
  60. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  61. if err != nil {
  62. panic(err)
  63. }
  64. // 发起一个连接并记录连接conn,后期释放
  65. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  66. grpc.WithResolvers(builder),
  67. grpc.WithBalancerName("round_robin"),
  68. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  69. Thirdparty = NewPropertyThirdpartyClient(conn)
  70. conns = append(conns, conn)
  71. } else {
  72. fmt.Println("[rpc] dial Thirdparty conn err", err)
  73. }
  74. return
  75. }
  76. // SetupClients 创建客户端
  77. func SetupClients() (conns []*grpc.ClientConn) {
  78. // 客户端配置参数
  79. var kacp = keepalive.ClientParameters{
  80. // send pings every n seconds if there is no activity
  81. Time: time.Duration(parser.Conf.Rpc.Keepalive.ClientTime) * time.Second,
  82. // wait n second for ping ack before considering the connection dead
  83. Timeout: time.Duration(parser.Conf.Rpc.Keepalive.ClientTimeout) * time.Second,
  84. // send pings even without active streams
  85. PermitWithoutStream: true,
  86. }
  87. setupSystemClient2(kacp, conns)
  88. setupThirdpartyClient2(kacp, conns)
  89. setupGardenClient2(kacp, conns)
  90. return
  91. }