config.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package zrpc
  2. import (
  3. "time"
  4. "github.com/wuntsong-org/go-zero-plus/core/discov"
  5. "github.com/wuntsong-org/go-zero-plus/core/service"
  6. "github.com/wuntsong-org/go-zero-plus/core/stores/redis"
  7. "github.com/wuntsong-org/go-zero-plus/zrpc/internal"
  8. "github.com/wuntsong-org/go-zero-plus/zrpc/resolver"
  9. )
  10. type (
  11. // ClientMiddlewaresConf defines whether to use client middlewares.
  12. ClientMiddlewaresConf = internal.ClientMiddlewaresConf
  13. // ServerMiddlewaresConf defines whether to use server middlewares.
  14. ServerMiddlewaresConf = internal.ServerMiddlewaresConf
  15. // StatConf defines the stat config.
  16. StatConf = internal.StatConf
  17. // MethodTimeoutConf defines specified timeout for gRPC method.
  18. MethodTimeoutConf = internal.MethodTimeoutConf
  19. // A RpcClientConf is a rpc client config.
  20. RpcClientConf struct {
  21. Etcd discov.EtcdConf `json:",optional,inherit"`
  22. Endpoints []string `json:",optional"`
  23. Target string `json:",optional"`
  24. App string `json:",optional"`
  25. Token string `json:",optional"`
  26. NonBlock bool `json:",optional"`
  27. Timeout int64 `json:",default=2000"`
  28. KeepaliveTime time.Duration `json:",optional"`
  29. Middlewares ClientMiddlewaresConf
  30. }
  31. // A RpcServerConf is a rpc server config.
  32. RpcServerConf struct {
  33. service.ServiceConf
  34. ListenOn string
  35. Etcd discov.EtcdConf `json:",optional,inherit"`
  36. Auth bool `json:",optional"`
  37. Redis redis.RedisKeyConf `json:",optional"`
  38. StrictControl bool `json:",optional"`
  39. // setting 0 means no timeout
  40. Timeout int64 `json:",default=2000"`
  41. CpuThreshold int64 `json:",default=900,range=[0:1000]"`
  42. // grpc health check switch
  43. Health bool `json:",default=true"`
  44. Middlewares ServerMiddlewaresConf
  45. // setting specified timeout for gRPC method
  46. MethodTimeouts []MethodTimeoutConf `json:",optional"`
  47. }
  48. )
  49. // NewDirectClientConf returns a RpcClientConf.
  50. func NewDirectClientConf(endpoints []string, app, token string) RpcClientConf {
  51. return RpcClientConf{
  52. Endpoints: endpoints,
  53. App: app,
  54. Token: token,
  55. }
  56. }
  57. // NewEtcdClientConf returns a RpcClientConf.
  58. func NewEtcdClientConf(hosts []string, key, app, token string) RpcClientConf {
  59. return RpcClientConf{
  60. Etcd: discov.EtcdConf{
  61. Hosts: hosts,
  62. Key: key,
  63. },
  64. App: app,
  65. Token: token,
  66. }
  67. }
  68. // HasEtcd checks if there is etcd settings in config.
  69. func (sc RpcServerConf) HasEtcd() bool {
  70. return len(sc.Etcd.Hosts) > 0 && len(sc.Etcd.Key) > 0
  71. }
  72. // Validate validates the config.
  73. func (sc RpcServerConf) Validate() error {
  74. if !sc.Auth {
  75. return nil
  76. }
  77. return sc.Redis.Validate()
  78. }
  79. // BuildTarget builds the rpc target from the given config.
  80. func (cc RpcClientConf) BuildTarget() (string, error) {
  81. if len(cc.Endpoints) > 0 {
  82. return resolver.BuildDirectTarget(cc.Endpoints), nil
  83. } else if len(cc.Target) > 0 {
  84. return cc.Target, nil
  85. }
  86. if err := cc.Etcd.Validate(); err != nil {
  87. return "", err
  88. }
  89. if cc.Etcd.HasAccount() {
  90. discov.RegisterAccount(cc.Etcd.Hosts, cc.Etcd.User, cc.Etcd.Pass)
  91. }
  92. if cc.Etcd.HasTLS() {
  93. if err := discov.RegisterTLS(cc.Etcd.Hosts, cc.Etcd.CertFile, cc.Etcd.CertKeyFile,
  94. cc.Etcd.CACertFile, cc.Etcd.InsecureSkipVerify); err != nil {
  95. return "", err
  96. }
  97. }
  98. return resolver.BuildDiscovTarget(cc.Etcd.Hosts, cc.Etcd.Key), nil
  99. }
  100. // HasCredential checks if there is a credential in config.
  101. func (cc RpcClientConf) HasCredential() bool {
  102. return len(cc.App) > 0 && len(cc.Token) > 0
  103. }