这是我的用例
我们有一个服务“ foobar”,其中有两个版本,legacy 并且version_2_of_doom (都在运行中)
legacy
version_2_of_doom
为了实现从legacy到的过渡version_2_of_doom,我们希望第一次将两个版本放在一起,并在两者上都收到POST请求(因为此中只有一个POST api调用)。
我看到的方式。将是
func(w http.ResponseWriter, req *http.Request) { req.URL.Host = "v2ofdoom.local:8081" req.Host = "v2ofdoom.local:8081" client := &http.Client{} client.Do(req) // legacy code
但这似乎不那么简单
它失败了 http: Request.RequestURI can't be set in client requests.
http: Request.RequestURI can't be set in client requests.
是否有一种众所周知的方法可以将此类操作(即,在不触摸的情况下进行传输)转移http.Request到另一台服务器?
http.Request
您需要将所需的值复制到新请求中。由于这与反向代理的工作非常相似,因此您可能需要查看“ net / http / httputil”的作用ReverseProxy。
ReverseProxy
创建一个新请求,然后仅将要发送的请求部分复制到下一台服务器。如果您打算在两个地方都使用它,则还需要读取和缓冲请求正文:
func handler(w http.ResponseWriter, req *http.Request) { // we need to buffer the body if we want to read it here and send it // in the request. body, err := ioutil.ReadAll(req.Body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // you can reassign the body if you need to parse it as multipart req.Body = ioutil.NopCloser(bytes.NewReader(body)) // create a new url from the raw RequestURI sent by the client url := fmt.Sprintf("%s://%s%s", proxyScheme, proxyHost, req.RequestURI) proxyReq, err := http.NewRequest(req.Method, url, bytes.NewReader(body)) // We may want to filter some headers, otherwise we could just use a shallow copy // proxyReq.Header = req.Header proxyReq.Header = make(http.Header) for h, val := range req.Header { proxyReq.Header[h] = val } resp, err := httpClient.Do(proxyReq) if err != nil { http.Error(w, err.Error(), http.StatusBadGateway) return } defer resp.Body.Close() // legacy code }