setup.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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-device-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. var Device DeviceClient
  16. func setupDeviceClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  17. // 根据是否为k8s来组装targets
  18. var serviceName string
  19. if parser.Conf.K8s {
  20. serviceName = parser.Conf.Rpc.Device.ServiceName
  21. } else {
  22. serviceName = parser.Conf.Rpc.Device.ServiceIp
  23. }
  24. // 发起一个连接并记录连接conn,后期释放
  25. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  26. parser.Conf.Rpc.Device.ServicePort),
  27. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  28. Device = NewDeviceClient(conn)
  29. conns = append(conns, conn)
  30. } else {
  31. fmt.Println("[rpc] dial device 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 setupGardenClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  55. // 根据是否为k8s来组装targets
  56. var serviceName string
  57. if parser.Conf.K8s {
  58. serviceName = parser.Conf.Rpc.Garden.ServiceName
  59. } else {
  60. serviceName = parser.Conf.Rpc.Garden.ServiceIp
  61. }
  62. // 发起一个连接并记录连接conn,后期释放
  63. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  64. parser.Conf.Rpc.Garden.ServicePort),
  65. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  66. Garden = NewGardenClient(conn)
  67. conns = append(conns, conn)
  68. } else {
  69. fmt.Println("[rpc] dial garden conn err", err)
  70. }
  71. return
  72. }
  73. func setupDeviceClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  74. // 根据是否为k8s来组装targets
  75. var serviceName string
  76. serviceName = parser.Conf.Rpc.Device.ServiceName
  77. builder, err := resolver.NewBuilder(parser.GetEtcdClient())
  78. if err != nil {
  79. panic(err)
  80. }
  81. // 发起一个连接并记录连接conn,后期释放
  82. if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
  83. grpc.WithResolvers(builder),
  84. grpc.WithBalancerName("round_robin"),
  85. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  86. Device = NewDeviceClient(conn)
  87. conns = append(conns, conn)
  88. } else {
  89. fmt.Println("[rpc] dial Device 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. //setupGardenClient(kacp, conns)
  105. //setupHouseholdClient(kacp, conns)
  106. if len(parser.Conf.EtcdAddrs) == 0 {
  107. setupDeviceClient(kacp, conns)
  108. return
  109. }
  110. setupDeviceClient2(kacp, conns)
  111. return
  112. }