qdy 3 месяцев назад
Родитель
Сommit
a5dc91d0ed
2 измененных файлов: 133 добавлений и 57 удалений
  1. 119
    51
      config/config.go
  2. 14
    6
      types/types.go

+ 119
- 51
config/config.go Просмотреть файл

11
 
11
 
12
 // Config 应用配置
12
 // Config 应用配置
13
 type Config struct {
13
 type Config struct {
14
-	Database DBConfig    `yaml:"database"`
15
-	Redis    RedisConfig `yaml:"redis"`
16
-	Doris    DorisConfig `yaml:"doris"`
17
-	Auth     Auth        `yaml:"auth"`
18
-	Service  Service     `yaml:"service"`
19
-	HTTP     HTTPConfig  `yaml:"http"` // 新增 HTTP 配置
14
+	Database DBConfig       `yaml:"database"`
15
+	Redis    RedisConfig    `yaml:"redis"`
16
+	Doris    DorisConfig    `yaml:"doris"`
17
+	RabbitMQ RabbitMQConfig `yaml:"rabbitmq"` // 新增 RabbitMQ 配置
18
+	Auth     Auth           `yaml:"auth"`
19
+	Service  Service        `yaml:"service"`
20
+	HTTP     HTTPConfig     `yaml:"http"`
21
+}
22
+
23
+// RabbitMQConfig RabbitMQ配置
24
+type RabbitMQConfig struct {
25
+	// 连接配置
26
+	Host     string `yaml:"host"`
27
+	Port     int    `yaml:"port"`
28
+	Username string `yaml:"username"`
29
+	Password string `yaml:"password"`
30
+	Vhost    string `yaml:"vhost"`
31
+
32
+	// TLS配置
33
+	UseTLS   bool   `yaml:"use_tls"`
34
+	CACert   string `yaml:"ca_cert"`
35
+	CertFile string `yaml:"cert_file"`
36
+	KeyFile  string `yaml:"key_file"`
37
+
38
+	// 连接池和超时配置
39
+	MaxOpenChannels      int `yaml:"max_open_channels"`      // 最大通道数
40
+	ReconnectDelay       int `yaml:"reconnect_delay"`        // 重连延迟(毫秒)
41
+	MaxReconnectAttempts int `yaml:"max_reconnect_attempts"` // 最大重试次数
42
+
43
+	// 心跳和超时
44
+	Heartbeat   int `yaml:"heartbeat"`    // 心跳间隔(秒)
45
+	ChannelSize int `yaml:"channel_size"` // 通道缓冲区大小
46
+
47
+	// 队列和交换机默认配置
48
+	DefaultExchange string `yaml:"default_exchange"`
49
+	DefaultQueue    string `yaml:"default_queue"`
50
+
51
+	// 消息确认模式
52
+	AutoAck   bool `yaml:"auto_ack"`
53
+	Mandatory bool `yaml:"mandatory"`
54
+	Immediate bool `yaml:"immediate"`
55
+
56
+	// QoS配置
57
+	PrefetchCount int  `yaml:"prefetch_count"` // 预取数量
58
+	PrefetchSize  int  `yaml:"prefetch_size"`  // 预取大小(字节)
59
+	Global        bool `yaml:"global"`         // 是否是全局QoS
60
+
61
+	// 发布确认
62
+	PublisherConfirms bool `yaml:"publisher_confirms"` // 是否启用发布者确认
63
+	ConfirmTimeout    int  `yaml:"confirm_timeout"`    // 确认超时(秒)
20
 }
64
 }
21
 
65
 
22
 // HTTPConfig HTTP客户端配置
66
 // HTTPConfig HTTP客户端配置
23
 type HTTPConfig struct {
67
 type HTTPConfig struct {
24
-	Timeout             int  `yaml:"timeout"`                 // 请求超时时间(秒)
25
-	MaxIdleConns        int  `yaml:"max_idle_conns"`          // 最大空闲连接数
26
-	MaxIdleConnsPerHost int  `yaml:"max_idle_conns_per_host"` // 每个主机的最大空闲连接数
27
-	IdleConnTimeout     int  `yaml:"idle_conn_timeout"`       // 空闲连接超时时间(秒)
28
-	MaxConnsPerHost     int  `yaml:"max_conns_per_host"`      // 每个主机的最大连接数
29
-	DisableCompression  bool `yaml:"disable_compression"`     // 是否禁用压缩
30
-	DisableKeepAlives   bool `yaml:"disable_keep_alives"`     // 是否禁用长连接
68
+	Timeout             int  `yaml:"timeout"`
69
+	MaxIdleConns        int  `yaml:"max_idle_conns"`
70
+	MaxIdleConnsPerHost int  `yaml:"max_idle_conns_per_host"`
71
+	IdleConnTimeout     int  `yaml:"idle_conn_timeout"`
72
+	MaxConnsPerHost     int  `yaml:"max_conns_per_host"`
73
+	DisableCompression  bool `yaml:"disable_compression"`
74
+	DisableKeepAlives   bool `yaml:"disable_keep_alives"`
31
 }
75
 }
