setup.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package pb
  4. /*
  5. import (
  6. "property-log/parser"
  7. fmt "fmt"
  8. "time"
  9. grpc "google.golang.org/grpc"
  10. "google.golang.org/grpc/keepalive"
  11. )
  12. // 客户端集合
  13. var Company CompanyClient
  14. func setupCompanyClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  15. // 根据是否为k8s来组装targets
  16. var serviceName string
  17. if parser.Conf.K8s {
  18. serviceName = parser.Conf.Rpc.Company.ServiceName
  19. } else {
  20. serviceName = parser.Conf.Rpc.Company.ServiceIp
  21. }
  22. // 发起一个连接并记录连接conn,后期释放
  23. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  24. parser.Conf.Rpc.Company.ServicePort),
  25. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  26. Company = NewCompanyClient(conn)
  27. conns = append(conns, conn)
  28. } else {
  29. fmt.Println("[rpc] dial device conn err", err)
  30. }
  31. return
  32. }
  33. // SetupClients 创建客户端
  34. func SetupClients() (conns []*grpc.ClientConn) {
  35. // 客户端配置参数
  36. var kacp = keepalive.ClientParameters{
  37. // send pings every n seconds if there is no activity
  38. Time: time.Duration(parser.Conf.Rpc.Keepalive.ClientTime) * time.Second,
  39. // wait n second for ping ack before considering the connection dead
  40. Timeout: time.Duration(parser.Conf.Rpc.Keepalive.ClientTimeout) * time.Second,
  41. // send pings even without active streams
  42. PermitWithoutStream: true,
  43. }
  44. setupCompanyClient(kacp, conns)
  45. return
  46. }
  47. */