|
|
@@ -1,49 +1,228 @@
|
|
|
1
|
+// subconfigs/service_config.go
|
|
1
|
2
|
package subconfigs
|
|
2
|
3
|
|
|
3
|
|
-import "fmt"
|
|
|
4
|
+import (
|
|
|
5
|
+ "fmt"
|
|
|
6
|
+ "log"
|
|
|
7
|
+)
|
|
4
|
8
|
|
|
5
|
|
-// ServiceConfig 微服务配置
|
|
|
9
|
+type ServicesConfig struct {
|
|
|
10
|
+ BaseConfig
|
|
|
11
|
+ Services map[string]*ServiceConfig `yaml:"services"`
|
|
|
12
|
+}
|
|
|
13
|
+
|
|
|
14
|
+// DatabaseConfig 数据库配置
|
|
6
|
15
|
type ServiceConfig struct {
|
|
7
|
16
|
BaseConfig
|
|
8
|
|
- ServiceName string `yaml:"service_name"`
|
|
9
|
|
- ServiceVersion string `yaml:"service_version"`
|
|
10
|
|
- ServiceTag string `yaml:"service_tags"`
|
|
11
|
|
- Port int `yaml:"port"`
|
|
12
|
|
- ReadTimeout int `yaml:"read_timeout"`
|
|
13
|
|
- WriteTimeout int `yaml:"write_timeout"`
|
|
14
|
|
- IdleTimeout int `yaml:"idle_timeout"`
|
|
15
|
|
- TrustedProxies string `yaml:"trusted_proxies"`
|
|
16
|
|
- InstanceName string `yaml:"instance-name"`
|
|
17
|
|
- Env string `yaml:"env"`
|
|
|
17
|
+ Port int `yaml:"port"`
|
|
|
18
|
+ ServiceName string `yaml:"service_name"`
|
|
|
19
|
+ InstanceName string `yaml:"instance_name"`
|
|
|
20
|
+ ReadTimeout int `yaml:"read_timeout"`
|
|
|
21
|
+ WriteTimeout int `yaml:"write_timeout"`
|
|
|
22
|
+ IdleTimeout int `yaml:"idle_timeout"`
|
|
18
|
23
|
}
|
|
19
|
24
|
|
|
20
|
|
-func NewServiceConfig() *ServiceConfig {
|
|
21
|
|
- return &ServiceConfig{}
|
|
|
25
|
+// SetDefaults 设置默认值 - 实现 ConfigLoader 接口
|
|
|
26
|
+func (c *ServicesConfig) SetDefaults() {
|
|
|
27
|
+ //因为是多集合,外层调这里赋默认值,这个时候对象还没有建立。赋默认值的地方移到其他地方去
|
|
22
|
28
|
}
|
|
23
|
29
|
|
|
|
30
|
+func (c *ServicesConfig) Load(data map[string]interface{}) error {
|
|
|
31
|
+ // 初始化
|
|
|
32
|
+ c.Services = make(map[string]*ServiceConfig)
|
|
|
33
|
+
|
|
|
34
|
+ // 遍历所有配置
|
|
|
35
|
+ for dbName, dbData := range data {
|
|
|
36
|
+ dbDataMap, ok := dbData.(map[interface{}]interface{})
|
|
|
37
|
+ if !ok {
|
|
|
38
|
+ return fmt.Errorf("service '%s' config is not a map", dbName)
|
|
|
39
|
+ }
|
|
|
40
|
+
|
|
|
41
|
+ // 转换为 map[string]interface{}
|
|
|
42
|
+ stringMap := make(map[string]interface{})
|
|
|
43
|
+ for k, v := range dbDataMap {
|
|
|
44
|
+ if key, ok := k.(string); ok {
|
|
|
45
|
+ stringMap[key] = v
|
|
|
46
|
+ }
|
|
|
47
|
+ }
|
|
|
48
|
+
|
|
|
49
|
+ // 创建 ServiceConfig 对象并设置默认值
|
|
|
50
|
+ dbConfig := &ServiceConfig{}
|
|
|
51
|
+ dbConfig.SetDefaults() // 先设置默认值
|
|
|
52
|
+
|
|
|
53
|
+ // 加载单个服务配置
|
|
|
54
|
+ if err := dbConfig.Load(stringMap); err != nil {
|
|
|
55
|
+ return fmt.Errorf("failed to load servuce '%s' config: %v", dbName, err)
|
|
|
56
|
+ }
|
|
|
57
|
+
|
|
|
58
|
+ c.Services[dbName] = dbConfig
|
|
|
59
|
+ }
|
|
|
60
|
+
|
|
|
61
|
+ // 设置默认值和验证
|
|
|
62
|
+ //c.SetDefaults()
|
|
|
63
|
+ return c.Validate()
|
|
|
64
|
+}
|
|
|
65
|
+
|
|
|
66
|
+// SetDefaults 设置默认值 - 实现 ConfigLoader 接口
|
|
24
|
67
|
func (c *ServiceConfig) SetDefaults() {
|
|
25
|
|
- c.ServiceName = "myService"
|
|
26
|
|
- c.Port = 8080
|
|
27
|
|
- c.ReadTimeout = 30
|
|
28
|
|
- c.WriteTimeout = 30
|
|
29
|
|
- c.IdleTimeout = 60
|
|
|
68
|
+
|
|
|
69
|
+ if c.Port == 0 {
|
|
|
70
|
+ c.Port = 8080
|
|
|
71
|
+ }
|
|
|
72
|
+ if c.IdleTimeout == 0 {
|
|
|
73
|
+ c.IdleTimeout = 60
|
|
|
74
|
+ }
|
|
|
75
|
+ if c.ReadTimeout == 0 {
|
|
|
76
|
+ c.ReadTimeout = 30
|
|
|
77
|
+ }
|
|
|
78
|
+ if c.WriteTimeout == 0 {
|
|
|
79
|
+ c.WriteTimeout = 30
|
|
|
80
|
+ }
|
|
30
|
81
|
}
|
|
31
|
82
|
|
|
|
83
|
+// Load 从yaml数据加载 - 实现 ConfigLoader 接口
|
|
32
|
84
|
func (c *ServiceConfig) Load(data map[string]interface{}) error {
|
|
|
85
|
+ // 虽然可能不会被直接调用,但为了接口完整性还是要实现
|
|
33
|
86
|
return c.LoadFromYAML(data, c)
|
|
34
|
87
|
}
|
|
35
|
88
|
|
|
|
89
|
+// ========== 业务方法 ==========
|
|
|
90
|
+
|
|
|
91
|
+// Validate 验证配置(数据库配置)
|
|
36
|
92
|
func (c *ServiceConfig) Validate() error {
|
|
37
|
|
- if c.ServiceName == "" {
|
|
38
|
|
- return fmt.Errorf("service name is required")
|
|
39
|
|
- }
|
|
|
93
|
+
|
|
40
|
94
|
if c.Port <= 0 || c.Port > 65535 {
|
|
41
|
|
- return fmt.Errorf("invalid port: %d", c.Port)
|
|
|
95
|
+ return fmt.Errorf("invalid service port: %d", c.Port)
|
|
42
|
96
|
}
|
|
|
97
|
+
|
|
43
|
98
|
return nil
|
|
44
|
99
|
}
|
|
45
|
100
|
|
|
|
101
|
+// Validate 验证配置(多数据库配置)
|
|
|
102
|
+func (c *ServicesConfig) Validate() error {
|
|
|
103
|
+ if c.Services == nil {
|
|
|
104
|
+ log.Println("❌ 错误: 未找到任何微服务配置,请检查配置文件中的 'services' 配置")
|
|
|
105
|
+ return fmt.Errorf("no services configurations found")
|
|
|
106
|
+ }
|
|
|
107
|
+
|
|
|
108
|
+ // 必须要有默认数据库
|
|
|
109
|
+ if _, exists := c.Services["default"]; !exists {
|
|
|
110
|
+ availableDBs := c.GetAllServiceNames()
|
|
|
111
|
+ log.Printf("❌ 错误: 默认微服务配置未找到。可用的微服务配置: %v。请在配置文件中添加 'default' 微服务", availableDBs)
|
|
|
112
|
+ return fmt.Errorf("default service not found")
|
|
|
113
|
+ }
|
|
|
114
|
+
|
|
|
115
|
+ // 验证每个数据库配置
|
|
|
116
|
+ for name, db := range c.Services {
|
|
|
117
|
+ if db == nil {
|
|
|
118
|
+ log.Printf("❌ 错误: 微服务 '%s' 配置为空", name)
|
|
|
119
|
+ return fmt.Errorf("service '%s' configuration is nil", name)
|
|
|
120
|
+ }
|
|
|
121
|
+
|
|
|
122
|
+ if err := db.Validate(); err != nil {
|
|
|
123
|
+ log.Printf("❌ 错误: 微服务 '%s' 验证失败: %v", name, err)
|
|
|
124
|
+ return fmt.Errorf("service '%s' validation failed: %v", name, err)
|
|
|
125
|
+ }
|
|
|
126
|
+ }
|
|
|
127
|
+
|
|
|
128
|
+ log.Printf("✅ 微服务配置验证通过,找到 %d 个微服务配置", len(c.Services))
|
|
|
129
|
+ return nil
|
|
|
130
|
+}
|
|
|
131
|
+
|
|
|
132
|
+// IsConfigured 判断是否已配置(数据库配置)
|
|
|
133
|
+func (c *ServiceConfig) IsConfigured() bool {
|
|
|
134
|
+
|
|
|
135
|
+ if c.Port <= 0 {
|
|
|
136
|
+ log.Println("⚠️ 警告: 微服务 Port 未配置或无效")
|
|
|
137
|
+ return false
|
|
|
138
|
+ }
|
|
|
139
|
+
|
|
|
140
|
+ return true
|
|
|
141
|
+}
|
|
|
142
|
+
|
|
|
143
|
+// IsConfigured 判断是否已配置(多数据库配置)
|
|
|
144
|
+func (c *ServicesConfig) IsConfigured() bool {
|
|
|
145
|
+ defaultDB := c.GetDefaultService()
|
|
|
146
|
+ if defaultDB == nil {
|
|
|
147
|
+ log.Println("❌ 错误: 默认微服务不存在")
|
|
|
148
|
+ return false
|
|
|
149
|
+ }
|
|
|
150
|
+
|
|
|
151
|
+ if !defaultDB.IsConfigured() {
|
|
|
152
|
+ log.Println("❌ 错误: 默认微服务配置不完整")
|
|
|
153
|
+ return false
|
|
|
154
|
+ }
|
|
|
155
|
+
|
|
|
156
|
+ return true
|
|
|
157
|
+}
|
|
|
158
|
+
|
|
|
159
|
+// GetDefaultService 获取默认微服务(必须存在)
|
|
|
160
|
+func (c *ServicesConfig) GetDefaultService() *ServiceConfig {
|
|
|
161
|
+ if c.Services == nil {
|
|
|
162
|
+ log.Println("❌ 错误: 尝试获取默认微服务时,微服务配置为空")
|
|
|
163
|
+ return nil
|
|
|
164
|
+ }
|
|
|
165
|
+
|
|
|
166
|
+ if db, exists := c.Services["default"]; exists {
|
|
|
167
|
+ return db
|
|
|
168
|
+ }
|
|
|
169
|
+
|
|
|
170
|
+ // 如果没有名为default的,尝试返回第一个(但会记录警告)
|
|
|
171
|
+ for name, db := range c.Services {
|
|
|
172
|
+ log.Printf("⚠️ 警告: 未找到名为 'default' 的微服务,使用第一个微服务 '%s' 作为默认", name)
|
|
|
173
|
+ return db
|
|
|
174
|
+ }
|
|
|
175
|
+
|
|
|
176
|
+ log.Println("❌ 错误: 服务模块配置中没有任何配置")
|
|
|
177
|
+ return nil
|
|
|
178
|
+}
|
|
|
179
|
+
|
|
|
180
|
+// GetService 按名称获取微服务(为空时记录错误)
|
|
|
181
|
+func (c *ServicesConfig) GetService(name string) *ServiceConfig {
|
|
|
182
|
+ if c.Services == nil {
|
|
|
183
|
+ log.Printf("❌ 错误: 尝试获取微服务 '%s' 时,微服务配置为空", name)
|
|
|
184
|
+ return nil
|
|
|
185
|
+ }
|
|
|
186
|
+
|
|
|
187
|
+ db := c.Services[name]
|
|
|
188
|
+ if db == nil {
|
|
|
189
|
+ availableDBs := c.GetAllServiceNames()
|
|
|
190
|
+ log.Printf("❌ 错误: 微服务 '%s' 不存在。可用的微服务: %v", name, availableDBs)
|
|
|
191
|
+ }
|
|
|
192
|
+
|
|
|
193
|
+ return db
|
|
|
194
|
+}
|
|
|
195
|
+
|
|
|
196
|
+// GetAllServiceNames 获取所有数据库名称
|
|
|
197
|
+func (c *ServicesConfig) GetAllServiceNames() []string {
|
|
|
198
|
+ if c.Services == nil {
|
|
|
199
|
+ log.Println("⚠️ 警告: 尝试获取微服务配置节点名称时,微服务配置为空")
|
|
|
200
|
+ return []string{}
|
|
|
201
|
+ }
|
|
|
202
|
+
|
|
|
203
|
+ names := make([]string, 0, len(c.Services))
|
|
|
204
|
+ for name := range c.Services {
|
|
|
205
|
+ names = append(names, name)
|
|
|
206
|
+ }
|
|
|
207
|
+ return names
|
|
|
208
|
+}
|
|
|
209
|
+
|
|
|
210
|
+// GetServiceNamesWithoutDefault 获取除default外的所有微服务配置节点名称
|
|
|
211
|
+func (c *ServicesConfig) GetServiceNamesWithoutDefault() []string {
|
|
|
212
|
+ if c.Services == nil {
|
|
|
213
|
+ return []string{}
|
|
|
214
|
+ }
|
|
|
215
|
+
|
|
|
216
|
+ names := make([]string, 0, len(c.Services)-1)
|
|
|
217
|
+ for name := range c.Services {
|
|
|
218
|
+ if name != "default" {
|
|
|
219
|
+ names = append(names, name)
|
|
|
220
|
+ }
|
|
|
221
|
+ }
|
|
|
222
|
+ return names
|
|
|
223
|
+}
|
|
|
224
|
+
|
|
46
|
225
|
// 自动注册
|
|
47
|
226
|
func init() {
|
|
48
|
|
- Register("service", &ServiceConfig{})
|
|
|
227
|
+ Register("services", &ServicesConfig{})
|
|
49
|
228
|
}
|