k8s服务A内部调用服务B的方式
k8s服务A内部调用服务B的方式
eg:https://iot.d.blueshark.com/dev/bike/maintain/info
内部:http://服务名称.名称空间:端口/服务内部接口
http://bikesvc.sg-bs:8080/bike/maintain/clearAlert
func MaintainClearAlert(c *gin.Context) {
type (
In struct {
Sn string `json:"sn" binding:"required"`
Total int `json:"total" binding:"required"`
MaintainTime int64 `json:"maintainTime"` // 时间
}
)
var in In
var out = &json.RawMessage{}
if !qgin.CheckJsonParam(c, &in) {
return
}
headers := map[string]string{"ts": fmt.Sprintf("%d", time.Now().Unix())}
_, err := qhttp.PostJSON("http://bikesvc.sg-bs:8080/bike/maintain/clearAlert", headers, in, out)
if err != nil {
logrus.Error(err)
return
}
c.JSON(http.StatusOK, out)
return
}
func PostJSON(url string, headers map[string]string, body interface{}, result interface{}) (resp *http.Response, err error) {
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewReader(b))
if err != nil {
return nil, err
}
if headers != nil {
for k, v := range headers {
req.Header.Add(k, v)
}
}
rsp, err := QHTTPClient.Do(req)
if err != nil {
return nil, err
}
if result != nil { //if result != nil, try Unmarshal the body
defer rsp.Body.Close()
rspBody, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(rspBody, result)
if err != nil {
return rsp, err
}
}
return rsp, err
}
选择了IT,必定终身学习