配置文件分了层级, ToElastic中每个请求独立完成且拥有自己的年属性

This commit is contained in:
zhangkun9038@dingtalk.com 2025-03-10 22:45:40 +08:00
parent d554c4b549
commit 16ea92317c
3 changed files with 23 additions and 11 deletions

View File

@ -6,10 +6,16 @@ import (
)
type Config struct {
FluentdURL string `json:"fluentdURL"`
ElasticsearchURL string `json:"ElasticsearchURL"`
ElasticsearchUser string `json:"ElasticsearchUser"`
ElasticsearchPassword string `json:"ElasticsearchPassword"`
Fluentd struct {
URL string `json:"url"`
} `json:"fluentd"`
Elasticsearch struct {
URL string `json:"url"`
Auth struct {
Username string `json:"username"`
Password string `json:"password"`
} `json:"auth"`
} `json:"elasticsearch"`
}
// LoadConfig 从指定路径加载配置文件

View File

@ -1,8 +1,14 @@
{
"fluentdURL": "http://fluentd.k8s.xunlang.home",
"ElasticsearchURL": "http://elastic.k8s.xunlang.home",
"ElasticsearchUser": "fluentd_user",
"ElasticsearchPassword": "fluentd_password"
"fluentd": {
"url": "http://fluentd.k8s.xunlang.home"
},
"elasticsearch": {
"url": "http://elastic.k8s.xunlang.home",
"auth": {
"username": "fluentd_user",
"password": "fluentd_password"
}
}
}

View File

@ -239,7 +239,7 @@ func (cl *CandleList) ToFluentd() error {
}
// 构造完整URL添加json参数
fullURL := fmt.Sprintf("%s/%s?json", strings.TrimRight(config.FluentdURL, "/"), tag)
fullURL := fmt.Sprintf("%s/%s?json", strings.TrimRight(config.Fluentd.URL, "/"), tag)
// 输出完整请求URL和请求体到日志
fmt.Printf("Sending request to URL: %s\n", fullURL)
@ -421,7 +421,7 @@ func (cl *CandleList) ToElastic() error {
}
// 构造完整URL
fullURL := fmt.Sprintf("%s/%s/_doc/%d", strings.TrimRight(config.ElasticsearchURL, "/"), index, ts)
fullURL := fmt.Sprintf("%s/%s/_doc/%d", strings.TrimRight(config.Elasticsearch.URL, "/"), index, ts)
fmt.Println("fullURL: ", fullURL)
// 创建请求
@ -436,7 +436,7 @@ func (cl *CandleList) ToElastic() error {
// 尝试从不同层级加载配置
// 设置基本认证
req.SetBasicAuth(config.ElasticsearchUser, config.ElasticsearchPassword)
req.SetBasicAuth(config.Elasticsearch.Auth.Username, config.Elasticsearch.Auth.Password)
// 发送HTTP请求
resp, err := client.Do(req)