暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

service_config.go 2.1KB

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