From 71a5c977ccda5b1cf91082a232b3419a809a4d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B9=BF=E9=91=AB?= Date: Fri, 25 Jun 2021 19:18:42 +0800 Subject: [PATCH] change query endpoint way --- src/config/address.go | 2 +- src/controllers/endpointcontroller.go | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/config/address.go b/src/config/address.go index ada5212..6915457 100644 --- a/src/config/address.go +++ b/src/config/address.go @@ -34,7 +34,7 @@ type ServerUrl struct { const ( MEP_AUTH_APIGW_URL string = "https://${MEP_IP}:${MEP_APIGW_PORT}/mep/token" MEP_SER_REGISTER_APIGW_URL string = "https://${MEP_IP}:${MEP_APIGW_PORT}/mep/mec_service_mgmt/v1/applications/${appInstanceId}/services" - MEP_SER_QUERY_BY_NAME_APIGW_URL string = "https://${MEP_IP}:${MEP_APIGW_PORT}/mep/mec_service_mgmt/v1/services?ser_name=" + MEP_SER_QUERY_BY_NAME_APIGW_URL string = "https://${MEP_IP}:${MEP_APIGW_PORT}/mep/mec_service_mgmt/v1/services" MEP_HEART_BEAT_APIGW_URL string = "https://${MEP_IP}:${MEP_APIGW_PORT}" MEP_IP string = "${MEP_IP}" MEP_APIGW_PORT string = "${MEP_APIGW_PORT}" diff --git a/src/controllers/endpointcontroller.go b/src/controllers/endpointcontroller.go index 6991796..11600b8 100644 --- a/src/controllers/endpointcontroller.go +++ b/src/controllers/endpointcontroller.go @@ -18,12 +18,11 @@ package controllers import ( "encoding/json" + "github.com/astaxie/beego" + log "github.com/sirupsen/logrus" "mep-agent/src/config" "mep-agent/src/service" "mep-agent/src/util" - - "github.com/astaxie/beego" - log "github.com/sirupsen/logrus" ) type EndpointController struct { @@ -31,6 +30,7 @@ type EndpointController struct { } type Service struct { + SerName string `json:"serName"` TransportInfo TransportInfo `yaml:"transportInfo" json:"transportInfo"` } @@ -63,7 +63,7 @@ func (c *EndpointController) Get() { log.Error("Failed to get serviceUrl") } - url := server.MepServiceDiscoveryUrl + serName + url := server.MepServiceDiscoveryUrl log.Info("get endpoint url: " + url) requestData := service.RequestData{Data: "", Url: url, Token: &util.MepToken} resBody, errPostRequest := service.SendQueryRequest(requestData) @@ -72,17 +72,23 @@ func (c *EndpointController) Get() { } var resBodyMap []Service - log.Info("resBodyMap: " + resBody) + log.Infof("resBody: %s", resBody) err := json.Unmarshal([]byte(resBody), &resBodyMap) + if err != nil { log.Error("Unmarshal failed") c.Data["json"] = "Service does not exist." c.Ctx.ResponseWriter.WriteHeader(400) } else { - transportInfo := resBodyMap[0].TransportInfo - log.Info("Endpoint: ", transportInfo.Endpoint) - c.Data["json"] = transportInfo.Endpoint - c.Ctx.ResponseWriter.WriteHeader(200) + for _, service := range resBodyMap { + if serName == service.SerName { + transportInfo := service.TransportInfo + log.Info("Endpoint: ", transportInfo.Endpoint) + c.Data["json"] = transportInfo.Endpoint + c.Ctx.ResponseWriter.WriteHeader(200) + break + } + } } c.ServeJSON() } -- Gitee