client.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package gd_management
  2. import (
  3. "context"
  4. "fmt"
  5. "runtime"
  6. "time"
  7. "github.com/smallnest/rpcx/client"
  8. "github.com/smallnest/rpcx/protocol"
  9. "github.com/smallnest/rpcx/share"
  10. )
  11. type GdManagementXClient struct {
  12. xcli client.XClient
  13. }
  14. func (a *GdManagementXClient) 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. if runtime.GOOS == "windows" {
  24. d := client.NewEtcdDiscovery(fmt.Sprintf("/%s", basePath), servicePath, etcdAddrs, nil)
  25. a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, d, opt)
  26. } else {
  27. d := client.NewEtcdDiscovery(basePath, servicePath, etcdAddrs, nil)
  28. a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, d, opt)
  29. }
  30. }
  31. // k8s模式下,服务发现方式需要改为点对点,使用k8s自身的Service进行服务发现,Service名称格式必须是abc-xyz的方式,不能使用"_"
  32. func (a *GdManagementXClient) InitForK8s(servicePath string, k8sServiceName string, k8sServicePort string) {
  33. opt := client.Option{
  34. Retries: 1,
  35. RPCPath: share.DefaultRPCPath,
  36. ConnectTimeout: 10 * time.Second,
  37. SerializeType: protocol.JSON,
  38. CompressType: protocol.None,
  39. BackupLatency: 10 * time.Millisecond,
  40. }
  41. sd := client.NewPeer2PeerDiscovery(fmt.Sprintf("tcp@%s:%s", k8sServiceName, k8sServicePort), "")
  42. a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, sd, opt)
  43. }
  44. func (a *GdManagementXClient) MangementGetUserMerchantCountCode(ctx context.Context, req *MangementGetUserMerchantCountCodeReq) (reply MangementGetUserMerchantCountCodeReply, err error) {
  45. err = a.xcli.Call(ctx, "MangementGetUserMerchantCountCode", req, &reply)
  46. return
  47. }
  48. func (a *GdManagementXClient) GetApiThreshold(ctx context.Context, req *GetApiThresholdReq) (reply GetApiThresholdReply, err error) {
  49. err = a.xcli.Call(ctx, "GetApiThreshold", req, &reply)
  50. return
  51. }
  52. func (a *GdManagementXClient) ManagementGetMerchantIds(ctx context.Context, req *ManagementGetMerchantIdsReq) (reply ManagementGetMerchantIdsReply, err error) {
  53. err = a.xcli.Call(ctx, "ManagementGetMerchantIds", req, &reply)
  54. return
  55. }
  56. func (a *GdManagementXClient) ManagementGetProviderCountInfo(ctx context.Context, req *ManagementGetProviderCountInfoReq) (reply ManagementGetProviderCountInfoReply, err error) {
  57. err = a.xcli.Call(ctx, "ManagementGetProviderCountInfo", req, &reply)
  58. return
  59. }
  60. func (a *GdManagementXClient) ManagementGetApiShowInfo(ctx context.Context, req *ManagementGetApiShowInfoReq) (reply ManagementGetApiShowInfoReply, err error) {
  61. err = a.xcli.Call(ctx, "ManagementGetApiShowInfo", req, &reply)
  62. return
  63. }
  64. func (a *GdManagementXClient) ManagementGetMerchantApiTimeout(ctx context.Context, req *ManagementGetMerchantApiTimeoutReq) (reply ManagementGetMerchantApiTimeoutReply, err error) {
  65. err = a.xcli.Call(ctx, "ManagementGetMerchantApiTimeout", req, &reply)
  66. return
  67. }
  68. func (a *GdManagementXClient) ManagementDelProviderOldCount(ctx context.Context, req *ManagementDelProviderOldCountReq) (reply ManagementDelProviderOldCountReply, err error) {
  69. err = a.xcli.Call(ctx, "ManagementDelProviderOldCount", req, &reply)
  70. return
  71. }
  72. func (a *GdManagementXClient) GenerateBill(ctx context.Context, req *GenerateBillReq) (reply GenerateBillReply, err error) {
  73. err = a.xcli.Call(ctx, "GenerateBill", req, &reply)
  74. return
  75. }
  76. func (a *GdManagementXClient) BillNotify(ctx context.Context, req *BillNotifyReq) (reply BillNotifyReply, err error) {
  77. err = a.xcli.Call(ctx, "BillNotify", req, &reply)
  78. return
  79. }