Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

service_config.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // subconfigs/service_config.go
  2. package subconfigs
  3. import (
  4. "fmt"
  5. "log"
  6. )
  7. // ServiceConfig 数据库配置
  8. type ServiceConfig struct {
  9. BaseConfig
  10. AppName string `yaml:"app_name"` //当前应用名称
  11. AppVersion string `yaml:"app_version"` //当前应用版本
  12. AppEnv string `yaml:"app_env"` //环境参数 dev test prod
  13. AppAuthToken string `yaml:"app_auth_token"` //使用静态认证的时候使用的token
  14. Port int `yaml:"port"`
  15. ServiceName string `yaml:"service_name"`
  16. InstanceName string `yaml:"instance_name"`
  17. ReadTimeout int `yaml:"read_timeout"`
  18. WriteTimeout int `yaml:"write_timeout"`
  19. IdleTimeout int `yaml:"idle_timeout"`
  20. }
  21. // SetDefaults 设置默认值 - 实现 ConfigLoader 接口
  22. func (c *ServiceConfig) SetDefaults() {
  23. if c.AppEnv == "" {
  24. c.AppEnv = "dev"
  25. }
  26. if c.Port == 0 {
  27. c.Port = 8080
  28. }
  29. if c.IdleTimeout == 0 {
  30. c.IdleTimeout = 60
  31. }
  32. if c.ReadTimeout == 0 {
  33. c.ReadTimeout = 30
  34. }
  35. if c.WriteTimeout == 0 {
  36. c.WriteTimeout = 30
  37. }
  38. }
  39. // Load 从yaml数据加载 - 实现 ConfigLoader 接口
  40. func (c *ServiceConfig) Load(data map[string]interface{}) error {
  41. // 虽然可能不会被直接调用,但为了接口完整性还是要实现
  42. return c.LoadFromYAML(data, c)
  43. }
  44. // ========== 业务方法 ==========
  45. // Validate 验证配置(数据库配置)
  46. func (c *ServiceConfig) Validate() error {
  47. if c.Port <= 0 || c.Port > 65535 {
  48. return fmt.Errorf("invalid service port: %d", c.Port)
  49. }
  50. return nil
  51. }
  52. // IsConfigured 判断是否已配置(数据库配置)
  53. func (c *ServiceConfig) IsConfigured() bool {
  54. if c.Port <= 0 {
  55. log.Println("⚠️ 警告: 微服务 Port 未配置或无效")
  56. return false
  57. }
  58. return true
  59. }
  60. // 自动注册
  61. func init() {
  62. Register("service", &ServiceConfig{})
  63. }