config.go 3.2 KB

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