package subconfigs import ( "fmt" "git.x2erp.com/qdy/go-base/config/core" ) // MicroConfig Go Micro微服务配置 type MicroConfig struct { core.BaseConfig RegistryAddress string `yaml:"registry_address" desc:"服务注册中心地址"` RegistryType string `yaml:"registry_type" desc:"注册中心类型:consul, etcd, mdns"` RegistryTimeout int `yaml:"registry_timeout" desc:"注册中心超时时间(秒)"` LBStrategy string `yaml:"lb_strategy" desc:"负载均衡策略:roundrobin, random, leastconn"` LBCacheTTL int `yaml:"lb_cache_ttl" desc:"负载均衡缓存TTL(秒)"` LBRetries int `yaml:"lb_retries" desc:"负载均衡重试次数"` ClientTimeout int `yaml:"client_timeout" desc:"客户端调用超时时间(秒)"` ClientPoolSize int `yaml:"client_pool_size" desc:"客户端连接池大小"` MaxRetries int `yaml:"max_retries" desc:"最大重试次数"` CircuitEnabled bool `yaml:"circuit_enabled" desc:"是否启用熔断器"` CircuitTimeout int `yaml:"circuit_timeout" desc:"熔断器超时时间(秒)"` ErrorThreshold int `yaml:"error_threshold" desc:"错误阈值(触发熔断的错误次数)"` SleepWindow int `yaml:"sleep_window" desc:"熔断恢复时间窗口(秒)"` HealthPath string `yaml:"health_path" desc:"健康检查路径"` HealthInterval int `yaml:"health_interval" desc:"健康检查间隔(秒)"` HealthTimeout int `yaml:"health_timeout" desc:"健康检查超时时间(秒)"` LogLevel string `yaml:"log_level" desc:"日志级别:debug, info, warn, error"` EnableDebug bool `yaml:"enable_debug" desc:"是否启用调试模式"` MetricsEnabled bool `yaml:"metrics_enabled" desc:"是否启用指标收集"` MetricsAddress string `yaml:"metrics_address" desc:"指标服务地址"` TracerEnabled bool `yaml:"tracer_enabled" desc:"是否启用链路追踪"` TracerAddress string `yaml:"tracer_address" desc:"链路追踪服务地址"` } func (c *MicroConfig) Description() string { return "Go Micro微服务框架配置,包含服务发现、负载均衡、熔断器等组件" } func NewMicroConfig() *MicroConfig { return &MicroConfig{} } func (c *MicroConfig) SetDefaults() { //c.RegistryAddress = "localhost:8500" c.RegistryType = "consul" c.RegistryTimeout = 10 c.LBStrategy = "roundrobin" c.LBCacheTTL = 30 c.LBRetries = 3 c.ClientTimeout = 30 c.ClientPoolSize = 100 c.MaxRetries = 3 c.CircuitEnabled = true c.CircuitTimeout = 60 c.ErrorThreshold = 5 c.SleepWindow = 5000 c.HealthPath = "/health" c.HealthInterval = 10 c.HealthTimeout = 5 c.LogLevel = "info" c.EnableDebug = false c.MetricsEnabled = true c.MetricsAddress = ":9090" } func (c *MicroConfig) Load(data map[string]interface{}) error { return c.LoadFromYAML(data, c) } func (c *MicroConfig) Validate() error { if c.RegistryAddress == "" { return fmt.Errorf("registry address is required") } return nil } func (c *MicroConfig) IsConfigured() bool { return c.RegistryAddress != "" } // 自动注册 func init() { core.Register("micro", &MicroConfig{}) }