1行Go代碼實現(xiàn)反向代理的示例
暫且放下你的編程語言來瞻仰下我所見過的最棒的標(biāo)準(zhǔn)庫。
為項目選擇編程語言和挑選你最愛的球隊不一樣。應(yīng)該從實用主義出發(fā),根據(jù)特定的工作選擇合適的工具。
在這篇文章中我會告訴你從何時開始并且為什么我認(rèn)為 Go 語言如此閃耀,具體來說是它的標(biāo)準(zhǔn)庫對于基本的網(wǎng)絡(luò)編程來說顯得非常穩(wěn)固。更具體一點,我們將要編寫一個反向代理程序。
Go 為此提供了很多,但真正支撐起它的在于這些低級的網(wǎng)絡(luò)管道任務(wù),沒有更好的語言了。
反向代理是什么? 有個很棒的說法是流量轉(zhuǎn)發(fā) 。我獲取到客戶端來的請求,將它發(fā)往另一個服務(wù)器,從服務(wù)器獲取到響應(yīng)再回給原先的客戶端。反向的意義簡單來說在于這個代理自身決定了何時將流量發(fā)往何處。
為什么這很有用?因為反向代理的概念是如此簡單以至于它可以被應(yīng)用于許多不同的場景:負(fù)載均衡,A/B 測試,高速緩存,驗證等等。
當(dāng)讀完這篇文章之后,你會學(xué)到:
- 如何響應(yīng) HTTP 請求
- 如何解析請求體
- 如何通過反向代理將流量轉(zhuǎn)發(fā)到另一臺服務(wù)器
我們的反向代理項目
我們來實際寫一下項目。我們需要一個 Web 服務(wù)器能夠提供以下功能:
- 獲取到請求
- 讀取請求體,特別是 proxy_condition 字段
- 如果代理域為 A,則轉(zhuǎn)發(fā)到 URL 1
- 如果代理域為 B,則轉(zhuǎn)發(fā)到 URL 2
- 如果代理域都不是以上,則轉(zhuǎn)發(fā)到默認(rèn)的 URL
準(zhǔn)備工作
- Go 語言環(huán)境。
- http-server 用來創(chuàng)建簡單的服務(wù)。
環(huán)境配置
我們要做的第一件事是將我們的配置信息寫入環(huán)境變量,如此就可以使用它們而不必寫死在我們的源代碼中。
我發(fā)現(xiàn)最好的方式是創(chuàng)建一個包含所需環(huán)境變量的 .env
文件。
以下就是我為特定項目編寫的文件內(nèi)容:
export PORT=1330 export A_CONDITION_URL="http://localhost:1331" export B_CONDITION_URL="http://localhost:1332" export DEFAULT_CONDITION_URL=http://localhost:1333
這是我從 12 Factor App 項目中獲得的技巧。
保存完 .env
文件之后就可以運行:
source .env
在任何時候都可以運行該指令來將配置加載進(jìn)環(huán)境變量。
項目基礎(chǔ)工作
接著我們創(chuàng)建 main.go
文件做如下事情:
- 將
PORT
,A_CONDITION_URL
,B_CONDITION_URL
和DEFAULT_CONDITION_URL
變量通過日志打印到控制臺。 - 在
/
路徑上監(jiān)聽請求:
package main import ( "bytes" "encoding/json" "io/ioutil" "log" "net/http" "net/http/httputil" "net/url" "os" "strings" ) // Get env var or default func getEnv(key, fallback string) string { if value, ok := os.LookupEnv(key); ok { return value } return fallback } // Get the port to listen on func getListenAddress() string { port := getEnv("PORT", "1338") return ":" + port } // Log the env variables required for a reverse proxy func logSetup() { a_condtion_url := os.Getenv("A_CONDITION_URL") b_condtion_url := os.Getenv("B_CONDITION_URL") default_condtion_url := os.Getenv("DEFAULT_CONDITION_URL") log.Printf("Server will run on: %s\n", getListenAddress()) log.Printf("Redirecting to A url: %s\n", a_condtion_url) log.Printf("Redirecting to B url: %s\n", b_condtion_url) log.Printf("Redirecting to Default url: %s\n", default_condtion_url) } // Given a request send it to the appropriate url func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) { // We will get to this... } func main() { // Log setup values logSetup() // start server http.HandleFunc("/", handleRequestAndRedirect) if err := http.ListenAndServe(getListenAddress(), nil); err != nil { panic(err) } }
現(xiàn)在你就可以運行代碼了。
解析請求體
有了項目的基本骨架之后,我們需要添加邏輯來處理解析請求的請求體部分。更新 handleRequestAndRedirect
函數(shù)來從請求體中解析出 proxy_condition
字段。
type requestPayloadStruct struct { ProxyCondition string `json:"proxy_condition"` } // Get a json decoder for a given requests body func requestBodyDecoder(request *http.Request) *json.Decoder { // Read body to buffer body, err := ioutil.ReadAll(request.Body) if err != nil { log.Printf("Error reading body: %v", err) panic(err) } // Because go lang is a pain in the ass if you read the body then any susequent calls // are unable to read the body again.... request.Body = ioutil.NopCloser(bytes.NewBuffer(body)) return json.NewDecoder(ioutil.NopCloser(bytes.NewBuffer(body))) } // Parse the requests body func parseRequestBody(request *http.Request) requestPayloadStruct { decoder := requestBodyDecoder(request) var requestPayload requestPayloadStruct err := decoder.Decode(&requestPayload) if err != nil { panic(err) } return requestPayload } // Given a request send it to the appropriate url func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) { requestPayload := parseRequestBody(req) // ... more to come }
通過 proxy_condition 判斷將流量發(fā)往何處
現(xiàn)在我們從請求中取得了 proxy_condition
的值,可以根據(jù)它來判斷我們要反向代理到何處。記住上文我們提到的三種情形:
- 如果
proxy_condition
值為A
,我們將流量發(fā)送到A_CONDITION_URL
- 如果
proxy_condition
值為B
,我們將流量發(fā)送到B_CONDITION_URL
- 其他情況將流量發(fā)送到
DEFAULT_CONDITION_URL
// Log the typeform payload and redirect url func logRequestPayload(requestionPayload requestPayloadStruct, proxyUrl string) { log.Printf("proxy_condition: %s, proxy_url: %s\n", requestionPayload.ProxyCondition, proxyUrl) } // Get the url for a given proxy condition func getProxyUrl(proxyConditionRaw string) string { proxyCondition := strings.ToUpper(proxyConditionRaw) a_condtion_url := os.Getenv("A_CONDITION_URL") b_condtion_url := os.Getenv("B_CONDITION_URL") default_condtion_url := os.Getenv("DEFAULT_CONDITION_URL") if proxyCondition == "A" { return a_condtion_url } if proxyCondition == "B" { return b_condtion_url } return default_condtion_url } // Given a request send it to the appropriate url func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) { requestPayload := parseRequestBody(req) url := getProxyUrl(requestPayload.ProxyCondition) logRequestPayload(requestPayload, url) // more still to come... }
反向代理到 URL
最終我們來到了實際的反向代理部分。在如此多的語言中要編寫一個反向代理需要考慮很多東西,寫大段的代碼?;蛘咧辽僖胍粋€復(fù)雜的外部庫。
然而 Go 的標(biāo)準(zhǔn)庫使得創(chuàng)建一個反向代理非常簡單以至于你都不敢相信。下面就是你所需要的最關(guān)鍵的一行代碼:
httputil.NewSingleHostReverseProxy(url).ServeHTTP(res, req)
注意下面代碼中我們做了些許修改來讓它能完整地支持 SSL 重定向(雖然不是必須的)。
// Serve a reverse proxy for a given url func serveReverseProxy(target string, res http.ResponseWriter, req *http.Request) { // parse the url url, _ := url.Parse(target) // create the reverse proxy proxy := httputil.NewSingleHostReverseProxy(url) // Update the headers to allow for SSL redirection req.URL.Host = url.Host req.URL.Scheme = url.Scheme req.Header.Set("X-Forwarded-Host", req.Header.Get("Host")) req.Host = url.Host // Note that ServeHttp is non blocking and uses a go routine under the hood proxy.ServeHTTP(res, req) } // Given a request send it to the appropriate url func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) { requestPayload := parseRequestBody(req) url := getProxyUrl(requestPayload.ProxyCondition) logRequestPayload(requestPayload, url) serveReverseProxy(url, res, req) }
全部啟動
好了,現(xiàn)在啟動我們的反向代理程序讓其監(jiān)聽 1330
端口。讓其他的 3 個簡單的服務(wù)分別監(jiān)聽 1331–1333
端口(在各自的終端中)。
- source .env && go install && $GOPATH/bin/reverse-proxy-demo
- http-server -p 1331
- http-server -p 1332
- http-server -p 1333
這些服務(wù)都啟動之后,我們就可以在另一個終端中像下面這樣開始發(fā)送帶有 JSON 體的請求了:
curl --request GET \ --url http://localhost:1330/ \ --header 'content-type: application/json' \ --data '{ "proxy_condition": "a" }'
如果你在找一個好用的 HTTP 請求客戶端,我極力推薦 Insomnia 。
然后我們就會看到我們的反向代理將流量轉(zhuǎn)發(fā)給了我們根據(jù) proxy_condition
字段配置的 3 臺服務(wù)中的其中一臺。
總結(jié)
Go 為此提供了很多,但真正支撐起它的在于這些低級的網(wǎng)絡(luò)管道任務(wù),沒有更好的語言了。我們寫的這個程序簡單,高性能,可靠并且隨時可用于生產(chǎn)環(huán)境。
我能看到在以后我會經(jīng)常使用 Go 來編寫簡單的服務(wù)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
代碼是開源的,你可以在 Github 上找到。 :heart: 在 Twitter 上我只聊關(guān)于編程和遠(yuǎn)程工作相關(guān)的東西。如果關(guān)注我,你不會后悔的。
相關(guān)文章
Go語言利用Unmarshal解析json字符串的實現(xiàn)
本文主要介紹了Go語言利用Unmarshal解析json字符串的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Go語言題解LeetCode1266訪問所有點的最小時間示例
這篇文章主要為大家介紹了Go語言題解LeetCode1266訪問所有點的最小時間示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Linux環(huán)境下編譯并運行g(shù)o項目的全過程
Go語言是Google的開源編程語言,廣泛應(yīng)用于云計算、分布式系統(tǒng)開發(fā)等領(lǐng)域,在Linux上也有大量的應(yīng)用場景,這篇文章主要給大家介紹了關(guān)于Linux環(huán)境下編譯并運行g(shù)o項目的相關(guān)資料,需要的朋友可以參考下2023-11-11