client.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package gd_crontab
  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 GdCrontabXClient struct {
  12. xcli client.XClient
  13. }
  14. func (a *GdCrontabXClient) 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 *GdCrontabXClient) 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 *GdCrontabXClient) ReciveThirdPartLog(ctx context.Context, req *ReciveThirdPartLogReq) (reply ReciveThirdPartLogReply, err error) {
  45. err = a.xcli.Call(ctx, "ReciveThirdPartLog", req, &reply)
  46. return
  47. }
  48. func (a *GdCrontabXClient) ReciveAccessLog(ctx context.Context, req *ReciveAccessLogReq) (reply ReciveAccessLogReply, err error) {
  49. err = a.xcli.Call(ctx, "ReciveAccessLog", req, &reply)
  50. return
  51. }