diff --git a/config/configStruct.go b/config/configStruct.go index 6badb82..fcbd11d 100644 --- a/config/configStruct.go +++ b/config/configStruct.go @@ -41,9 +41,11 @@ type UdpConfig struct { } type HttpConfig struct { - Url string `json:"url"` - Method string `json:"method"` //post,put,get,delete - Token Token `json:"token,omitempty"` + Url string `json:"url" yaml:"url"` + Method string `json:"method" yaml:"method"` //post,put,get,delete + Username string `json:"userName" yaml:"userName"` + Password string `json:"password" yaml:"password"` + Token Token `json:"token,omitempty" yaml:"token"` } type ApiServerConfig struct { diff --git a/consumers/consumerHBJCAS.go b/consumers/consumerHBJCAS.go index 563705e..5f1ecbe 100644 --- a/consumers/consumerHBJCAS.go +++ b/consumers/consumerHBJCAS.go @@ -9,9 +9,10 @@ import ( "goInOut/dbOperate" "goInOut/monitors" "goInOut/utils" - "gopkg.in/yaml.v3" "log" "time" + + "gopkg.in/yaml.v3" ) type consumerHBJCAS struct { @@ -51,6 +52,7 @@ func (the *consumerHBJCAS) InputInitial() error { the.ch = make(chan []adaptors.NeedPush, 200) //数据入口 the.InHttp = &dbOperate.HttpHelper{Url: the.Info.IoConfig.In.Http.Url, Token: ""} + the.InHttp.SetAuth(the.Info.IoConfig.In.Http.Username, the.Info.IoConfig.In.Http.Password) the.monitor = &monitors.CommonMonitor{ MonitorHelper: &monitors.MonitorHelper{}, } @@ -139,7 +141,8 @@ func (the *consumerHBJCAS) getEs1HourAggData() { for _, factorId := range hourFactorIds { esQuery := the.getESQueryStrByHour(structId, factorId, start, end) auth := map[string]string{"Authorization": "Bear 85a441d4-022b-4613-abba-aaa8e2693bf7"} - esAggResultStr := the.InHttp.HttpGetWithHeader(esQuery, auth) + //esAggResultStr := the.InHttp.HttpGetWithHeader(esQuery, auth) + esAggResultStr := the.InHttp.HttpPostWithAuth(esQuery, auth) adaptor := the.getAdaptor() adaptor.PointInfo = the.Info.PointInfo @@ -167,7 +170,8 @@ func (the *consumerHBJCAS) getEs10minAggData() { for _, factorId := range factorIds { esQuery := the.getESQueryStrBy10min(structId, factorId, start, end) auth := map[string]string{"Authorization": "Bear 85a441d4-022b-4613-abba-aaa8e2693bf7"} - esAggResultStr := the.InHttp.HttpGetWithHeader(esQuery, auth) + //esAggResultStr := the.InHttp.HttpGetWithHeader(esQuery, auth) + esAggResultStr := the.InHttp.HttpPostWithAuth(esQuery, auth) adaptor := the.getAdaptor() adaptor.PointInfo = the.Info.PointInfo diff --git a/dbOperate/httpHelper.go b/dbOperate/httpHelper.go index c84c6a5..3c3a886 100644 --- a/dbOperate/httpHelper.go +++ b/dbOperate/httpHelper.go @@ -244,6 +244,41 @@ func HttpPostWithHeader(url string, queryBody string, headers map[string]string) return string(body), err } +func (the *HttpHelper) HttpPostWithAuth(queryBody string, headerMap map[string]string) string { + url := the.Url + client := the.client + req, err := http.NewRequest("GET", url, strings.NewReader(queryBody)) + req.Header.Set("Content-Type", "application/json") + if the.userName != "" { + req.SetBasicAuth(the.userName, the.passWord) + } else { + for k, v := range headerMap { + req.Header.Set(k, v) + } + } + + //取消证书校验,避免证书过期问题 + client.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} + + resp, err := client.Do(req) + if err != nil { + log.Printf("查询esproxy 异常=>%s", err.Error()) + return "" + } + + defer func(resp *http.Response) { + if resp != nil && resp.Body != nil { + errClose := resp.Body.Close() + if errClose != nil { + log.Printf("关闭异常") + } + } + }(resp) + log.Println("http get 请求,url", url, " <- code=", resp.StatusCode) + body, err := io.ReadAll(resp.Body) + return string(body) +} + // 静态方法 func HttpPostFormDataWithHeader(url string, queryBody string, headers map[string]string) (string, error) { tr := &http.Transport{