config.go 3.1 KB

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