setup.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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-callback-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 Garden GardenClient
  14. var Household HouseholdClient
  15. func setupGardenClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  16. // 根据是否为k8s来组装targets
  17. var serviceName string
  18. serviceName = parser.Conf.Rpc.Garden.ServiceName
  19. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  20. if err != nil {
  21. panic(err)
  22. }
  23. // 发起一个连接并记录连接conn,后期释放
  24. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  25. grpc.WithResolvers(builder),
  26. grpc.WithBalancerName("round_robin"),
  27. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  28. Garden = NewGardenClient(conn)
  29. conns = append(conns, conn)
  30. } else {
  31. fmt.Println("[rpc] dial Garden conn err", err)
  32. }
  33. return
  34. }
  35. func setupHouseholdClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  36. // 根据是否为k8s来组装targets
  37. var serviceName string
  38. if parser.Conf.K8s {
  39. serviceName = parser.Conf.Rpc.Household.ServiceName
  40. } else {
  41. serviceName = parser.Conf.Rpc.Household.ServiceIp
  42. }
  43. // 发起一个连接并记录连接conn,后期释放
  44. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  45. parser.Conf.Rpc.Household.ServicePort),
  46. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  47. Household = NewHouseholdClient(conn)
  48. conns = append(conns, conn)
  49. } else {
  50. fmt.Println("[rpc] dial household conn err", err)
  51. }
  52. return
  53. }
  54. func setupHouseholdClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  55. // 根据是否为k8s来组装targets
  56. var serviceName string
  57. serviceName = parser.Conf.Rpc.Household.ServiceName
  58. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  59. if err != nil {
  60. panic(err)
  61. }
  62. // 发起一个连接并记录连接conn,后期释放
  63. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  64. grpc.WithResolvers(builder),
  65. grpc.WithBalancerName("round_robin"),
  66. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  67. Household = NewHouseholdClient(conn)
  68. conns = append(conns, conn)
  69. } else {
  70. fmt.Println("[rpc] dial Household conn err", err)
  71. }
  72. return
  73. }
  74. func setupGardenClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  75. // 根据是否为k8s来组装targets
  76. var serviceName string
  77. if parser.Conf.K8s {
  78. serviceName = parser.Conf.Rpc.Garden.ServiceName
  79. } else {
  80. serviceName = parser.Conf.Rpc.Garden.ServiceIp
  81. }
  82. // 发起一个连接并记录连接conn,后期释放
  83. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  84. parser.Conf.Rpc.Garden.ServicePort),
  85. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  86. Garden = NewGardenClient(conn)
  87. conns = append(conns, conn)
  88. } else {
  89. fmt.Println("[rpc] dial garden conn err", err)
  90. }
  91. return
  92. }
  93. // SetupClients 创建客户端
  94. func SetupClients() (conns []*grpc.ClientConn) {
  95. // 客户端配置参数
  96. var kacp = keepalive.ClientParameters{
  97. // send pings every n seconds if there is no activity
  98. Time: time.Duration(parser.Conf.Rpc.Keepalive.ClientTime) * time.Second,
  99. // wait n second for ping ack before considering the connection dead
  100. Timeout: time.Duration(parser.Conf.Rpc.Keepalive.ClientTimeout) * time.Second,
  101. // send pings even without active streams
  102. PermitWithoutStream: true,
  103. }
  104. if len(parser.Conf.EtcdAddrs) == 0 {
  105. setupGardenClient(kacp, conns)
  106. setupHouseholdClient(kacp, conns)
  107. return
  108. }
  109. setupGardenClient2(kacp, conns)
  110. setupHouseholdClient2(kacp, conns)
  111. return
  112. }