client.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. // package call gd_auth_check micro service function
  4. package gd_adm_data
  5. import (
  6. "context"
  7. "fmt"
  8. "gd_vehicle/apis"
  9. "time"
  10. "github.com/smallnest/rpcx/client"
  11. "github.com/smallnest/rpcx/protocol"
  12. "github.com/smallnest/rpcx/share"
  13. )
  14. type GdAdmDataXClient struct {
  15. xcli client.XClient
  16. }
  17. func (a *GdAdmDataXClient) Init(etcdAddrs []string, basePath, servicePath string) {
  18. opt := client.Option{
  19. Retries: 3,
  20. RPCPath: share.DefaultRPCPath,
  21. ConnectTimeout: 10 * time.Second,
  22. SerializeType: protocol.JSON,
  23. CompressType: protocol.None,
  24. BackupLatency: 10 * time.Millisecond,
  25. }
  26. d := client.NewEtcdDiscovery(basePath, servicePath, etcdAddrs, nil)
  27. a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, d, opt)
  28. }
  29. // k8s模式下,服务发现方式需要改为点对点,使用k8s自身的Service进行服务发现,Service名称格式必须是abc-xyz的方式,不能使用"_"
  30. func (a *GdAdmDataXClient) InitForK8s(servicePath string, k8sServiceName string, k8sServicePort int) {
  31. opt := client.Option{
  32. Retries: 1,
  33. RPCPath: share.DefaultRPCPath,
  34. ConnectTimeout: 10 * time.Second,
  35. SerializeType: protocol.JSON,
  36. CompressType: protocol.None,
  37. BackupLatency: 10 * time.Millisecond,
  38. }
  39. sd := client.NewPeer2PeerDiscovery(fmt.Sprintf("tcp@%s:%d", k8sServiceName, k8sServicePort), "")
  40. a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, sd, opt)
  41. }
  42. func (a *GdAdmDataXClient) Query(ctx context.Context, req *apis.QueryRequest) (reply apis.QueryResponse, err error) {
  43. err = a.xcli.Call(ctx, "Query", req, &reply)
  44. return
  45. }