config.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/resolver"
  7. )
  8. type (
  9. // A RpcServerConf is a rpc server config.
  10. RpcServerConf struct {
  11. service.ServiceConf
  12. ListenOn string
  13. Etcd discov.EtcdConf `json:",optional"`
  14. Auth bool `json:",optional"`
  15. Redis redis.RedisKeyConf `json:",optional"`
  16. StrictControl bool `json:",optional"`
  17. // setting 0 means no timeout
  18. Timeout int64 `json:",default=2000"`
  19. CpuThreshold int64 `json:",default=900,range=[0:1000]"`
  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. NonBlock bool `json:",optional"`
  29. Timeout int64 `json:",default=2000"`
  30. }
  31. )
  32. // NewDirectClientConf returns a RpcClientConf.
  33. func NewDirectClientConf(endpoints []string, app, token string) RpcClientConf {
  34. return RpcClientConf{
  35. Endpoints: endpoints,
  36. App: app,
  37. Token: token,
  38. }
  39. }
  40. // NewEtcdClientConf returns a RpcClientConf.
  41. func NewEtcdClientConf(hosts []string, key, app, token string) RpcClientConf {
  42. return RpcClientConf{
  43. Etcd: discov.EtcdConf{
  44. Hosts: hosts,
  45. Key: key,
  46. },
  47. App: app,
  48. Token: token,
  49. }
  50. }
  51. // HasEtcd checks if there is etcd settings in config.
  52. func (sc RpcServerConf) HasEtcd() bool {
  53. return len(sc.Etcd.Hosts) > 0 && len(sc.Etcd.Key) > 0
  54. }
  55. // Validate validates the config.
  56. func (sc RpcServerConf) Validate() error {
  57. if !sc.Auth {
  58. return nil
  59. }
  60. return sc.Redis.Validate()
  61. }
  62. // BuildTarget builds the rpc target from the given config.
  63. func (cc RpcClientConf) BuildTarget() (string, error) {
  64. if len(cc.Endpoints) > 0 {
  65. return resolver.BuildDirectTarget(cc.Endpoints), nil
  66. } else if len(cc.Target) > 0 {
  67. return cc.Target, nil
  68. }
  69. if err := cc.Etcd.Validate(); err != nil {
  70. return "", err
  71. }
  72. if cc.Etcd.HasAccount() {
  73. discov.RegisterAccount(cc.Etcd.Hosts, cc.Etcd.User, cc.Etcd.Pass)
  74. }
  75. if cc.Etcd.HasTLS() {
  76. if err := discov.RegisterTLS(cc.Etcd.Hosts, cc.Etcd.CertFile, cc.Etcd.CertKeyFile,
  77. cc.Etcd.CACertFile, cc.Etcd.InsecureSkipVerify); err != nil {
  78. return "", err
  79. }
  80. }
  81. return resolver.BuildDiscovTarget(cc.Etcd.Hosts, cc.Etcd.Key), nil
  82. }
  83. // HasCredential checks if there is a credential in config.
  84. func (cc RpcClientConf) HasCredential() bool {
  85. return len(cc.App) > 0 && len(cc.Token) > 0
  86. }