Nginx實(shí)現(xiàn)接口復(fù)制的示例代碼
1、前言
項(xiàng)目中,通常會遇到一個(gè)中轉(zhuǎn)服務(wù)需要往多個(gè)不同的系統(tǒng)推送同一份數(shù)據(jù),傳統(tǒng)做法是需要在Java代碼側(cè)中調(diào)用多個(gè)API接口進(jìn)行發(fā)送。其實(shí)Nginx作為一個(gè)請求代理轉(zhuǎn)發(fā)中間件必然具備類似的功能,常見就有mirror指令進(jìn)行流的鏡像復(fù)制。
2、接口流復(fù)制
2.1、方式一:使用mirror指令
注意:要使用nginx的mirror指令,需要nginx安裝ngx_http_mirror_module模塊??梢酝ㄟ^nginx -V命令查看。nginx 1.13.4及后續(xù)版本已經(jīng)內(nèi)置了ngx_http_mirror_module模塊,之前的版本需要手動編譯安裝。
示例場景:
- 發(fā)送一個(gè)主請求,端口8080;
- nginx同時(shí)轉(zhuǎn)發(fā)到8081服務(wù)和8082服務(wù);
- 8081和8082各自access log都有訪問記錄,且狀態(tài)正常。
開擼,我這里準(zhǔn)備的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內(nèi)容隨便填寫:
mkdir -p local mirror touch test.html echo "local.test---> test.html" >> test.html echo "mirror.test---> test.html" >> test.html
2.1.2、配置說明
- 主請求:/ 會將請求轉(zhuǎn)發(fā)到 localhostServer。
- 鏡像請求:配置了 mirror 指令,將請求同時(shí)復(fù)制一份轉(zhuǎn)發(fā)到 /mirror。/mirror 使用了 internal,防止客戶端直接訪問。
- 鏡像服務(wù):/mirror 會將請求轉(zhuǎn)發(fā)到 mirrorServer。
2.1.3、測試結(jié)果
由于8081和8082服務(wù)都配置了access log,因此當(dāng)我們訪問http://localhost:8080時(shí),預(yù)期的結(jié)果是訪問請求能得到正常的回顯,并且8081和8082服務(wù)的access log都有相應(yīng)的訪問日志。
8081-access.log:
8082-access.log:
這樣就完成了nginx實(shí)現(xiàn)接口復(fù)制的功能。Nginx 實(shí)現(xiàn)接口復(fù)制的需求通常用于在接收到請求后,將請求數(shù)據(jù)轉(zhuǎn)發(fā)到多個(gè)后端服務(wù)器(例如用于日志記錄、監(jiān)控或者負(fù)載分?jǐn)偅?/p>
2.1.4、注意事項(xiàng)
- 鏡像請求是非阻塞的,Nginx 不會等待鏡像請求的響應(yīng)。
- 鏡像功能僅支持 HTTP 請求,不支持 WebSocket 或其他非 HTTP 協(xié)議。
2.2、方式二:使用Lua
Nginx支持Lua腳本有2種方式:
- 手動編譯nginx,加入ngx_http_lua_module模塊;
- 安裝openresty,使用該組件的Nginx;
OpenResty是一個(gè)基于 Nginx 與 Lua 的高性能 Web 平臺,其內(nèi)部集成了大量精良的 Lua 庫、第三方模塊以及大多數(shù)的依賴項(xiàng)。用于方便地搭建能夠處理超高并發(fā)、擴(kuò)展性極高的動態(tài) Web 應(yīng)用、Web 服務(wù)和動態(tài)網(wǎng)關(guān)。
2.2.1、安裝Openresty
下載最新版本的openresty,OpenResty - 下載。這里下載的是1.27.1.1版本。
這里下的是源碼包,需要重新編譯。
注:這里直接編譯會使用QAT硬件加速,你需要手動安裝QAT_Engine依賴。
這里直接禁用硬件加速,編譯時(shí)排除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、配置說明
- 主請求:/ 會先處理主服務(wù)的請求,通過 ngx.location.capture 調(diào)用 /localServer。
- 鏡像請求:使用 Lua 的 resty.http 庫,手動發(fā)起 HTTP 請求將數(shù)據(jù)復(fù)制到鏡像服務(wù)器。
- 返回響應(yīng):將主請求的結(jié)果返回給客戶端。
2.2.4、測試結(jié)果
直接訪問請求:curl http://localhost:8090/test.html
8901-access.log:
8902-access.log:
3、小結(jié)
- 實(shí)時(shí)請求復(fù)制:推薦使用 ngx_http_mirror_module,簡單易用。
- 高級控制:如果需要復(fù)雜邏輯,使用 ngx_http_lua_module 配合 Lua 腳本。
- 離線分析:通過日志記錄請求數(shù)據(jù),然后離線處理。
到此這篇關(guān)于Nginx實(shí)現(xiàn)接口復(fù)制的示例代碼的文章就介紹到這了,更多相關(guān)Nginx 接口復(fù)制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx反向代理多域名的HTTP和HTTPS服務(wù)的實(shí)現(xiàn)
這篇文章主要介紹了Nginx反向代理多域名的HTTP和HTTPS服務(wù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06windows下nginx+tomcat配置負(fù)載均衡的方法
這篇文章主要介紹了windows下nginx+tomcat配置負(fù)載均衡的方法,需要的朋友可以參考下2016-09-09Nginx 連接tomcat時(shí)會話粘性問題分析及解決方法
這篇文章主要介紹了Nginx 連接tomcat時(shí)會話粘性問題分析及解決方法的相關(guān)資料,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10nginx如何使用openssl自簽名實(shí)現(xiàn)https登錄
這篇文章主要介紹了nginx使用openssl自簽名實(shí)現(xiàn)https登錄,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08Nginx代理MySQL實(shí)現(xiàn)通過域名連接數(shù)據(jù)庫的詳細(xì)教程
我們的生產(chǎn)環(huán)境基本上都部署在云服務(wù)器上,例如應(yīng)用服務(wù)器、MySQL服務(wù)器等,如果MySQL服務(wù)器直接暴露在公網(wǎng),就會存在很大的風(fēng)險(xiǎn),為了保證數(shù)據(jù)安全,MySQL服務(wù)器的端口是不對外開放的,所以本文介紹了Nginx代理MySQL實(shí)現(xiàn)通過域名連接數(shù)據(jù)庫的詳細(xì)教程2024-07-07