Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package subconfigs
  2. import (
  3. "fmt"
  4. "git.x2erp.com/qdy/go-base/config/core"
  5. )
  6. // MicroConfig Go Micro微服务配置
  7. type MicroConfig struct {
  8. core.BaseConfig
  9. RegistryAddress string `yaml:"registry_address" desc:"服务注册中心地址"`
  10. RegistryType string `yaml:"registry_type" desc:"注册中心类型:consul, etcd, mdns"`
  11. RegistryTimeout int `yaml:"registry_timeout" desc:"注册中心超时时间(秒)"`
  12. LBStrategy string `yaml:"lb_strategy" desc:"负载均衡策略:roundrobin, random, leastconn"`
  13. LBCacheTTL int `yaml:"lb_cache_ttl" desc:"负载均衡缓存TTL(秒)"`
  14. LBRetries int `yaml:"lb_retries" desc:"负载均衡重试次数"`
  15. ClientTimeout int `yaml:"client_timeout" desc:"客户端调用超时时间(秒)"`
  16. ClientPoolSize int `yaml:"client_pool_size" desc:"客户端连接池大小"`
  17. MaxRetries int `yaml:"max_retries" desc:"最大重试次数"`
  18. CircuitEnabled bool `yaml:"circuit_enabled" desc:"是否启用熔断器"`
  19. CircuitTimeout int `yaml:"circuit_timeout" desc:"熔断器超时时间(秒)"`
  20. ErrorThreshold int `yaml:"error_threshold" desc:"错误阈值(触发熔断的错误次数)"`
  21. SleepWindow int `yaml:"sleep_window" desc:"熔断恢复时间窗口(秒)"`
  22. HealthPath string `yaml:"health_path" desc:"健康检查路径"`
  23. HealthInterval int `yaml:"health_interval" desc:"健康检查间隔(秒)"`
  24. HealthTimeout int `yaml:"health_timeout" desc:"健康检查超时时间(秒)"`
  25. LogLevel string `yaml:"log_level" desc:"日志级别:debug, info, warn, error"`
  26. EnableDebug bool `yaml:"enable_debug" desc:"是否启用调试模式"`
  27. MetricsEnabled bool `yaml:"metrics_enabled" desc:"是否启用指标收集"`
  28. MetricsAddress string `yaml:"metrics_address" desc:"指标服务地址"`
  29. TracerEnabled bool `yaml:"tracer_enabled" desc:"是否启用链路追踪"`
  30. TracerAddress string `yaml:"tracer_address" desc:"链路追踪服务地址"`
  31. }
  32. func (c *MicroConfig) Description() string {
  33. return "Go Micro微服务框架配置,包含服务发现、负载均衡、熔断器等组件"
  34. }
  35. func NewMicroConfig() *MicroConfig {
  36. return &MicroConfig{}
  37. }
  38. func (c *MicroConfig) SetDefaults() {
  39. //c.RegistryAddress = "localhost:8500"
  40. c.RegistryType = "consul"
  41. c.RegistryTimeout = 10
  42. c.LBStrategy = "roundrobin"
  43. c.LBCacheTTL = 30
  44. c.LBRetries = 3
  45. c.ClientTimeout = 30
  46. c.ClientPoolSize = 100
  47. c.MaxRetries = 3
  48. c.CircuitEnabled = true
  49. c.CircuitTimeout = 60
  50. c.ErrorThreshold = 5
  51. c.SleepWindow = 5000
  52. c.HealthPath = "/health"
  53. c.HealthInterval = 10
  54. c.HealthTimeout = 5
  55. c.LogLevel = "info"
  56. c.EnableDebug = false
  57. c.MetricsEnabled = true
  58. c.MetricsAddress = ":9090"
  59. }
  60. func (c *MicroConfig) Load(data map[string]interface{}) error {
  61. return c.LoadFromYAML(data, c)
  62. }
  63. func (c *MicroConfig) Validate() error {
  64. if c.RegistryAddress == "" {
  65. return fmt.Errorf("registry address is required")
  66. }
  67. return nil
  68. }
  69. func (c *MicroConfig) IsConfigured() bool {
  70. return c.RegistryAddress != ""
  71. }
  72. // 自动注册
  73. func init() {
  74. core.Register("micro", &MicroConfig{})
  75. }