説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

micro_config.go 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package subconfigs
  2. import "fmt"
  3. // MicroConfig Go Micro微服务配置
  4. type MicroConfig struct {
  5. BaseConfig
  6. RegistryAddress string `yaml:"registry_address"`
  7. RegistryType string `yaml:"registry_type"`
  8. RegistryTimeout int `yaml:"registry_timeout"`
  9. LBStrategy string `yaml:"lb_strategy"`
  10. LBCacheTTL int `yaml:"lb_cache_ttl"`
  11. LBRetries int `yaml:"lb_retries"`
  12. ClientTimeout int `yaml:"client_timeout"`
  13. ClientPoolSize int `yaml:"client_pool_size"`
  14. MaxRetries int `yaml:"max_retries"`
  15. CircuitEnabled bool `yaml:"circuit_enabled"`
  16. CircuitTimeout int `yaml:"circuit_timeout"`
  17. ErrorThreshold int `yaml:"error_threshold"`
  18. SleepWindow int `yaml:"sleep_window"`
  19. HealthPath string `yaml:"health_path"`
  20. HealthInterval int `yaml:"health_interval"`
  21. HealthTimeout int `yaml:"health_timeout"`
  22. LogLevel string `yaml:"log_level"`
  23. EnableDebug bool `yaml:"enable_debug"`
  24. MetricsEnabled bool `yaml:"metrics_enabled"`
  25. MetricsAddress string `yaml:"metrics_address"`
  26. TracerEnabled bool `yaml:"tracer_enabled"`
  27. TracerAddress string `yaml:"tracer_address"`
  28. }
  29. func NewMicroConfig() *MicroConfig {
  30. return &MicroConfig{}
  31. }
  32. func (c *MicroConfig) SetDefaults() {
  33. //c.RegistryAddress = "localhost:8500"
  34. c.RegistryType = "consul"
  35. c.RegistryTimeout = 10
  36. c.LBStrategy = "roundrobin"
  37. c.LBCacheTTL = 30
  38. c.LBRetries = 3
  39. c.ClientTimeout = 30
  40. c.ClientPoolSize = 100
  41. c.MaxRetries = 3
  42. c.CircuitEnabled = true
  43. c.CircuitTimeout = 60
  44. c.ErrorThreshold = 5
  45. c.SleepWindow = 5000
  46. c.HealthPath = "/health"
  47. c.HealthInterval = 10
  48. c.HealthTimeout = 5
  49. c.LogLevel = "info"
  50. c.EnableDebug = false
  51. c.MetricsEnabled = true
  52. c.MetricsAddress = ":9090"
  53. }
  54. func (c *MicroConfig) Load(data map[string]interface{}) error {
  55. return c.LoadFromYAML(data, c)
  56. }
  57. func (c *MicroConfig) Validate() error {
  58. if c.RegistryAddress == "" {
  59. return fmt.Errorf("registry address is required")
  60. }
  61. return nil
  62. }
  63. func (c *MicroConfig) IsConfigured() bool {
  64. return c.RegistryAddress != ""
  65. }
  66. // 自动注册
  67. func init() {
  68. Register("micro", &MicroConfig{})
  69. }