配置文件分了层级, 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 { type Config struct {
FluentdURL string `json:"fluentdURL"` Fluentd struct {
ElasticsearchURL string `json:"ElasticsearchURL"` URL string `json:"url"`
ElasticsearchUser string `json:"ElasticsearchUser"` } `json:"fluentd"`
ElasticsearchPassword string `json:"ElasticsearchPassword"` Elasticsearch struct {
URL string `json:"url"`
Auth struct {
Username string `json:"username"`
Password string `json:"password"`
} `json:"auth"`
} `json:"elasticsearch"`
} }
// LoadConfig 从指定路径加载配置文件 // LoadConfig 从指定路径加载配置文件

View File

@ -1,8 +1,14 @@
{ {
"fluentdURL": "http://fluentd.k8s.xunlang.home", "fluentd": {
"ElasticsearchURL": "http://elastic.k8s.xunlang.home", "url": "http://fluentd.k8s.xunlang.home"
"ElasticsearchUser": "fluentd_user", },
"ElasticsearchPassword": "fluentd_password" "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参数 // 构造完整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和请求体到日志 // 输出完整请求URL和请求体到日志
fmt.Printf("Sending request to URL: %s\n", fullURL) fmt.Printf("Sending request to URL: %s\n", fullURL)
@ -421,7 +421,7 @@ func (cl *CandleList) ToElastic() error {
} }
// 构造完整URL // 构造完整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) 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请求 // 发送HTTP请求
resp, err := client.Do(req) resp, err := client.Do(req)