Nginx實現接口復制的示例代碼
1、前言
項目中,通常會遇到一個中轉服務需要往多個不同的系統(tǒng)推送同一份數據,傳統(tǒng)做法是需要在Java代碼側中調用多個API接口進行發(fā)送。其實Nginx作為一個請求代理轉發(fā)中間件必然具備類似的功能,常見就有mirror指令進行流的鏡像復制。
2、接口流復制
2.1、方式一:使用mirror指令
注意:要使用nginx的mirror指令,需要nginx安裝ngx_http_mirror_module模塊??梢酝ㄟ^nginx -V命令查看。nginx 1.13.4及后續(xù)版本已經內置了ngx_http_mirror_module模塊,之前的版本需要手動編譯安裝。
示例場景:
- 發(fā)送一個主請求,端口8080;
- nginx同時轉發(fā)到8081服務和8082服務;
- 8081和8082各自access log都有訪問記錄,且狀態(tài)正常。
開擼,我這里準備的nginx版本為1.20.1。
2.1.1、nginx配置
server { listen 8080; index index.php index.html; server_name localhost; location / { mirror /mirror; proxy_pass http://localhostServer; } location = /mirror { internal; proxy_pass http://mirrorServer$request_uri; } } upstream localhostServer { server localhost:8081; } upstream mirrorServer { server localhost:8082; } server { listen 8081; server_name localhost; access_log /var/log/nginx/8081-access.log; root html/local; } server { listen 8082; server_name localhost; access_log /var/log/nginx/8082-access.log; root html/mirror; }
接著在nginx的html目錄下,創(chuàng)建local目錄和mirror目錄,并創(chuàng)建文件test.html。test.html內容隨便填寫:
mkdir -p local mirror touch test.html echo "local.test---> test.html" >> test.html echo "mirror.test---> test.html" >> test.html
2.1.2、配置說明
- 主請求:/ 會將請求轉發(fā)到 localhostServer。
- 鏡像請求:配置了 mirror 指令,將請求同時復制一份轉發(fā)到 /mirror。/mirror 使用了 internal,防止客戶端直接訪問。
- 鏡像服務:/mirror 會將請求轉發(fā)到 mirrorServer。
2.1.3、測試結果
由于8081和8082服務都配置了access log,因此當我們訪問http://localhost:8080時,預期的結果是訪問請求能得到正常的回顯,并且8081和8082服務的access log都有相應的訪問日志。
8081-access.log:
8082-access.log:
這樣就完成了nginx實現接口復制的功能。Nginx 實現接口復制的需求通常用于在接收到請求后,將請求數據轉發(fā)到多個后端服務器(例如用于日志記錄、監(jiān)控或者負載分攤)。
2.1.4、注意事項
- 鏡像請求是非阻塞的,Nginx 不會等待鏡像請求的響應。
- 鏡像功能僅支持 HTTP 請求,不支持 WebSocket 或其他非 HTTP 協(xié)議。
2.2、方式二:使用Lua
Nginx支持Lua腳本有2種方式:
- 手動編譯nginx,加入ngx_http_lua_module模塊;
- 安裝openresty,使用該組件的Nginx;
OpenResty是一個基于 Nginx 與 Lua 的高性能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模塊以及大多數的依賴項。用于方便地搭建能夠處理超高并發(fā)、擴展性極高的動態(tài) Web 應用、Web 服務和動態(tài)網關。
2.2.1、安裝Openresty
下載最新版本的openresty,OpenResty - 下載。這里下載的是1.27.1.1版本。
這里下的是源碼包,需要重新編譯。
注:這里直接編譯會使用QAT硬件加速,你需要手動安裝QAT_Engine依賴。
這里直接禁用硬件加速,編譯時排除qat:
./configure --without-http_qat_module # 安裝 make sudo make install # 這里還需要安裝resty.http模塊 /usr/local/openresty/bin/opm get ledgetech/lua-resty-http
安裝完成后,啟動openresty的nginx:
/usr/local/openresty/nginx/sbin/nginx
2.2.2、nginx配置
server { listen 8090; location / { content_by_lua_block { local http = require "resty.http" local res = ngx.location.capture("/localServer") local httpc = http.new() httpc:set_timeout(2000) local ok, err = httpc:request_uri("http://127.0.0.1:8092/test.html", { method = ngx.var.request_method, body = ngx.var.request_body, headers = ngx.req.get_headers(), follow_redirects = true, }) if not ok then ngx.log(ngx.ERR, "Mirror request failed: ", err) end ngx.say(res.body) } } location /localServer { proxy_pass http://127.0.0.1:8091; } } server { listen 8091; server_name 127.0.0.1; access_log /usr/local/openresty/nginx/logs/8091-access.log; root html/local; } server { listen 8092; server_name 127.0.0.1; access_log /usr/local/openresty/nginx/logs/8092-access.log; root html/mirror; }
2.2.3、配置說明
- 主請求:/ 會先處理主服務的請求,通過 ngx.location.capture 調用 /localServer。
- 鏡像請求:使用 Lua 的 resty.http 庫,手動發(fā)起 HTTP 請求將數據復制到鏡像服務器。
- 返回響應:將主請求的結果返回給客戶端。
2.2.4、測試結果
直接訪問請求:curl http://localhost:8090/test.html
8901-access.log:
8902-access.log:
3、小結
- 實時請求復制:推薦使用 ngx_http_mirror_module,簡單易用。
- 高級控制:如果需要復雜邏輯,使用 ngx_http_lua_module 配合 Lua 腳本。
- 離線分析:通過日志記錄請求數據,然后離線處理。
到此這篇關于Nginx實現接口復制的示例代碼的文章就介紹到這了,更多相關Nginx 接口復制內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!