Kaynağa Gözat

增加自动获取所有配置文件方法,这样可以注册到数据库

qdy 1 ay önce
ebeveyn
işleme
1bb7036e1f

+ 14
- 13
config/config.go Dosyayı Görüntüle

@@ -3,6 +3,7 @@ package config
3 3
 import (
4 4
 	"sync"
5 5
 
6
+	"git.x2erp.com/qdy/go-base/config/core"
6 7
 	"git.x2erp.com/qdy/go-base/config/subconfigs"
7 8
 )
8 9
 
@@ -63,7 +64,7 @@ type Config struct {
63 64
 
64 65
 // GetDatabaseConfig 按名称获取数据库配置
65 66
 func (c *Config) GetDatabaseConfig() *subconfigs.DatabaseConfig {
66
-	if config := subconfigs.GetRegisteredConfig("database"); config != nil {
67
+	if config := core.GetRegisteredConfig("database"); config != nil {
67 68
 		return config.(*subconfigs.DatabaseConfig)
68 69
 	}
69 70
 	return nil
@@ -72,7 +73,7 @@ func (c *Config) GetDatabaseConfig() *subconfigs.DatabaseConfig {
72 73
 // GetDbsConfig 按名称获取数据库配置
73 74
 func (c *Config) GetDbsConfig(name string) (*subconfigs.DatabaseConfig, bool) {
74 75
 	// 获取 dbs 配置
75
-	dbsConfig := subconfigs.GetRegisteredConfig("dbs")
76
+	dbsConfig := core.GetRegisteredConfig("dbs")
76 77
 	if dbsConfig == nil {
77 78
 		return nil, false
78 79
 	}
@@ -91,76 +92,76 @@ func (c *Config) GetDbsConfig(name string) (*subconfigs.DatabaseConfig, bool) {
91 92
 
92 93
 	// 将 DbConfig 转换为 DatabaseConfig
93 94
 	return &subconfigs.DatabaseConfig{
94
-		BaseConfig: subconfigs.BaseConfig{},
95
+		BaseConfig: core.BaseConfig{},
95 96
 		DbConfig:   *dbConfig,
96 97
 	}, true
97 98
 }
98 99
 
99 100
 func (c *Config) GetRedisConfig() *subconfigs.RedisConfig {
100
-	if config := subconfigs.GetRegisteredConfig("redis"); config != nil {
101
+	if config := core.GetRegisteredConfig("redis"); config != nil {
101 102
 		return config.(*subconfigs.RedisConfig)
102 103
 	}
103 104
 	return nil
104 105
 }
105 106
 
106 107
 func (c *Config) GetConsulConfig() *subconfigs.ConsulConfig {
107
-	if config := subconfigs.GetRegisteredConfig("consul"); config != nil {
108
+	if config := core.GetRegisteredConfig("consul"); config != nil {
108 109
 		return config.(*subconfigs.ConsulConfig)
109 110
 	}
110 111
 	return nil
111 112
 }
112 113
 
113 114
 func (c *Config) GetMongoDBConfig() *subconfigs.MongoDBConfig {
114
-	if config := subconfigs.GetRegisteredConfig("mongodb"); config != nil {
115
+	if config := core.GetRegisteredConfig("mongodb"); config != nil {
115 116
 		return config.(*subconfigs.MongoDBConfig)
116 117
 	}
117 118
 	return nil
118 119
 }
119 120
 
120 121
 func (c *Config) GetDorisConfig() *subconfigs.DorisConfig {
121
-	if config := subconfigs.GetRegisteredConfig("doris"); config != nil {
122
+	if config := core.GetRegisteredConfig("doris"); config != nil {
122 123
 		return config.(*subconfigs.DorisConfig)
123 124
 	}
124 125
 	return nil
125 126
 }
126 127
 
127 128
 func (c *Config) GetRabbitMQConfig() *subconfigs.RabbitMQConfig {
128
-	if config := subconfigs.GetRegisteredConfig("rabbitmq"); config != nil {
129
+	if config := core.GetRegisteredConfig("rabbitmq"); config != nil {
129 130
 		return config.(*subconfigs.RabbitMQConfig)
130 131
 	}
131 132
 	return nil
132 133
 }
133 134
 
134 135
 func (c *Config) GetServiceConfig() *subconfigs.ServiceConfig {
135
-	if config := subconfigs.GetRegisteredConfig("service"); config != nil {
136
+	if config := core.GetRegisteredConfig("service"); config != nil {
136 137
 		return config.(*subconfigs.ServiceConfig)
137 138
 	}
138 139
 	return nil
139 140
 }
140 141
 
141 142
 func (c *Config) GetMcpServiceConfig() *subconfigs.McpServiceConfig {
142
-	if config := subconfigs.GetRegisteredConfig("mcpservice"); config != nil {
143
+	if config := core.GetRegisteredConfig("mcpservice"); config != nil {
143 144
 		return config.(*subconfigs.McpServiceConfig)
144 145
 	}
145 146
 	return nil
146 147
 }
147 148
 
148 149
 func (c *Config) GetHTTPConfig() *subconfigs.HTTPConfig {
149
-	if config := subconfigs.GetRegisteredConfig("http"); config != nil {
150
+	if config := core.GetRegisteredConfig("http"); config != nil {
150 151
 		return config.(*subconfigs.HTTPConfig)
151 152
 	}
152 153
 	return nil
153 154
 }
154 155
 
155 156
 func (c *Config) GetMicroConfig() *subconfigs.MicroConfig {
156
-	if config := subconfigs.GetRegisteredConfig("micro"); config != nil {
157
+	if config := core.GetRegisteredConfig("micro"); config != nil {
157 158
 		return config.(*subconfigs.MicroConfig)
158 159
 	}
159 160
 	return nil
160 161
 }
161 162
 
162 163
 func (c *Config) GetLogConfig() *subconfigs.LogConfig {
163
-	if config := subconfigs.GetRegisteredConfig("log"); config != nil {
164
+	if config := core.GetRegisteredConfig("log"); config != nil {
164 165
 		return config.(*subconfigs.LogConfig)
165 166
 	}
166 167
 	return nil

config/subconfigs/base.go → config/core/core_base_config.go Dosyayı Görüntüle

@@ -1,4 +1,4 @@
1
-package subconfigs
1
+package core
2 2
 
3 3
 import (
4 4
 	"gopkg.in/yaml.v2"

+ 182
- 0
config/core/core_metadata_config.go Dosyayı Görüntüle

@@ -0,0 +1,182 @@
1
+package core
2
+
3
+import (
4
+	"reflect"
5
+	"strings"
6
+)
7
+
8
+// ConfigMeta 配置元信息
9
+type ConfigMeta struct {
10
+	Name   string               `json:"name"`   // 配置名称,如 "consul"
11
+	Desc   string               `json:"desc"`   // 配置描述
12
+	Fields map[string]FieldMeta `json:"fields"` // 字段信息
13
+}
14
+
15
+// FieldMeta 字段元信息
16
+type FieldMeta struct {
17
+	Type    string `json:"type"`    // 字段类型,如 "string"
18
+	YamlTag string `json:"yamlTag"` // YAML标签,如 "address"
19
+	Desc    string `json:"desc"`    // 字段描述
20
+}
21
+
22
+// GetAllConfigsMeta 获取所有配置的元信息
23
+func GetAllConfigsMeta() map[string]*ConfigMeta {
24
+	// 获取所有配置实例
25
+	allConfigs := GetAllConfigs()
26
+	result := make(map[string]*ConfigMeta, len(allConfigs))
27
+
28
+	// 遍历所有配置
29
+	for name, config := range allConfigs {
30
+		meta := getConfigMeta(name, config)
31
+		result[name] = meta
32
+	}
33
+
34
+	return result
35
+}
36
+
37
+// GetConfigMeta 获取单个配置的元信息
38
+func GetConfigMeta(name string) (*ConfigMeta, error) {
39
+	config := GetConfig(name)
40
+	if config == nil {
41
+		return nil, nil
42
+	}
43
+
44
+	return getConfigMeta(name, config), nil
45
+}
46
+
47
+// getConfigMeta 内部方法:获取配置元信息
48
+func getConfigMeta(name string, config ConfigLoader) *ConfigMeta {
49
+	meta := &ConfigMeta{
50
+		Name:   name,
51
+		Desc:   config.Description(),
52
+		Fields: make(map[string]FieldMeta),
53
+	}
54
+
55
+	// 使用反射获取类型信息
56
+	t := reflect.TypeOf(config)
57
+
58
+	// 如果是指针类型,获取其指向的类型
59
+	if t.Kind() == reflect.Ptr {
60
+		t = t.Elem()
61
+	}
62
+
63
+	// 遍历所有字段
64
+	for i := 0; i < t.NumField(); i++ {
65
+		field := t.Field(i)
66
+		yamlTag := field.Tag.Get("yaml")
67
+
68
+		// 跳过BaseConfig这种匿名字段(除非是inline)
69
+		if field.Anonymous && !strings.Contains(yamlTag, "inline") {
70
+			continue
71
+		}
72
+
73
+		// 处理inline字段
74
+		if strings.Contains(yamlTag, "inline") {
75
+			// 获取字段类型
76
+			fieldType := field.Type
77
+
78
+			// 如果是map类型,提取map值的结构体信息
79
+			if fieldType.Kind() == reflect.Map {
80
+				// 获取map的值类型
81
+				valueType := fieldType.Elem()
82
+
83
+				// 如果是指针类型,获取指向的类型
84
+				if valueType.Kind() == reflect.Ptr {
85
+					valueType = valueType.Elem()
86
+				}
87
+
88
+				// 如果值类型是结构体,提取其字段
89
+				if valueType.Kind() == reflect.Struct {
90
+					// 创建FieldMeta
91
+					fieldMeta := FieldMeta{
92
+						Type:    field.Type.String(),
93
+						YamlTag: yamlTag,
94
+						Desc:    field.Tag.Get("desc"),
95
+					}
96
+
97
+					// 提取结构体字段
98
+					valueFields := make(map[string]FieldMeta)
99
+					extractStructFields(valueType, valueFields)
100
+
101
+					// 这里可以添加一个子字段信息,或者直接合并到主字段
102
+					// 为了简单,我们直接合并
103
+					for name, fm := range valueFields {
104
+						meta.Fields[name] = fm
105
+					}
106
+
107
+					// 同时保留map字段本身
108
+					meta.Fields[field.Name] = fieldMeta
109
+				} else {
110
+					// 不是结构体类型,只显示map本身
111
+					meta.Fields[field.Name] = FieldMeta{
112
+						Type:    field.Type.String(),
113
+						YamlTag: yamlTag,
114
+						Desc:    field.Tag.Get("desc"),
115
+					}
116
+				}
117
+			} else if fieldType.Kind() == reflect.Struct ||
118
+				(fieldType.Kind() == reflect.Ptr && fieldType.Elem().Kind() == reflect.Struct) {
119
+				// 对于结构体类型(或指向结构体的指针),递归提取内联结构体的字段
120
+				if fieldType.Kind() == reflect.Ptr {
121
+					fieldType = fieldType.Elem()
122
+				}
123
+				extractInlineFields(fieldType, meta.Fields)
124
+			} else {
125
+				// 其他类型
126
+				meta.Fields[field.Name] = FieldMeta{
127
+					Type:    field.Type.String(),
128
+					YamlTag: yamlTag,
129
+					Desc:    field.Tag.Get("desc"),
130
+				}
131
+			}
132
+			continue
133
+		}
134
+
135
+		// 处理普通字段
136
+		meta.Fields[field.Name] = FieldMeta{
137
+			Type:    field.Type.String(),
138
+			YamlTag: yamlTag,
139
+			Desc:    field.Tag.Get("desc"),
140
+		}
141
+	}
142
+
143
+	return meta
144
+}
145
+
146
+// extractInlineFields 提取内联结构体的字段
147
+func extractInlineFields(t reflect.Type, fields map[string]FieldMeta) {
148
+	// 遍历结构体的字段
149
+	for i := 0; i < t.NumField(); i++ {
150
+		field := t.Field(i)
151
+
152
+		// 跳过匿名字段
153
+		if field.Anonymous {
154
+			continue
155
+		}
156
+
157
+		fields[field.Name] = FieldMeta{
158
+			Type:    field.Type.String(),
159
+			YamlTag: field.Tag.Get("yaml"),
160
+			Desc:    field.Tag.Get("desc"),
161
+		}
162
+	}
163
+}
164
+
165
+// extractStructFields 提取结构体字段
166
+func extractStructFields(t reflect.Type, fields map[string]FieldMeta) {
167
+	// 遍历结构体的字段
168
+	for i := 0; i < t.NumField(); i++ {
169
+		field := t.Field(i)
170
+
171
+		// 跳过匿名字段
172
+		if field.Anonymous {
173
+			continue
174
+		}
175
+
176
+		fields[field.Name] = FieldMeta{
177
+			Type:    field.Type.String(),
178
+			YamlTag: field.Tag.Get("yaml"),
179
+			Desc:    field.Tag.Get("desc"),
180
+		}
181
+	}
182
+}

config/subconfigs/register_config.go → config/core/core_register_config.go Dosyayı Görüntüle

@@ -1,9 +1,10 @@
1
-package subconfigs
1
+package core
2 2
 
3 3
 // ConfigLoader 配置加载接口
4 4
 type ConfigLoader interface {
5 5
 	SetDefaults()
6 6
 	Load(data map[string]interface{}) error
7
+	Description() string // 新增:返回配置描述
7 8
 	//Validate() error
8 9
 }
9 10
 

+ 3
- 3
config/loader.go Dosyayı Görüntüle

@@ -6,7 +6,7 @@ import (
6 6
 	"os"
7 7
 	"path/filepath"
8 8
 
9
-	"git.x2erp.com/qdy/go-base/config/subconfigs"
9
+	"git.x2erp.com/qdy/go-base/config/core"
10 10
 	"gopkg.in/yaml.v2"
11 11
 )
12 12
 
@@ -40,12 +40,12 @@ func LoadConfig() {
40 40
 // 新增方法,供外部调用(比如从数据库加载后使用)
41 41
 func LoadConfigFromMap(rawConfig map[string]interface{}) {
42 42
 	// 1. 设置所有注册配置的默认值
43
-	for _, config := range subconfigs.GetAllConfigs() {
43
+	for _, config := range core.GetAllConfigs() {
44 44
 		config.SetDefaults()
45 45
 	}
46 46
 
47 47
 	// 2. 循环注册表,为每个配置加载数据
48
-	for name, config := range subconfigs.GetAllConfigs() {
48
+	for name, config := range core.GetAllConfigs() {
49 49
 		if configData, ok := rawConfig[name].(map[interface{}]interface{}); ok {
50 50
 			// 转换为 map[string]interface{}
51 51
 			strMap := convertMap(configData)

+ 15
- 7
config/subconfigs/consul_config.go Dosyayı Görüntüle

@@ -1,13 +1,22 @@
1 1
 package subconfigs
2 2
 
3
-import "fmt"
3
+import (
4
+	"fmt"
5
+
6
+	"git.x2erp.com/qdy/go-base/config/core"
7
+)
4 8
 
5 9
 // Config Consul客户端配置
6 10
 type ConsulConfig struct {
7
-	BaseConfig
8
-	Address string `yaml:"address"` // Consul服务器地址,如:"localhost:8500"
9
-	Token   string `yaml:"token"`   // ACL token(可选)
10
-	Scheme  string `yaml:"scheme"`  // "http" 或 "https"
11
+	core.BaseConfig
12
+	Address string `yaml:"address" desc:"Consul服务器地址,如:localhost:8500"`
13
+	Token   string `yaml:"token" desc:"ACL token(可选)"`
14
+	Scheme  string `yaml:"scheme" desc:"http 或 https"`
15
+}
16
+
17
+// consul.go
18
+func (c *ConsulConfig) Description() string {
19
+	return "Consul客户端配置,用于服务发现和配置管理"
11 20
 }
12 21
 
13 22
 func NewConsulConfig() *ConsulConfig {
@@ -15,7 +24,6 @@ func NewConsulConfig() *ConsulConfig {
15 24
 }
16 25
 
17 26
 func (c *ConsulConfig) SetDefaults() {
18
-	//c.Address = "localhost:8500"
19 27
 	c.Scheme = "http"
20 28
 }
21 29
 
@@ -36,5 +44,5 @@ func (c *ConsulConfig) IsConfigured() bool {
36 44
 
37 45
 // 自动注册
38 46
 func init() {
39
-	Register("consul", &ConsulConfig{})
47
+	core.Register("consul", &ConsulConfig{})
40 48
 }

+ 9
- 9
config/subconfigs/database_common.go Dosyayı Görüntüle

@@ -7,15 +7,15 @@ import (
7 7
 
8 8
 // DbConfig 单个数据库配置(共享结构)
9 9
 type DbConfig struct {
10
-	Type            string `yaml:"type"`
11
-	Host            string `yaml:"host"`
12
-	Port            int    `yaml:"port"`
13
-	Username        string `yaml:"username"`
14
-	Password        string `yaml:"password"`
15
-	Database        string `yaml:"database"`
16
-	MaxOpenConns    int    `yaml:"max_open_conns"`
17
-	MaxIdleConns    int    `yaml:"max_idle_conns"`
18
-	ConnMaxLifetime int    `yaml:"conn_max_lifetime"`
10
+	Type            string `yaml:"type" desc:"数据库类型,如:mysql、postgresql"`
11
+	Host            string `yaml:"host" desc:"数据库主机地址"`
12
+	Port            int    `yaml:"port" desc:"数据库端口"`
13
+	Username        string `yaml:"username" desc:"数据库用户名"`
14
+	Password        string `yaml:"password" desc:"数据库密码"`
15
+	Database        string `yaml:"database" desc:"数据库名称"`
16
+	MaxOpenConns    int    `yaml:"max_open_conns" desc:"最大打开连接数"`
17
+	MaxIdleConns    int    `yaml:"max_idle_conns" desc:"最大空闲连接数"`
18
+	ConnMaxLifetime int    `yaml:"conn_max_lifetime" desc:"连接最大生命周期(秒)"`
19 19
 }
20 20
 
21 21
 // SetDbDefaults 设置数据库配置默认值

+ 10
- 3
config/subconfigs/database_config.go Dosyayı Görüntüle

@@ -1,9 +1,16 @@
1 1
 package subconfigs
2 2
 
3
+import "git.x2erp.com/qdy/go-base/config/core"
4
+
3 5
 // DatabaseConfig 单个数据库配置
4 6
 type DatabaseConfig struct {
5
-	BaseConfig
6
-	DbConfig `yaml:",inline"` // 嵌入共享的 DbConfig
7
+	core.BaseConfig
8
+	DbConfig `yaml:",inline" desc:"数据库连接配置"`
9
+}
10
+
11
+// consul.go
12
+func (c *DatabaseConfig) Description() string {
13
+	return "数据库连接配置"
7 14
 }
8 15
 
9 16
 func (c *DatabaseConfig) SetDefaults() {
@@ -23,5 +30,5 @@ func (c *DatabaseConfig) IsConfigured() bool {
23 30
 }
24 31
 
25 32
 func init() {
26
-	Register("database", &DatabaseConfig{})
33
+	core.Register("database", &DatabaseConfig{})
27 34
 }

+ 8
- 2
config/subconfigs/dbs_config.go Dosyayı Görüntüle

@@ -3,14 +3,20 @@ package subconfigs
3 3
 import (
4 4
 	"fmt"
5 5
 	"log"
6
+
7
+	"git.x2erp.com/qdy/go-base/config/core"
6 8
 )
7 9
 
8 10
 // DbsConfig 多数据库配置
9 11
 type DbsConfig struct {
10
-	BaseConfig
12
+	core.BaseConfig
11 13
 	Databases map[string]*DbConfig `yaml:",inline"`
12 14
 }
13 15
 
16
+func (c *DbsConfig) Description() string {
17
+	return "多个数据库连接配置"
18
+}
19
+
14 20
 func (c *DbsConfig) SetDefaults() {
15 21
 	for _, db := range c.Databases {
16 22
 		SetDbDefaults(db)
@@ -65,5 +71,5 @@ func (c *DbsConfig) Default() *DbConfig {
65 71
 }
66 72
 
67 73
 func init() {
68
-	Register("dbs", &DbsConfig{})
74
+	core.Register("dbs", &DbsConfig{})
69 75
 }

+ 22
- 15
config/subconfigs/doris_config.go Dosyayı Görüntüle

@@ -1,21 +1,28 @@
1 1
 package subconfigs
2 2
 
3
-import "fmt"
3
+import (
4
+	"fmt"
5
+
6
+	"git.x2erp.com/qdy/go-base/config/core"
7
+)
4 8
 
5
-// DorisConfig Doris配置
6 9
 type DorisConfig struct {
7
-	BaseConfig
8
-	FEHost            string `yaml:"fe_host"`
9
-	FEPort            int    `yaml:"fe_port"`
10
-	FEUsername        string `yaml:"fe_username"`
11
-	FEPassword        string `yaml:"fe_password"`
12
-	MySQLHost         string `yaml:"mysql_host"`
13
-	MySQLPort         int    `yaml:"mysql_port"`
14
-	MaxOpenConns      int    `yaml:"max_open_conns"`
15
-	MaxIdleConns      int    `yaml:"max_idle_conns"`
16
-	ConnMaxLifetime   int    `yaml:"conn_max_lifetime"`
17
-	StreamLoadTimeout int    `yaml:"stream_load_timeout"`
18
-	BatchSize         int    `yaml:"batch_size"`
10
+	core.BaseConfig
11
+	FEHost            string `yaml:"fe_host" desc:"Doris FE节点主机地址"`
12
+	FEPort            int    `yaml:"fe_port" desc:"Doris FE节点端口"`
13
+	FEUsername        string `yaml:"fe_username" desc:"Doris FE节点用户名"`
14
+	FEPassword        string `yaml:"fe_password" desc:"Doris FE节点密码"`
15
+	MySQLHost         string `yaml:"mysql_host" desc:"MySQL兼容接口主机地址"`
16
+	MySQLPort         int    `yaml:"mysql_port" desc:"MySQL兼容接口端口"`
17
+	MaxOpenConns      int    `yaml:"max_open_conns" desc:"最大打开连接数"`
18
+	MaxIdleConns      int    `yaml:"max_idle_conns" desc:"最大空闲连接数"`
19
+	ConnMaxLifetime   int    `yaml:"conn_max_lifetime" desc:"连接最大生命周期(秒)"`
20
+	StreamLoadTimeout int    `yaml:"stream_load_timeout" desc:"Stream Load超时时间(秒)"`
21
+	BatchSize         int    `yaml:"batch_size" desc:"批量操作大小"`
22
+}
23
+
24
+func (c *DorisConfig) Description() string {
25
+	return "Doris数据库连接配置"
19 26
 }
20 27
 
21 28
 func NewDorisConfig() *DorisConfig {
@@ -49,5 +56,5 @@ func (c *DorisConfig) IsConfigured() bool {
49 56
 
50 57
 // 自动注册
51 58
 func init() {
52
-	Register("doris", &DorisConfig{})
59
+	core.Register("doris", &DorisConfig{})
53 60
 }

+ 18
- 10
config/subconfigs/http_config.go Dosyayı Görüntüle

@@ -1,17 +1,25 @@
1 1
 package subconfigs
2 2
 
3
-import "fmt"
3
+import (
4
+	"fmt"
5
+
6
+	"git.x2erp.com/qdy/go-base/config/core"
7
+)
4 8
 
5 9
 // HTTPConfig HTTP客户端配置
6 10
 type HTTPConfig struct {
7
-	BaseConfig
8
-	Timeout             int  `yaml:"timeout"`
9
-	MaxIdleConns        int  `yaml:"max_idle_conns"`
10
-	MaxIdleConnsPerHost int  `yaml:"max_idle_conns_per_host"`
11
-	IdleConnTimeout     int  `yaml:"idle_conn_timeout"`
12
-	MaxConnsPerHost     int  `yaml:"max_conns_per_host"`
13
-	DisableCompression  bool `yaml:"disable_compression"`
14
-	DisableKeepAlives   bool `yaml:"disable_keep_alives"`
11
+	core.BaseConfig
12
+	Timeout             int  `yaml:"timeout" desc:"请求超时时间(秒)"`
13
+	MaxIdleConns        int  `yaml:"max_idle_conns" desc:"最大空闲连接数"`
14
+	MaxIdleConnsPerHost int  `yaml:"max_idle_conns_per_host" desc:"每个主机的最大空闲连接数"`
15
+	IdleConnTimeout     int  `yaml:"idle_conn_timeout" desc:"空闲连接超时时间(秒)"`
16
+	MaxConnsPerHost     int  `yaml:"max_conns_per_host" desc:"每个主机的最大连接数"`
17
+	DisableCompression  bool `yaml:"disable_compression" desc:"是否禁用压缩"`
18
+	DisableKeepAlives   bool `yaml:"disable_keep_alives" desc:"是否禁用长连接"`
19
+}
20
+
21
+func (c *HTTPConfig) Description() string {
22
+	return "调用其他api的http服务配置"
15 23
 }
16 24
 
17 25
 func NewHTTPConfig() *HTTPConfig {
@@ -42,5 +50,5 @@ func (c *HTTPConfig) IsConfigured() bool {
42 50
 
43 51
 // 自动注册
44 52
 func init() {
45
-	Register("http", &HTTPConfig{})
53
+	core.Register("http", &HTTPConfig{})
46 54
 }

+ 18
- 12
config/subconfigs/log_config.go Dosyayı Görüntüle

@@ -3,31 +3,37 @@ package subconfigs
3 3
 import (
4 4
 	"fmt"
5 5
 	"strings"
6
+
7
+	"git.x2erp.com/qdy/go-base/config/core"
6 8
 )
7 9
 
8 10
 // LogConfig 日志配置
9 11
 type LogConfig struct {
10
-	BaseConfig
11
-	Level  string `yaml:"level" json:"level"`   // 日志级别
12
-	Output string `yaml:"output" json:"output"` // 输出目标:console,file,es
12
+	core.BaseConfig
13
+	Level  string `yaml:"level" json:"level" desc:"日志级别:debug, info, warn, error"`
14
+	Output string `yaml:"output" json:"output" desc:"输出目标:console,file,es"`
13 15
 	//JSONFormat bool   `yaml:"json_format" json:"json_format"` // 是否JSON格式
14 16
 
15 17
 	// 文件输出配置
16
-	FilePath   string `yaml:"file_path" json:"file_path"`
17
-	MaxSize    int    `yaml:"max_size" json:"max_size"`
18
-	MaxBackups int    `yaml:"max_backups" json:"max_backups"`
19
-	MaxAge     int    `yaml:"max_age" json:"max_age"`
20
-	Compress   bool   `yaml:"compress" json:"compress"`
18
+	FilePath   string `yaml:"file_path" json:"file_path" desc:"日志文件路径"`
19
+	MaxSize    int    `yaml:"max_size" json:"max_size" desc:"单个日志文件最大大小(MB)"`
20
+	MaxBackups int    `yaml:"max_backups" json:"max_backups" desc:"最大保留日志文件数"`
21
+	MaxAge     int    `yaml:"max_age" json:"max_age" desc:"日志文件最大保留天数"`
22
+	Compress   bool   `yaml:"compress" json:"compress" desc:"是否压缩旧日志文件"`
21 23
 
22 24
 	// ES输出配置(当Output包含"es"时生效)
23
-	ESPath     string `yaml:"es_path" json:"es_path"`         // ES地址
24
-	ESUsername string `yaml:"es_username" json:"es_username"` // 用户名(可选)
25
-	ESPassword string `yaml:"es_password" json:"es_password"` // 密码(可选)
25
+	ESPath     string `yaml:"es_path" json:"es_path" desc:"Elasticsearch地址"`
26
+	ESUsername string `yaml:"es_username" json:"es_username" desc:"Elasticsearch用户名(可选)"`
27
+	ESPassword string `yaml:"es_password" json:"es_password" desc:"Elasticsearch密码(可选)"`
26 28
 	//ESAPIKey   string `yaml:"es_api_key" json:"es_api_key"`     // API Key(可选)
27 29
 	//ESBuffer   int    `yaml:"es_buffer" json:"es_buffer"`       // 缓冲大小,默认10000
28 30
 	//ESMaxRetry int    `yaml:"es_max_retry" json:"es_max_retry"` // 最大重试,默认3
29 31
 }
30 32
 
33
+func (c *LogConfig) Description() string {
34
+	return "日志系统配置,支持控制台、文件、Elasticsearch等多种输出方式"
35
+}
36
+
31 37
 // NewLogConfig 创建日志配置实例
32 38
 func NewLogConfig() *LogConfig {
33 39
 	return &LogConfig{}
@@ -74,5 +80,5 @@ func (c *LogConfig) Validate() error {
74 80
 
75 81
 // 自动注册
76 82
 func init() {
77
-	Register("log", &LogConfig{})
83
+	core.Register("log", &LogConfig{})
78 84
 }

+ 14
- 8
config/subconfigs/mcp_config.go Dosyayı Görüntüle

@@ -4,17 +4,23 @@ package subconfigs
4 4
 import (
5 5
 	"fmt"
6 6
 	"log"
7
+
8
+	"git.x2erp.com/qdy/go-base/config/core"
7 9
 )
8 10
 
9 11
 // ServiceConfig 数据库配置
10 12
 type McpServiceConfig struct {
11
-	BaseConfig
12
-	Port         int    `yaml:"port"`
13
-	ServiceName  string `yaml:"service_name"`
14
-	InstanceName string `yaml:"instance_name"`
15
-	ReadTimeout  int    `yaml:"read_timeout"`
16
-	WriteTimeout int    `yaml:"write_timeout"`
17
-	IdleTimeout  int    `yaml:"idle_timeout"`
13
+	core.BaseConfig
14
+	Port         int    `yaml:"port" desc:"服务监听端口"`
15
+	ServiceName  string `yaml:"service_name" desc:"服务名称"`
16
+	InstanceName string `yaml:"instance_name" desc:"服务实例名称"`
17
+	ReadTimeout  int    `yaml:"read_timeout" desc:"读取超时时间(秒)"`
18
+	WriteTimeout int    `yaml:"write_timeout" desc:"写入超时时间(秒)"`
19
+	IdleTimeout  int    `yaml:"idle_timeout" desc:"空闲连接超时时间(秒)"`
20
+}
21
+
22
+func (c *McpServiceConfig) Description() string {
23
+	return "MCP(Model Context Protocol)服务配置"
18 24
 }
19 25
 
20 26
 // SetDefaults 设置默认值 - 实现 ConfigLoader 接口
@@ -65,5 +71,5 @@ func (c *McpServiceConfig) IsConfigured() bool {
65 71
 
66 72
 // 自动注册
67 73
 func init() {
68
-	Register("mcpservice", &McpServiceConfig{})
74
+	core.Register("mcpservice", &McpServiceConfig{})
69 75
 }

+ 32
- 25
config/subconfigs/micro_config.go Dosyayı Görüntüle

@@ -1,34 +1,41 @@
1 1
 package subconfigs
2 2
 
3
-import "fmt"
3
+import (
4
+	"fmt"
5
+
6
+	"git.x2erp.com/qdy/go-base/config/core"
7
+)
4 8
 
5 9
 // MicroConfig Go Micro微服务配置
6 10
 type MicroConfig struct {
7
-	BaseConfig
8
-	RegistryAddress string `yaml:"registry_address"`
9
-	RegistryType    string `yaml:"registry_type"`
10
-	RegistryTimeout int    `yaml:"registry_timeout"`
11
-	LBStrategy      string `yaml:"lb_strategy"`
12
-	LBCacheTTL      int    `yaml:"lb_cache_ttl"`
13
-	LBRetries       int    `yaml:"lb_retries"`
14
-	ClientTimeout   int    `yaml:"client_timeout"`
15
-	ClientPoolSize  int    `yaml:"client_pool_size"`
16
-	MaxRetries      int    `yaml:"max_retries"`
17
-	CircuitEnabled  bool   `yaml:"circuit_enabled"`
18
-	CircuitTimeout  int    `yaml:"circuit_timeout"`
19
-	ErrorThreshold  int    `yaml:"error_threshold"`
20
-	SleepWindow     int    `yaml:"sleep_window"`
21
-	HealthPath      string `yaml:"health_path"`
22
-	HealthInterval  int    `yaml:"health_interval"`
23
-	HealthTimeout   int    `yaml:"health_timeout"`
24
-	LogLevel        string `yaml:"log_level"`
25
-	EnableDebug     bool   `yaml:"enable_debug"`
26
-	MetricsEnabled  bool   `yaml:"metrics_enabled"`
27
-	MetricsAddress  string `yaml:"metrics_address"`
28
-	TracerEnabled   bool   `yaml:"tracer_enabled"`
29
-	TracerAddress   string `yaml:"tracer_address"`
11
+	core.BaseConfig
12
+	RegistryAddress string `yaml:"registry_address" desc:"服务注册中心地址"`
13
+	RegistryType    string `yaml:"registry_type" desc:"注册中心类型:consul, etcd, mdns"`
14
+	RegistryTimeout int    `yaml:"registry_timeout" desc:"注册中心超时时间(秒)"`
15
+	LBStrategy      string `yaml:"lb_strategy" desc:"负载均衡策略:roundrobin, random, leastconn"`
16
+	LBCacheTTL      int    `yaml:"lb_cache_ttl" desc:"负载均衡缓存TTL(秒)"`
17
+	LBRetries       int    `yaml:"lb_retries" desc:"负载均衡重试次数"`
18
+	ClientTimeout   int    `yaml:"client_timeout" desc:"客户端调用超时时间(秒)"`
19
+	ClientPoolSize  int    `yaml:"client_pool_size" desc:"客户端连接池大小"`
20
+	MaxRetries      int    `yaml:"max_retries" desc:"最大重试次数"`
21
+	CircuitEnabled  bool   `yaml:"circuit_enabled" desc:"是否启用熔断器"`
22
+	CircuitTimeout  int    `yaml:"circuit_timeout" desc:"熔断器超时时间(秒)"`
23
+	ErrorThreshold  int    `yaml:"error_threshold" desc:"错误阈值(触发熔断的错误次数)"`
24
+	SleepWindow     int    `yaml:"sleep_window" desc:"熔断恢复时间窗口(秒)"`
25
+	HealthPath      string `yaml:"health_path" desc:"健康检查路径"`
26
+	HealthInterval  int    `yaml:"health_interval" desc:"健康检查间隔(秒)"`
27
+	HealthTimeout   int    `yaml:"health_timeout" desc:"健康检查超时时间(秒)"`
28
+	LogLevel        string `yaml:"log_level" desc:"日志级别:debug, info, warn, error"`
29
+	EnableDebug     bool   `yaml:"enable_debug" desc:"是否启用调试模式"`
30
+	MetricsEnabled  bool   `yaml:"metrics_enabled" desc:"是否启用指标收集"`
31
+	MetricsAddress  string `yaml:"metrics_address" desc:"指标服务地址"`
32
+	TracerEnabled   bool   `yaml:"tracer_enabled" desc:"是否启用链路追踪"`
33
+	TracerAddress   string `yaml:"tracer_address" desc:"链路追踪服务地址"`
30 34
 }
31 35
 
36
+func (c *MicroConfig) Description() string {
37
+	return "Go Micro微服务框架配置,包含服务发现、负载均衡、熔断器等组件"
38
+}
32 39
 func NewMicroConfig() *MicroConfig {
33 40
 	return &MicroConfig{}
34 41
 }
@@ -73,5 +80,5 @@ func (c *MicroConfig) IsConfigured() bool {
73 80
 
74 81
 // 自动注册
75 82
 func init() {
76
-	Register("micro", &MicroConfig{})
83
+	core.Register("micro", &MicroConfig{})
77 84
 }

+ 17
- 11
config/subconfigs/mongodb_config.go Dosyayı Görüntüle

@@ -3,20 +3,26 @@ package subconfigs
3 3
 import (
4 4
 	"fmt"
5 5
 	"time"
6
+
7
+	"git.x2erp.com/qdy/go-base/config/core"
6 8
 )
7 9
 
8 10
 // MongoDBConfig MongoDB客户端配置
9 11
 type MongoDBConfig struct {
10
-	BaseConfig
11
-	URI         string `yaml:"uri"`         // MongoDB连接URI,如:"mongodb://localhost:27017"
12
-	Database    string `yaml:"database"`    // 默认数据库名
13
-	Username    string `yaml:"username"`    // 用户名(可选)
14
-	Password    string `yaml:"password"`    // 密码(可选)
15
-	AuthSource  string `yaml:"authSource"`  // 认证数据库,默认:"admin"
16
-	MaxPoolSize uint64 `yaml:"maxPoolSize"` // 最大连接池大小
17
-	MinPoolSize uint64 `yaml:"minPoolSize"` // 最小连接池大小
18
-	SSL         bool   `yaml:"ssl"`         // 是否使用SSL连接
19
-	Timeout     int    `yaml:"timeout"`     // 连接超时时间,单位:秒
12
+	core.BaseConfig
13
+	URI         string `yaml:"uri" desc:"MongoDB连接URI,如:mongodb://localhost:27017"`
14
+	Database    string `yaml:"database" desc:"默认数据库名称"`
15
+	Username    string `yaml:"username" desc:"用户名(可选)"`
16
+	Password    string `yaml:"password" desc:"密码(可选)"`
17
+	AuthSource  string `yaml:"authSource" desc:"认证数据库,默认:admin"`
18
+	MaxPoolSize uint64 `yaml:"maxPoolSize" desc:"最大连接池大小"`
19
+	MinPoolSize uint64 `yaml:"minPoolSize" desc:"最小连接池大小"`
20
+	SSL         bool   `yaml:"ssl" desc:"是否使用SSL连接"`
21
+	Timeout     int    `yaml:"timeout" desc:"连接超时时间(秒)"`
22
+}
23
+
24
+func (c *MongoDBConfig) Description() string {
25
+	return "MongoDB数据库连接配置"
20 26
 }
21 27
 
22 28
 // NewMongoDBConfig 创建MongoDB配置实例
@@ -68,5 +74,5 @@ func (c *MongoDBConfig) IsConfigured() bool {
68 74
 
69 75
 // 自动注册
70 76
 func init() {
71
-	Register("mongodb", &MongoDBConfig{})
77
+	core.Register("mongodb", &MongoDBConfig{})
72 78
 }

+ 35
- 27
config/subconfigs/rabbitmq_config.go Dosyayı Görüntüle

@@ -1,34 +1,42 @@
1 1
 package subconfigs
2 2
 
3
-import "fmt"
3
+import (
4
+	"fmt"
5
+
6
+	"git.x2erp.com/qdy/go-base/config/core"
7
+)
4 8
 
5 9
 // RabbitMQConfig RabbitMQ配置
6 10
 type RabbitMQConfig struct {
7
-	BaseConfig
8
-	Host                 string `yaml:"host"`
9
-	Port                 int    `yaml:"port"`
10
-	Username             string `yaml:"username"`
11
-	Password             string `yaml:"password"`
12
-	Vhost                string `yaml:"vhost"`
13
-	UseTLS               bool   `yaml:"use_tls"`
14
-	CACert               string `yaml:"ca_cert"`
15
-	CertFile             string `yaml:"cert_file"`
16
-	KeyFile              string `yaml:"key_file"`
17
-	MaxOpenChannels      int    `yaml:"max_open_channels"`
18
-	ReconnectDelay       int    `yaml:"reconnect_delay"`
19
-	MaxReconnectAttempts int    `yaml:"max_reconnect_attempts"`
20
-	Heartbeat            int    `yaml:"heartbeat"`
21
-	ChannelSize          int    `yaml:"channel_size"`
22
-	DefaultExchange      string `yaml:"default_exchange"`
23
-	DefaultQueue         string `yaml:"default_queue"`
24
-	AutoAck              bool   `yaml:"auto_ack"`
25
-	Mandatory            bool   `yaml:"mandatory"`
26
-	Immediate            bool   `yaml:"immediate"`
27
-	PrefetchCount        int    `yaml:"prefetch_count"`
28
-	PrefetchSize         int    `yaml:"prefetch_size"`
29
-	Global               bool   `yaml:"global"`
30
-	PublisherConfirms    bool   `yaml:"publisher_confirms"`
31
-	ConfirmTimeout       int    `yaml:"confirm_timeout"`
11
+	core.BaseConfig
12
+	Host                 string `yaml:"host" desc:"RabbitMQ服务器主机地址"`
13
+	Port                 int    `yaml:"port" desc:"RabbitMQ服务器端口"`
14
+	Username             string `yaml:"username" desc:"RabbitMQ用户名"`
15
+	Password             string `yaml:"password" desc:"RabbitMQ密码"`
16
+	Vhost                string `yaml:"vhost" desc:"RabbitMQ虚拟主机"`
17
+	UseTLS               bool   `yaml:"use_tls" desc:"是否使用TLS加密连接"`
18
+	CACert               string `yaml:"ca_cert" desc:"CA证书文件路径"`
19
+	CertFile             string `yaml:"cert_file" desc:"客户端证书文件路径"`
20
+	KeyFile              string `yaml:"key_file" desc:"客户端私钥文件路径"`
21
+	MaxOpenChannels      int    `yaml:"max_open_channels" desc:"最大打开的通道数"`
22
+	ReconnectDelay       int    `yaml:"reconnect_delay" desc:"重连延迟时间(毫秒)"`
23
+	MaxReconnectAttempts int    `yaml:"max_reconnect_attempts" desc:"最大重连尝试次数"`
24
+	Heartbeat            int    `yaml:"heartbeat" desc:"心跳间隔时间(秒)"`
25
+	ChannelSize          int    `yaml:"channel_size" desc:"通道缓冲区大小"`
26
+	DefaultExchange      string `yaml:"default_exchange" desc:"默认交换机名称"`
27
+	DefaultQueue         string `yaml:"default_queue" desc:"默认队列名称"`
28
+	AutoAck              bool   `yaml:"auto_ack" desc:"是否自动确认消息"`
29
+	Mandatory            bool   `yaml:"mandatory" desc:"是否强制路由"`
30
+	Immediate            bool   `yaml:"immediate" desc:"是否立即交付"`
31
+	PrefetchCount        int    `yaml:"prefetch_count" desc:"预取消息数量"`
32
+	PrefetchSize         int    `yaml:"prefetch_size" desc:"预取消息大小"`
33
+	Global               bool   `yaml:"global" desc:"是否为全局预取设置"`
34
+	PublisherConfirms    bool   `yaml:"publisher_confirms" desc:"是否启用发布者确认"`
35
+	ConfirmTimeout       int    `yaml:"confirm_timeout" desc:"确认超时时间(秒)"`
36
+}
37
+
38
+func (c *RabbitMQConfig) Description() string {
39
+	return "RabbitMQ消息队列配置"
32 40
 }
33 41
 
34 42
 func NewRabbitMQConfig() *RabbitMQConfig {
@@ -70,5 +78,5 @@ func (c *RabbitMQConfig) IsConfigured() bool {
70 78
 
71 79
 // 自动注册
72 80
 func init() {
73
-	Register("rabbitmq", &RabbitMQConfig{})
81
+	core.Register("rabbitmq", &RabbitMQConfig{})
74 82
 }

+ 21
- 13
config/subconfigs/redis_config.go Dosyayı Görüntüle

@@ -1,20 +1,28 @@
1 1
 package subconfigs
2 2
 
3
-import "fmt"
3
+import (
4
+	"fmt"
5
+
6
+	"git.x2erp.com/qdy/go-base/config/core"
7
+)
4 8
 
5 9
 // RedisConfig Redis配置
6 10
 type RedisConfig struct {
7
-	BaseConfig
8
-	Host         string `yaml:"host"`
9
-	Port         int    `yaml:"port"`
10
-	Password     string `yaml:"password"`
11
-	DB           int    `yaml:"db"`
12
-	PoolSize     int    `yaml:"pool_size"`
13
-	DialTimeout  int    `yaml:"dial_timeout"`
14
-	ReadTimeout  int    `yaml:"read_timeout"`
15
-	WriteTimeout int    `yaml:"write_timeout"`
16
-	IdleTimeout  int    `yaml:"idle_timeout"`
17
-	MaxConnAge   int    `yaml:"max_conn_age"`
11
+	core.BaseConfig
12
+	Host         string `yaml:"host" desc:"Redis服务器主机地址"`
13
+	Port         int    `yaml:"port" desc:"Redis服务器端口"`
14
+	Password     string `yaml:"password" desc:"Redis密码"`
15
+	DB           int    `yaml:"db" desc:"Redis数据库索引(0-15)"`
16
+	PoolSize     int    `yaml:"pool_size" desc:"连接池大小"`
17
+	DialTimeout  int    `yaml:"dial_timeout" desc:"连接超时时间(秒)"`
18
+	ReadTimeout  int    `yaml:"read_timeout" desc:"读取超时时间(秒)"`
19
+	WriteTimeout int    `yaml:"write_timeout" desc:"写入超时时间(秒)"`
20
+	IdleTimeout  int    `yaml:"idle_timeout" desc:"空闲连接超时时间(秒)"`
21
+	MaxConnAge   int    `yaml:"max_conn_age" desc:"连接最大生命周期(秒)"`
22
+}
23
+
24
+func (c *RedisConfig) Description() string {
25
+	return "Redis缓存数据库配置"
18 26
 }
19 27
 
20 28
 func NewRedisConfig() *RedisConfig {
@@ -48,5 +56,5 @@ func (c *RedisConfig) IsConfigured() bool {
48 56
 
49 57
 // 自动注册
50 58
 func init() {
51
-	Register("redis", &RedisConfig{})
59
+	core.Register("redis", &RedisConfig{})
52 60
 }

+ 20
- 14
config/subconfigs/service_config.go Dosyayı Görüntüle

@@ -4,22 +4,28 @@ package subconfigs
4 4
 import (
5 5
 	"fmt"
6 6
 	"log"
7
+
8
+	"git.x2erp.com/qdy/go-base/config/core"
7 9
 )
8 10
 
9
-// ServiceConfig 数据库配置
11
+// ServiceConfig 服务配置
10 12
 type ServiceConfig struct {
11
-	BaseConfig
12
-	AppName      string `yaml:"app_name"`       //当前应用名称
13
-	AppVersion   string `yaml:"app_version"`    //当前应用版本
14
-	AppEnv       string `yaml:"app_env"`        //环境参数 dev test prod
15
-	AppAuthToken string `yaml:"app_auth_token"` //使用静态认证的时候使用的token
16
-
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"`
13
+	core.BaseConfig
14
+	AppName      string `yaml:"app_name" desc:"当前应用名称"`
15
+	AppVersion   string `yaml:"app_version" desc:"当前应用版本"`
16
+	AppEnv       string `yaml:"app_env" desc:"环境参数:dev, test, prod"`
17
+	AppAuthToken string `yaml:"app_auth_token" desc:"静态认证使用的token"`
18
+
19
+	Port         int    `yaml:"port" desc:"服务监听端口"`
20
+	ServiceName  string `yaml:"service_name" desc:"服务名称"`
21
+	InstanceName string `yaml:"instance_name" desc:"服务实例名称"`
22
+	ReadTimeout  int    `yaml:"read_timeout" desc:"读取超时时间(秒)"`
23
+	WriteTimeout int    `yaml:"write_timeout" desc:"写入超时时间(秒)"`
24
+	IdleTimeout  int    `yaml:"idle_timeout" desc:"空闲连接超时时间(秒)"`
25
+}
26
+
27
+func (c *ServiceConfig) Description() string {
28
+	return "基础服务配置,包含应用信息、端口和超时设置"
23 29
 }
24 30
 
25 31
 // SetDefaults 设置默认值 - 实现 ConfigLoader 接口
@@ -74,5 +80,5 @@ func (c *ServiceConfig) IsConfigured() bool {
74 80
 
75 81
 // 自动注册
76 82
 func init() {
77
-	Register("service", &ServiceConfig{})
83
+	core.Register("service", &ServiceConfig{})
78 84
 }

+ 25
- 0
config/test_metadata_test.go Dosyayı Görüntüle

@@ -0,0 +1,25 @@
1
+package config
2
+
3
+import (
4
+	"fmt"
5
+	"testing"
6
+
7
+	"git.x2erp.com/qdy/go-base/config/core"
8
+	_ "git.x2erp.com/qdy/go-base/config/subconfigs"
9
+)
10
+
11
+func TestGetAllConfigsMeta(t *testing.T) {
12
+	fmt.Println("=== 调试开始 ===")
13
+
14
+	allMeta := core.GetAllConfigsMeta()
15
+	fmt.Printf("配置数量(直接): %d\n", len(allMeta))
16
+
17
+	for configName, configMeta := range allMeta {
18
+		fmt.Printf("配置: %s.  %s\n", configName, configMeta.Desc)
19
+
20
+		for fieldName, fieldMeta := range configMeta.Fields {
21
+			fmt.Printf("  字段: %s, 类型: %s, YAML标签: %s, 描述: %s\n",
22
+				fieldName, fieldMeta.Type, fieldMeta.YamlTag, fieldMeta.Desc)
23
+		}
24
+	}
25
+}

Loading…
İptal
Kaydet