原文地址:Golang Gin實踐 連載十七 用 Nginx 部署 Go 應用html
若是已經看過前面 「十六部連載,兩部番外」,相信您的能力已經有所提高nginx
那麼,如今今天來講說簡單部署後端服務的事兒 🤓git
在本章節,咱們將簡單介紹 Nginx 以及使用 Nginx 來完成對 go-gin-example 的部署,會實現反向代理和簡單負載均衡的功能github
Nginx 是一個 Web Server,能夠用做反向代理、負載均衡、郵件代理、TCP / UDP、HTTP 服務器等等,它擁有不少吸引人的特性,例如:golang
請右拐谷歌或百度,安裝好 Nginx 以備接下來的使用segmentfault
一、 proxy_pass:配置反向代理的路徑。須要注意的是若是 proxy_pass 的 url 最後爲
/,則表示絕對路徑。不然(不含變量下)表示相對路徑,全部的路徑都會被代理過去後端
二、 upstream:配置負載均衡,upstream 默認是以輪詢的方式進行負載,另外還支持四種模式,分別是:api
(1)weight:權重,指定輪詢的機率,weight 與訪問機率成正比緩存
(2)ip_hash:按照訪問 IP 的 hash 結果值分配服務器
(3)fair:按後端服務器響應時間進行分配,響應時間越短優先級別越高
(4)url_hash:按照訪問 URL 的 hash 結果值分配
在這裏須要對 nginx.conf 進行配置,若是你不知道對應的配置文件是哪一個,可執行 nginx -t
看一下
$ nginx -t nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
顯然,個人配置文件在 /usr/local/etc/nginx/
目錄下,而且測試經過
反向代理是指以代理服務器來接受網絡上的鏈接請求,而後將請求轉發給內部網絡上的服務器,並將從服務器上獲得的結果返回給請求鏈接的客戶端,此時代理服務器對外就表現爲一個反向代理服務器。(來自百科)
因爲須要用本機做爲演示,所以先把映射配上去,打開 /etc/hosts
,增長內容:
127.0.0.1 api.blog.com
打開 nginx 的配置文件 nginx.conf(個人是 /usr/local/etc/nginx/nginx.conf),咱們作了以下事情:
增長 server 片斷的內容,設置 server_name 爲 api.blog.com 而且監聽 8081 端口,將全部路徑轉發到 http://127.0.0.1:8000/
下
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 8081; server_name api.blog.com; location / { proxy_pass http://127.0.0.1:8000/; } } }
回到 go-gin-example 的項目下,執行 make,再運行 ./go-gin-exmaple
$ make github.com/EDDYCJY/go-gin-example $ ls LICENSE README.md conf go-gin-example middleware pkg runtime vendor Makefile README_ZH.md docs main.go models routers service $ ./go-gin-example ... [GIN-debug] DELETE /api/v1/articles/:id --> github.com/EDDYCJY/go-gin-example/routers/api/v1.DeleteArticle (4 handlers) [GIN-debug] POST /api/v1/articles/poster/generate --> github.com/EDDYCJY/go-gin-example/routers/api/v1.GenerateArticlePoster (4 handlers) Actual pid is 14672
$ nginx -t nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful $ nginx -s reload
如此,就實現了一個簡單的反向代理了,是否是很簡單呢
負載均衡,英文名稱爲Load Balance(常稱 LB),其意思就是分攤到多個操做單元上進行執行(來自百科)
你能從運維口中常常聽見,XXX 負載怎麼忽然那麼高。 那麼它究竟是什麼呢?
其背後通常有多臺 server,系統會根據配置的策略(例如 Nginx 有提供四種選擇)來進行動態調整,儘量的達到各節點均衡,從而提升系統總體的吞吐量和快速響應
前提條件爲多個後端服務,那麼勢必須要多個 go-gin-example,爲了演示咱們能夠啓動多個端口,達到模擬的效果
爲了便於演示,分別在啓動前將 conf/app.ini 的應用端口修改成 8001 和 8002(也能夠作成傳入參數的模式),達到啓動 2 個監聽 8001 和 8002 的後端服務
回到 nginx.conf 的老地方,增長負載均衡所需的配置。新增 upstream 節點,設置其對應的 2 個後端服務,最後修改了 proxy_pass 指向(格式爲 http:// + upstream 的節點名稱)
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream api.blog.com { server 127.0.0.1:8001; server 127.0.0.1:8002; } server { listen 8081; server_name api.blog.com; location / { proxy_pass http://api.blog.com/; } } }
$ nginx -t nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful $ nginx -s reload
再重複訪問 http://api.blog.com:8081/auth?username={USER_NAME}}&password={PASSWORD}
,多訪問幾回便於查看效果
目前 Nginx 沒有進行特殊配置,那麼它是輪詢策略,而 go-gin-example 默認開着 debug 模式,看看請求 log 就明白了
在本章節,但願您可以簡單習得平常使用的 Web Server 背後都是一些什麼邏輯,Nginx 是什麼?反向代理?負載均衡?
怎麼簡單部署,知道了吧。