32
 
76
 
33
 // DBConfig 数据库配置
77
 // DBConfig 数据库配置
40
 	Database        string `yaml:"database"`
84
 	Database        string `yaml:"database"`
41
 	MaxOpenConns    int    `yaml:"max_open_conns"`
85
 	MaxOpenConns    int    `yaml:"max_open_conns"`
42
 	MaxIdleConns    int    `yaml:"max_idle_conns"`
86
 	MaxIdleConns    int    `yaml:"max_idle_conns"`
43
-	ConnMaxLifetime int    `yaml:"conn_max_lifetime"` // 单位:秒
87
+	ConnMaxLifetime int    `yaml:"conn_max_lifetime"`
44
 }
88
 }
45
 
89
 
46
 // RedisConfig Redis配置
90
 // RedisConfig Redis配置
47
 type RedisConfig struct {
91
 type RedisConfig struct {
48
-	Host     string `yaml:"host"`
49
-	Port     int    `yaml:"port"`
50
-	Password string `yaml:"password"`
51
-	DB       int    `yaml:"db"`
52
-	PoolSize int    `yaml:"pool_size"`
53
-	// 连接超时和读写超时(秒)
54
-	DialTimeout  int `yaml:"dial_timeout"`
55
-	ReadTimeout  int `yaml:"read_timeout"`
56
-	WriteTimeout int `yaml:"write_timeout"`
57
-	// 连接池配置
58
-	IdleTimeout int `yaml:"idle_timeout"` // 空闲连接超时(秒)
59
-	MaxConnAge  int `yaml:"max_conn_age"` // 连接最大存活时间(秒)
92
+	Host         string `yaml:"host"`
93
+	Port         int    `yaml:"port"`
94
+	Password     string `yaml:"password"`
95
+	DB           int    `yaml:"db"`
96
+	PoolSize     int    `yaml:"pool_size"`
97
+	DialTimeout  int    `yaml:"dial_timeout"`
98
+	ReadTimeout  int    `yaml:"read_timeout"`
99
+	WriteTimeout int    `yaml:"write_timeout"`
100
+	IdleTimeout  int    `yaml:"idle_timeout"`
101
+	MaxConnAge   int    `yaml:"max_conn_age"`
60
 }
102
 }
61
 
103
 
62
 // DorisConfig Doris配置
104
 // DorisConfig Doris配置
63
 type DorisConfig struct {
105
 type DorisConfig struct {
64
-	// FE节点配置
65
-	FEHost     string `yaml:"fe_host"`
66
-	FEPort     int    `yaml:"fe_port"`
67
-	FEUsername string `yaml:"fe_username"`
68
-	FEPassword string `yaml:"fe_password"`
69
-
70
-	// MySQL协议配置
71
-	MySQLHost string `yaml:"mysql_host"`
72
-	MySQLPort int    `yaml:"mysql_port"`
73
-
74
-	// 连接池配置
75
-	MaxOpenConns    int `yaml:"max_open_conns"`
76
-	MaxIdleConns    int `yaml:"max_idle_conns"`
77
-	ConnMaxLifetime int `yaml:"conn_max_lifetime"` // 单位:秒
78
-
79
-	// Stream Load配置
80
-	StreamLoadTimeout int `yaml:"stream_load_timeout"` // 秒
81
-	BatchSize         int `yaml:"batch_size"`          // 批量大小
106
+	FEHost            string `yaml:"fe_host"`
107
+	FEPort            int    `yaml:"fe_port"`
108
+	FEUsername        string `yaml:"fe_username"`
109
+	FEPassword        string `yaml:"fe_password"`
110
+	MySQLHost         string `yaml:"mysql_host"`
111
+	MySQLPort         int    `yaml:"mysql_port"`
112
+	MaxOpenConns      int    `yaml:"max_open_conns"`
113
+	MaxIdleConns      int    `yaml:"max_idle_conns"`
114
+	ConnMaxLifetime   int    `yaml:"conn_max_lifetime"`
115
+	StreamLoadTimeout int    `yaml:"stream_load_timeout"`
116
+	BatchSize         int    `yaml:"batch_size"`
82
 }
117
 }
