暫且放下你的編程語言來瞻仰下我所見過的最棒的標準庫。

為項目選擇編程語言和挑選你最愛的球隊不一樣。應該從實用主義出發,根據特定的工作選擇合適的工具。
在這篇文章中我會告訴你從何時開始并且為什么我認為 Go 語言如此閃耀,具體來說是它的標準庫對于基本的網絡編程來說顯得非常穩固。更具體一點,我們將要編寫一個反向代理程序。
Go 為此提供了很多,但真正支撐起它的在于這些低級的網絡管道任務,沒有更好的語言了。
反向代理是什么? 有個很棒的說法是流量轉發 。我獲取到客戶端來的請求,將它發往另一個服務器,從服務器獲取到響應再回給原先的客戶端。反向的意義簡單來說在于這個代理自身決定了何時將流量發往何處。

為什么這很有用?因為反向代理的概念是如此簡單以至于它可以被應用于許多不同的場景:負載均衡,A/B 測試,高速緩存,驗證等等。
當讀完這篇文章之后,你會學到:
- 如何響應 HTTP 請求
- 如何解析請求體
- 如何通過反向代理將流量轉發到另一臺服務器
我們的反向代理項目
我們來實際寫一下項目。我們需要一個 Web 服務器能夠提供以下功能:
- 獲取到請求
- 讀取請求體,特別是 proxy_condition 字段
- 如果代理域為 A,則轉發到 URL 1
- 如果代理域為 B,則轉發到 URL 2
- 如果代理域都不是以上,則轉發到默認的 URL
準備工作
- Go 語言環境。
- http-server 用來創建簡單的服務。
環境配置
我們要做的第一件事是將我們的配置信息寫入環境變量,如此就可以使用它們而不必寫死在我們的源代碼中。
我發現最好的方式是創建一個包含所需環境變量的 .env
文件。
以下就是我為特定項目編寫的文件內容:
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
文件之后就可以運行:
在任何時候都可以運行該指令來將配置加載進環境變量。
項目基礎工作
接著我們創建 main.go
文件做如下事情:
- 將
PORT
, A_CONDITION_URL
, B_CONDITION_URL
和 DEFAULT_CONDITION_URL
變量通過日志打印到控制臺。
- 在
/
路徑上監聽請求:
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)
}
}
現在你就可以運行代碼了。
解析請求體
有了項目的基本骨架之后,我們需要添加邏輯來處理解析請求的請求體部分。更新 handleRequestAndRedirect
函數來從請求體中解析出 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 判斷將流量發往何處
現在我們從請求中取得了 proxy_condition
的值,可以根據它來判斷我們要反向代理到何處。記住上文我們提到的三種情形:
- 如果
proxy_condition
值為 A
,我們將流量發送到 A_CONDITION_URL
- 如果
proxy_condition
值為 B
,我們將流量發送到 B_CONDITION_URL
- 其他情況將流量發送到
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
最終我們來到了實際的反向代理部分。在如此多的語言中要編寫一個反向代理需要考慮很多東西,寫大段的代碼。或者至少引入一個復雜的外部庫。
然而 Go 的標準庫使得創建一個反向代理非常簡單以至于你都不敢相信。下面就是你所需要的最關鍵的一行代碼:
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)
}
全部啟動
好了,現在啟動我們的反向代理程序讓其監聽 1330
端口。讓其他的 3 個簡單的服務分別監聽 1331–1333
端口(在各自的終端中)。
- source .env go install $GOPATH/bin/reverse-proxy-demo
- http-server -p 1331
- http-server -p 1332
- http-server -p 1333
這些服務都啟動之后,我們就可以在另一個終端中像下面這樣開始發送帶有 JSON 體的請求了:
curl --request GET \
--url http://localhost:1330/ \
--header 'content-type: application/json' \
--data '{
"proxy_condition": "a"
}'
如果你在找一個好用的 HTTP 請求客戶端,我極力推薦 Insomnia 。
然后我們就會看到我們的反向代理將流量轉發給了我們根據 proxy_condition
字段配置的 3 臺服務中的其中一臺。

總結
Go 為此提供了很多,但真正支撐起它的在于這些低級的網絡管道任務,沒有更好的語言了。我們寫的這個程序簡單,高性能,可靠并且隨時可用于生產環境。
我能看到在以后我會經常使用 Go 來編寫簡單的服務。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
代碼是開源的,你可以在 Github 上找到。 :heart: 在 Twitter 上我只聊關于編程和遠程工作相關的東西。如果關注我,你不會后悔的。
您可能感興趣的文章:- go語言實現簡單http服務的方法
- go語言實現一個簡單的http客戶端抓取遠程url的方法
- go語言實現一個最簡單的http文件服務器實例
- 一個簡單的Golang實現的HTTP Proxy方法