Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

service_config.go 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. Username string `yaml:"username" desc:"Basic认证用户名称"`
  15. Password string `yaml:"password" desc:"Basic认证用户密码"`
  16. SecretKey string `yaml:"secret_key" desc:"当前微服务解密token的密钥"`
  17. Port int `yaml:"port" desc:"服务监听端口"`
  18. ServiceName string `yaml:"service_name" desc:"服务名称"`
  19. InstanceName string `yaml:"instance_name" desc:"服务实例名称"`
  20. ReadTimeout int `yaml:"read_timeout" desc:"读取超时时间(秒)"`
  21. WriteTimeout int `yaml:"write_timeout" desc:"写入超时时间(秒)"`
  22. IdleTimeout int `yaml:"idle_timeout" desc:"空闲连接超时时间(秒)"`
  23. }
  24. func (c *ServiceConfig) Description() string {
  25. return "基础服务配置,包含应用信息、端口和超时设置"
  26. }
  27. // SetDefaults 设置默认值 - 实现 ConfigLoader 接口
  28. func (c *ServiceConfig) SetDefaults() {
  29. if c.AppEnv == "" {
  30. c.AppEnv = "dev"
  31. }
  32. if c.SecretKey == "" {
  33. c.SecretKey = "qwudndgzvxdypdoqd1bhdcdd1qqwzxpoew"
  34. }
  35. if c.Port == 0 {
  36. c.Port = 8080
  37. }
  38. if c.IdleTimeout == 0 {
  39. c.IdleTimeout = 60
  40. }
  41. if c.ReadTimeout == 0 {
  42. c.ReadTimeout = 30
  43. }
  44. if c.WriteTimeout == 0 {
  45. c.WriteTimeout = 30
  46. }
  47. }
  48. // Load 从yaml数据加载 - 实现 ConfigLoader 接口
  49. func (c *ServiceConfig) Load(data map[string]interface{}) error {
  50. // 虽然可能不会被直接调用,但为了接口完整性还是要实现
  51. return c.LoadFromYAML(data, c)
  52. }
  53. // ========== 业务方法 ==========
  54. // Validate 验证配置(数据库配置)
  55. func (c *ServiceConfig) Validate() error {
  56. if c.Port <= 0 || c.Port > 65535 {
  57. return fmt.Errorf("invalid service port: %d", c.Port)
  58. }
  59. return nil
  60. }
  61. // IsConfigured 判断是否已配置(数据库配置)
  62. func (c *ServiceConfig) IsConfigured() bool {
  63. if c.Port <= 0 {
  64. log.Println("⚠️ 警告: 微服务 Port 未配置或无效")
  65. return false
  66. }
  67. return true
  68. }
  69. // 自动注册
  70. func init() {
  71. core.Register("service", &ServiceConfig{})
  72. }