client.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package gd_vehicle
  2. import (
  3. "context"
  4. "fmt"
  5. "gd_gateway/apis"
  6. "time"
  7. "github.com/smallnest/rpcx/client"
  8. "github.com/smallnest/rpcx/protocol"
  9. "github.com/smallnest/rpcx/share"
  10. )
  11. type GdVehicleXClient struct {
  12. xcli client.XClient
  13. }
  14. func (a *GdVehicleXClient) Init(etcdAddrs []string, basePath, servicePath string) {
  15. opt := client.Option{
  16. Retries: 3,
  17. RPCPath: share.DefaultRPCPath,
  18. ConnectTimeout: 10 * time.Second,
  19. SerializeType: protocol.JSON,
  20. CompressType: protocol.None,
  21. BackupLatency: 10 * time.Millisecond,
  22. }
  23. d := client.NewEtcdDiscovery(basePath, servicePath, etcdAddrs, nil)
  24. a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, d, opt)
  25. }
  26. func (a *GdVehicleXClient) InitForK8s(servicePath string, k8sServiceName string, k8sServicePort string) {
  27. opt := client.Option{
  28. Retries: 1,
  29. RPCPath: share.DefaultRPCPath,
  30. ConnectTimeout: 10 * time.Second,
  31. SerializeType: protocol.JSON,
  32. CompressType: protocol.None,
  33. BackupLatency: 10 * time.Millisecond,
  34. }
  35. sd := client.NewPeer2PeerDiscovery(fmt.Sprintf("tcp@%s:%s", k8sServiceName, k8sServicePort), "")
  36. a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, sd, opt)
  37. }
  38. func (a *GdVehicleXClient) Query(ctx context.Context, req *apis.CommonReq) (reply apis.CommonReply, err error) {
  39. err = a.xcli.Call(ctx, "Query", req, &reply)
  40. return
  41. }