Browse Source

河北秦皇岛es查询修改

dev
zhangyuxiang 4 days ago
parent
commit
29527b4d6b
  1. 8
      config/configStruct.go
  2. 10
      consumers/consumerHBJCAS.go
  3. 35
      dbOperate/httpHelper.go

8
config/configStruct.go

@ -41,9 +41,11 @@ type UdpConfig struct {
} }
type HttpConfig struct { type HttpConfig struct {
Url string `json:"url"` Url string `json:"url" yaml:"url"`
Method string `json:"method"` //post,put,get,delete Method string `json:"method" yaml:"method"` //post,put,get,delete
Token Token `json:"token,omitempty"` Username string `json:"userName" yaml:"userName"`
Password string `json:"password" yaml:"password"`
Token Token `json:"token,omitempty" yaml:"token"`
} }
type ApiServerConfig struct { type ApiServerConfig struct {

10
consumers/consumerHBJCAS.go

@ -9,9 +9,10 @@ import (
"goInOut/dbOperate" "goInOut/dbOperate"
"goInOut/monitors" "goInOut/monitors"
"goInOut/utils" "goInOut/utils"
"gopkg.in/yaml.v3"
"log" "log"
"time" "time"
"gopkg.in/yaml.v3"
) )
type consumerHBJCAS struct { type consumerHBJCAS struct {
@ -51,6 +52,7 @@ func (the *consumerHBJCAS) InputInitial() error {
the.ch = make(chan []adaptors.NeedPush, 200) the.ch = make(chan []adaptors.NeedPush, 200)
//数据入口 //数据入口
the.InHttp = &dbOperate.HttpHelper{Url: the.Info.IoConfig.In.Http.Url, Token: ""} 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{ the.monitor = &monitors.CommonMonitor{
MonitorHelper: &monitors.MonitorHelper{}, MonitorHelper: &monitors.MonitorHelper{},
} }
@ -139,7 +141,8 @@ func (the *consumerHBJCAS) getEs1HourAggData() {
for _, factorId := range hourFactorIds { for _, factorId := range hourFactorIds {
esQuery := the.getESQueryStrByHour(structId, factorId, start, end) esQuery := the.getESQueryStrByHour(structId, factorId, start, end)
auth := map[string]string{"Authorization": "Bear 85a441d4-022b-4613-abba-aaa8e2693bf7"} 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 := the.getAdaptor()
adaptor.PointInfo = the.Info.PointInfo adaptor.PointInfo = the.Info.PointInfo
@ -167,7 +170,8 @@ func (the *consumerHBJCAS) getEs10minAggData() {
for _, factorId := range factorIds { for _, factorId := range factorIds {
esQuery := the.getESQueryStrBy10min(structId, factorId, start, end) esQuery := the.getESQueryStrBy10min(structId, factorId, start, end)
auth := map[string]string{"Authorization": "Bear 85a441d4-022b-4613-abba-aaa8e2693bf7"} 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 := the.getAdaptor()
adaptor.PointInfo = the.Info.PointInfo adaptor.PointInfo = the.Info.PointInfo

35
dbOperate/httpHelper.go

@ -244,6 +244,41 @@ func HttpPostWithHeader(url string, queryBody string, headers map[string]string)
return string(body), err 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) { func HttpPostFormDataWithHeader(url string, queryBody string, headers map[string]string) (string, error) {
tr := &http.Transport{ tr := &http.Transport{

Loading…
Cancel
Save