83
 
118
 
84
 // Auth 认证配置
119
 // Auth 认证配置
101
 	GetDatabase() DBConfig
136
 	GetDatabase() DBConfig
102
 	GetRedis() RedisConfig
137
 	GetRedis() RedisConfig
103
 	GetDoris() DorisConfig
138
 	GetDoris() DorisConfig
139
+	GetRabbitMQ() RabbitMQConfig // 新增获取RabbitMQ配置方法
104
 	GetAuth() Auth
140
 	GetAuth() Auth
105
 	GetService() Service
141
 	GetService() Service
106
-	GetHTTP() HTTPConfig // 新增获取HTTP配置方法
142
+	GetHTTP() HTTPConfig
107
 	IsDatabaseConfigured() bool
143
 	IsDatabaseConfigured() bool
108
 	IsRedisConfigured() bool
144
 	IsRedisConfigured() bool
109
 	IsDorisConfigured() bool
145
 	IsDorisConfigured() bool
110
-	IsHTTPConfigured() bool // 新增检查HTTP配置方法
146
+	IsRabbitMQConfigured() bool // 新增检查RabbitMQ配置方法
147
+	IsHTTPConfigured() bool
148
+	IsAuthConfigured() bool
111
 }
149
 }
112
 
150
 
113
 // 实现接口的具体类型
151
 // 实现接口的具体类型
127
 	return cw.config.Doris
165
 	return cw.config.Doris
128
 }
166
 }
129
 
167
 
168
+func (cw *configWrapper) GetRabbitMQ() RabbitMQConfig {
169
+	return cw.config.RabbitMQ
170
+}
171
+
130
 func (cw *configWrapper) GetAuth() Auth {
172
 func (cw *configWrapper) GetAuth() Auth {
131
 	return cw.config.Auth
173
 	return cw.config.Auth
132
 }
174
 }
155
 
197
 
156
 func (cw *configWrapper) IsDorisConfigured() bool {
198
 func (cw *configWrapper) IsDorisConfigured() bool {
157
 	doris := cw.config.Doris
199
 	doris := cw.config.Doris
158
-	// 检查FE配置(HTTP API方式)
159
 	return doris.FEHost != "" && doris.FEPort > 0
200
 	return doris.FEHost != "" && doris.FEPort > 0
160
 }
201
 }
161
 
202
 
