config.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package zrpc
  2. import (
  3. "github.com/tal-tech/go-zero/core/discov"
  4. "github.com/tal-tech/go-zero/core/service"
  5. "github.com/tal-tech/go-zero/core/stores/redis"
  6. )
  7. type (
  8. // A RpcServerConf is a rpc server config.
  9. RpcServerConf struct {
  10. service.ServiceConf
  11. ListenOn string
  12. Etcd discov.EtcdConf `json:",optional"`
  13. Auth bool `json:",optional"`
  14. Redis redis.RedisKeyConf `json:",optional"`
  15. StrictControl bool `json:",optional"`
  16. // setting 0 means no timeout
  17. Timeout int64 `json:",default=2000"`
  18. CpuThreshold int64 `json:",default=900,range=[0:1000]"`
  19. MaxRetries int `json:",default=0,range=[0:]"`
  20. }
  21. // A RpcClientConf is a rpc client config.
  22. RpcClientConf struct {
  23. Etcd discov.EtcdConf `json:",optional"`
  24. Endpoints []string `json:",optional"`
  25. Target string `json:",optional"`
  26. App string `json:",optional"`
  27. Token string `json:",optional"`
  28. Retry bool `json:",optional"` // grpc auto retry
  29. Timeout int64 `json:",default=2000"`
  30. InsecureVerify bool `json:",default=false"`
  31. }
  32. )
  33. // NewDirectClientConf returns a RpcClientConf.
  34. func NewDirectClientConf(endpoints []string, app, token string) RpcClientConf {
  35. return RpcClientConf{
  36. Endpoints: endpoints,
  37. App: app,
  38. Token: token,
  39. }
  40. }
  41. // NewEtcdClientConf returns a RpcClientConf.
  42. func NewEtcdClientConf(hosts []string, key, app, token string) RpcClientConf {
  43. return RpcClientConf{
  44. Etcd: discov.EtcdConf{
  45. Hosts: hosts,
  46. Key: key,
  47. },
  48. App: app,
  49. Token: token,
  50. }
  51. }
  52. // HasEtcd checks if there is etcd settings in config.
  53. func (sc RpcServerConf) HasEtcd() bool {
  54. return len(sc.Etcd.Hosts) > 0 && len(sc.Etcd.Key) > 0
  55. }
  56. // Validate validates the config.
  57. func (sc RpcServerConf) Validate() error {
  58. if !sc.Auth {
  59. return nil
  60. }
  61. return sc.Redis.Validate()
  62. }
  63. // HasCredential checks if there is a credential in config.
  64. func (cc RpcClientConf) HasCredential() bool {
  65. return len(cc.App) > 0 && len(cc.Token) > 0
  66. }
  67. //HasTls checks if there is a SSL in config.
  68. func (cc RpcClientConf) HasSslVerify() bool {
  69. return cc.InsecureVerify
  70. }