config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // grpc health check switch
  21. Health bool `json:",default=true"`
  22. }
  23. // A RpcClientConf is a rpc client config.
  24. RpcClientConf struct {
  25. Etcd discov.EtcdConf `json:",optional"`
  26. Endpoints []string `json:",optional"`
  27. Target string `json:",optional"`
  28. App string `json:",optional"`
  29. Token string `json:",optional"`
  30. NonBlock bool `json:",optional"`
  31. Timeout int64 `json:",default=2000"`
  32. }
  33. )
  34. // NewDirectClientConf returns a RpcClientConf.
  35. func NewDirectClientConf(endpoints []string, app, token string) RpcClientConf {
  36. return RpcClientConf{
  37. Endpoints: endpoints,
  38. App: app,
  39. Token: token,
  40. }
  41. }
  42. // NewEtcdClientConf returns a RpcClientConf.
  43. func NewEtcdClientConf(hosts []string, key, app, token string) RpcClientConf {
  44. return RpcClientConf{
  45. Etcd: discov.EtcdConf{
  46. Hosts: hosts,
  47. Key: key,
  48. },
  49. App: app,
  50. Token: token,
  51. }
  52. }
  53. // HasEtcd checks if there is etcd settings in config.
  54. func (sc RpcServerConf) HasEtcd() bool {
  55. return len(sc.Etcd.Hosts) > 0 && len(sc.Etcd.Key) > 0
  56. }
  57. // Validate validates the config.
  58. func (sc RpcServerConf) Validate() error {
  59. if !sc.Auth {
  60. return nil
  61. }
  62. return sc.Redis.Validate()
  63. }
  64. // BuildTarget builds the rpc target from the given config.
  65. func (cc RpcClientConf) BuildTarget() (string, error) {
  66. if len(cc.Endpoints) > 0 {
  67. return resolver.BuildDirectTarget(cc.Endpoints), nil
  68. } else if len(cc.Target) > 0 {
  69. return cc.Target, nil
  70. }
  71. if err := cc.Etcd.Validate(); err != nil {
  72. return "", err
  73. }
  74. if cc.Etcd.HasAccount() {
  75. discov.RegisterAccount(cc.Etcd.Hosts, cc.Etcd.User, cc.Etcd.Pass)
  76. }
  77. if cc.Etcd.HasTLS() {
  78. if err := discov.RegisterTLS(cc.Etcd.Hosts, cc.Etcd.CertFile, cc.Etcd.CertKeyFile,
  79. cc.Etcd.CACertFile, cc.Etcd.InsecureSkipVerify); err != nil {
  80. return "", err
  81. }
  82. }
  83. return resolver.BuildDiscovTarget(cc.Etcd.Hosts, cc.Etcd.Key), nil
  84. }
  85. // HasCredential checks if there is a credential in config.
  86. func (cc RpcClientConf) HasCredential() bool {
  87. return len(cc.App) > 0 && len(cc.Token) > 0
  88. }