API网关在微服务系统中是必要的组件,承担了负载均衡,反向代理,统计,鉴权,监控等等职责。此处只就反向代理的功能做一个实现。
主要用到httputil.ReverseProxy
对象。它的Director
表示要重定向到的 *** 作,ModifyResponse
是你可以 *** 作返回的结果。
proxy := httputil.NewSingleHostReverseProxy(target)
查看源码会发现 NewSingleHostReverseProxy()
的实现中,对req.URL.Path
的包装不太符合实际需求,于是需要自己来实现 Director
。另外此方法返回的ReverseProxy
对象并没有实现ModifyResponse
。
一个简单的反向代理服务器:
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
type handle struct {
host string
port string
}
type Service struct {
auth *handle
user *handle
}
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var target *url.URL
if strings.Contains(r.RequestURI, "/api/stats") {
target, _ = url.Parse("http://" + s.auth.host + ":" + s.auth.port + "/show/stats")
} else if strings.Contains(r.RequestURI, "/api/autoCommentList") {
target, _ = url.Parse("http://" + s.user.host + ":" + s.user.port + "/show/autoCommentList")
} else {
fmt.Fprintf(w, "404 Not Found")
return
}
targetQuery := target.RawQuery
director := func(req *http.Request) {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path = target.Path
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if _, ok := req.Header["User-Agent"]; !ok {
req.Header.Set("User-Agent", "")
}
}
proxy := &httputil.ReverseProxy{Director: director}
proxy.ModifyResponse = func(response *http.Response) error {
cont, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(cont))
response.Body = ioutil.NopCloser(bytes.NewReader(cont))
return nil
}
proxy.ServeHTTP(w, r)
}
func startServer() {
service := &Service{
auth: &handle{host: "192.168.2.157", port: "8007"},
user: &handle{host: "192.168.2.157", port: "8007"},
}
err := http.ListenAndServe(":8888", service)
if err != nil {
log.Fatalln("ListenAndServe: ", err)
}
}
func main() {
startServer()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)