From 16ea92317c45af0f265871a3fe73d1d338db97ca Mon Sep 17 00:00:00 2001 From: "zhangkun9038@dingtalk.com" Date: Mon, 10 Mar 2025 22:45:40 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E5=88=86?= =?UTF-8?q?=E4=BA=86=E5=B1=82=E7=BA=A7,=20ToElastic=E4=B8=AD=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=E8=AF=B7=E6=B1=82=E7=8B=AC=E7=AB=8B=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E4=B8=94=E6=8B=A5=E6=9C=89=E8=87=AA=E5=B7=B1=E7=9A=84=E5=B9=B4?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 14 ++++++++++---- config/config.json | 14 ++++++++++---- okx/candleList.go | 6 +++--- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index 110de88..1dcbc91 100644 --- a/config/config.go +++ b/config/config.go @@ -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 从指定路径加载配置文件 diff --git a/config/config.json b/config/config.json index ab5f889..c16da24 100644 --- a/config/config.json +++ b/config/config.json @@ -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" + } + } } diff --git a/okx/candleList.go b/okx/candleList.go index 48f9dec..30e1de4 100644 --- a/okx/candleList.go +++ b/okx/candleList.go @@ -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)