| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- // subconfigs/service_config.go
- package subconfigs
-
- import (
- "fmt"
- "log"
-
- "git.x2erp.com/qdy/go-base/config/core"
- )
-
- // ServiceConfig 服务配置
- type ServiceConfig struct {
- core.BaseConfig
- AppName string `yaml:"app_name" desc:"当前应用名称"`
- AppVersion string `yaml:"app_version" desc:"当前应用版本"`
- AppEnv string `yaml:"app_env" desc:"环境参数:dev, test, prod"`
-
- Username string `yaml:"username" desc:"Basic认证用户名称"`
- Password string `yaml:"password" desc:"Basic认证用户密码"`
- SecretKey string `yaml:"secret_key" desc:"当前微服务解密token的密钥"`
-
- Port int `yaml:"port" desc:"服务监听端口"`
- ServiceName string `yaml:"service_name" desc:"服务名称"`
- InstanceName string `yaml:"instance_name" desc:"服务实例名称"`
- ReadTimeout int `yaml:"read_timeout" desc:"读取超时时间(秒)"`
- WriteTimeout int `yaml:"write_timeout" desc:"写入超时时间(秒)"`
- IdleTimeout int `yaml:"idle_timeout" desc:"空闲连接超时时间(秒)"`
- }
-
- func (c *ServiceConfig) Description() string {
- return "基础服务配置,包含应用信息、端口和超时设置"
- }
-
- // SetDefaults 设置默认值 - 实现 ConfigLoader 接口
- func (c *ServiceConfig) SetDefaults() {
-
- if c.AppEnv == "" {
- c.AppEnv = "dev"
- }
-
- if c.SecretKey == "" {
- c.SecretKey = "qwudndgzvxdypdoqd1bhdcdd1qqwzxpoew"
- }
-
- if c.Port == 0 {
- c.Port = 8080
- }
- if c.IdleTimeout == 0 {
- c.IdleTimeout = 60
- }
- if c.ReadTimeout == 0 {
- c.ReadTimeout = 30
- }
- if c.WriteTimeout == 0 {
- c.WriteTimeout = 30
- }
- }
-
- // Load 从yaml数据加载 - 实现 ConfigLoader 接口
- func (c *ServiceConfig) Load(data map[string]interface{}) error {
- // 虽然可能不会被直接调用,但为了接口完整性还是要实现
- return c.LoadFromYAML(data, c)
- }
-
- // ========== 业务方法 ==========
-
- // Validate 验证配置(数据库配置)
- func (c *ServiceConfig) Validate() error {
-
- if c.Port <= 0 || c.Port > 65535 {
- return fmt.Errorf("invalid service port: %d", c.Port)
- }
-
- return nil
- }
-
- // IsConfigured 判断是否已配置(数据库配置)
- func (c *ServiceConfig) IsConfigured() bool {
-
- if c.Port <= 0 {
- log.Println("⚠️ 警告: 微服务 Port 未配置或无效")
- return false
- }
-
- return true
- }
-
- // 自动注册
- func init() {
- core.Register("service", &ServiceConfig{})
- }
|