203
+func (cw *configWrapper) IsRabbitMQConfigured() bool {
204
+	rabbit := cw.config.RabbitMQ
205
+	// 基础连接配置检查
206
+	return rabbit.Host != "" && rabbit.Port > 0
207
+}
208
+
162
 func (cw *configWrapper) IsHTTPConfigured() bool {
209
 func (cw *configWrapper) IsHTTPConfigured() bool {
163
 	httpCfg := cw.config.HTTP
210
 	httpCfg := cw.config.HTTP
164
 	return httpCfg.Timeout > 0
211
 	return httpCfg.Timeout > 0
180
 		config, err := loadConfig()
227
 		config, err := loadConfig()
181
 		if err != nil {
228
 		if err != nil {
182
 			initErr = err
229
 			initErr = err
183
-			// 创建一个空的配置实例,避免nil指针
184
 			instance = &configWrapper{config: &Config{}}
230
 			instance = &configWrapper{config: &Config{}}
185
 			return
231
 			return
186
 		}
232
 		}
196
 
242
 
197
 // loadConfig 加载配置文件
243
 // loadConfig 加载配置文件
198
 func loadConfig() (*Config, error) {
244
 func loadConfig() (*Config, error) {
199
-
200
 	configFile, err := findConfigFile()
245
 	configFile, err := findConfigFile()
201
 	if err != nil {
246
 	if err != nil {
202
 		return nil, err
247
 		return nil, err
222
 			ReadTimeout:  3,
267
 			ReadTimeout:  3,
223
 			WriteTimeout: 3,
268
 			WriteTimeout: 3,
224
 			IdleTimeout:  300,
269
 			IdleTimeout:  300,
225
-			MaxConnAge:   0, // 0表示不限制
270
+			MaxConnAge:   0,
226
 		},
271
 		},
227
 		Doris: DorisConfig{
272
 		Doris: DorisConfig{
228
 			FEPort:            8030,
273
 			FEPort:            8030,
238
 			MaxIdleConns:        100,
283
 			MaxIdleConns:        100,
239
 			MaxIdleConnsPerHost: 10,
284
 			MaxIdleConnsPerHost: 10,
240
 			IdleConnTimeout:     90,
285
 			IdleConnTimeout:     90,
241
-			MaxConnsPerHost:     0, // 0表示不限制
286
+			MaxConnsPerHost:     0,
242
 			DisableCompression:  false,
287
 			DisableCompression:  false,
243
 			DisableKeepAlives:   false,
288
 			DisableKeepAlives:   false,
244
 		},
289
 		},
290
+		RabbitMQ: RabbitMQConfig{
291
+			Port:                 5672,
292
+			Username:             "guest",
293
+			Password:             "guest",
294
+			Vhost:                "/",
295
+			UseTLS:               false,
296
+			MaxOpenChannels:      10,
297
+			ReconnectDelay:       5000, // 5秒
298
+			MaxReconnectAttempts: 10,   // 最多重试10次
299
+			Heartbeat:            30,   // 30秒心跳
300
+			ChannelSize:          100,
301
+			DefaultExchange:      "amq.direct",
302
+			DefaultQueue:         "",
303
+			AutoAck:              false, // 默认手动确认
304
+			Mandatory:            false,
305
+			Immediate:            false,
306
+			PrefetchCount:        1, // 一次预取1条消息
307
+			PrefetchSize:         0,
308
+			Global:               false,
309
+			PublisherConfirms:    false, // 默认不启用发布确认
310
+			ConfirmTimeout:       30,    // 5秒确认超时
311
+		},
245
 	}
312
 	}
313
+
246
 	// 读取配置文件
314
 	// 读取配置文件
247
 	data, err := os.ReadFile(configFile)
315
 	data, err := os.ReadFile(configFile)
248
 	if err != nil {
316
 	if err != nil {

+ 14
- 6
types/types.go Просмотреть файл

9
 	SQL              string                 `json:"sql" binding:"required"`
9
 	SQL              string                 `json:"sql" binding:"required"`
10
 	Params           map[string]interface{} `json:"params,omitempty"`           //名称参数
10
 	Params           map[string]interface{} `json:"params,omitempty"`           //名称参数
11
 	PositionalParams []interface{}          `json:"positionalParams,omitempty"` // 位置参数
11
 	PositionalParams []interface{}          `json:"positionalParams,omitempty"` // 位置参数
12
-	WriterHeader     bool                   //如果查询返回cvs格式时候,包含表头还是不包含。true包含。第一行是表头
12
+	WriterHeader     bool                   `json:"writerHeader,omitempty"`     //如果查询返回cvs格式时候,包含表头还是不包含。true包含。第一行是表头
13
+	AgentUrl         string                 `json:"agentUrl,omitempty"`         //远程服务器地址
14
+	AgentToken       string                 `json:"agentToken,omitempty"`       //远程代理服务器token
15
+	DorisDatabase    string                 `json:"dorisDatabase,omitempty"`    //doris数据库名称
16
+	DorisTable       string                 `json:"dorisTable,omitempty"`       //doris数据库里的表名称
17
+
13
 }
18
 }
14
 
19
 
15
 // QueryResult 查询结果
20
 // QueryResult 查询结果
16
 type QueryResult struct {
21
 type QueryResult struct {
17
-	Success bool        `json:"success"`
18
-	Data    interface{} `json:"data,omitempty"`
19
-	Error   string      `json:"error,omitempty"`
20
-	Count   int         `json:"count,omitempty"`
21
-	Time    string      `json:"time,omitempty"`
22
+	Success    bool          `json:"success"`
23
+	Data       interface{}   `json:"data,omitempty"`
24
+	Error      string        `json:"error,omitempty"`
25
+	Count      int           `json:"count,omitempty"`
26
+	Time       string        `json:"time,omitempty"`
27
+	QueryTime  time.Duration `json:"queryTime,omitempty""`
28
+	SaveTime   time.Duration `json:"saveTime,omitempty""`
29
+	TotalCount int           `json:"totalCount,omitempty""`
22
 }
30
 }
23
 
31
 
24
 // PageResult 分页结果
32
 // PageResult 分页结果

Загрузка…
Отмена
Сохранить