nginx 获取body参数_分布式实战:Nginx缓存之流量分发层
本文首發(fā)于Ressmix個(gè)人站點(diǎn):https://www.tpvlog.com
本章,我將進(jìn)行Nginx流量分發(fā)層的lua代碼編寫。流量分發(fā)層的Nginx服務(wù)器,會(huì)基于商品id執(zhí)行流量分發(fā)策略:
獲取請求參數(shù)productId;
對productId進(jìn)行hash運(yùn)算,將hash值與應(yīng)用層的Nginx服務(wù)器數(shù)量取模,獲取到一個(gè)應(yīng)用層服務(wù)器;
利用http包發(fā)送請求到應(yīng)用層nginx;
獲取響應(yīng)后返回。
一、OpenResty配置
我們需要對OpenResty做一些額外配置,引入依賴的工具包。我們目前的項(xiàng)目目錄結(jié)構(gòu)如下:
1/www2??/conf3????nginx.conf4??/logs5??/lua6????main.lua7??/lualib首先修改nginx.conf配置如下:
1worker_processes??1; 2error_log?logs/error.log; 3events?{ 4????worker_connections?1024; 5} 6http?{ 7????lua_package_path?"$prefix/lualib/?.lua;;";?? 8????lua_package_cpath?"$prefix/lualib/?.so;;";? 9????server?{10????????listen?80;11????????location?/product?{12????????????default_type?text/html;13????????????content_by_lua_file?lua/main.lua;14????????}15????}16}上面的$prefix是指我們啟動(dòng)openresty時(shí)代入的-p參數(shù)。然后,我們需要將一些官方或第三方的lua庫放到lualib目錄下,比如我們可以直接將openresty默認(rèn)安裝包下的lualib直接復(fù)制到我們自己的項(xiàng)目的lualib中。
然后,在我們項(xiàng)目中的lualib/resty目錄下,執(zhí)行以下命令,安裝http庫:
1cd?/usr/local/www/lualib/resty/??2wget?https://github.com/ledgetech/lua-resty-http/blob/master/lib/resty/http.lua??3wget?https://github.com/ledgetech/lua-resty-http/blob/master/lib/resty/http_headers.lua二、功能實(shí)現(xiàn)
接著,我們就要在ressmix-dsf01上的main.lua中實(shí)現(xiàn)流量分發(fā)邏輯了,我們直接根據(jù)請求URI中的productId進(jìn)行哈希:
1--獲取http請求中的uri參數(shù) 2local?uri_args?=?ngx.req.get_uri_args() 3local?productId?=?uri_args["productId"] 4local?shopId?=?uri_args["shopId"] 5 6--確定路由到那臺應(yīng)用層Nginx服務(wù)器 7local?host?=?{"192.168.0.109",?"192.168.0.110"} 8local?hash?=?ngx.crc32_long(productId) 910hash?=?(hash?%?2)?+?111backend?=?"http://"..host[hash]1213local?requestBody?=?"/product?productId="..productId.."&shopId="..shopId1415local?http?=?require("resty.http")16local?httpc?=?http.new()1718--發(fā)起請求19local?resp,?err?=?httpc:request_uri(backend,?{20????method?=?"GET",21????path?=?requestBody22})2324if?not?resp?then25????ngx.say("request?error?:",?err)26????return27end2829--響應(yīng)30ngx.say(resp.body)3132httpc:close()最后,分別啟動(dòng)ressmix-dsf01、ressmix-dsf02、ressmix-dsf03上的openresty:
1openresty?-p?/usr/local/www?-c?/usr/local/www/conf/nginx.conf然后在ressmix-dsf01上發(fā)起兩筆請求測試下:
1[root@ressmix-dsf01?lua]#?curl?-i?http://192.168.0.107/product?productId=117&shopId=123 2HTTP/1.1?200?OK 3Server:?openresty/1.15.8.3 4Date:?Wed,?03?Jun?2020?19:25:55?GMT 5Content-Type:?text/html 6Transfer-Encoding:?chunked 7Connection:?keep-alive 8 9http://192.168.0.107/product?productId=117&shopId=12310hello,?world,ressmix-dsf02
1[root@ressmix-dsf01?lua]#?curl?-i?http://192.168.0.107/product?productId=112&shopId=123 2HTTP/1.1?200?OK 3Server:?openresty/1.15.8.3 4Date:?Wed,?03?Jun?2020?19:26:26?GMT 5Content-Type:?text/html 6Transfer-Encoding:?chunked 7Connection:?keep-alive 8 9http://192.168.0.107/product?productId=112&shopId=12310hello,?world,ressmix-dsf03
可以看到,ressmix-dsf01上的OpenResty流量分發(fā)層已經(jīng)能夠根據(jù)productId將請求分發(fā)到應(yīng)用層了。
三、總結(jié)
本章,我通過lua腳本腳本實(shí)現(xiàn)了Nginx流量分發(fā)層的分發(fā)邏輯,核心是利用了OpenResty的http包,讀者在使用OpenResty時(shí)需要多參考官方的文檔,因?yàn)镺penResty有很多API使用上的坑,也沒有一個(gè)比較好的調(diào)試工具,所以出現(xiàn)錯(cuò)誤時(shí)有時(shí)會(huì)很難排查。
總結(jié)
以上是生活随笔為你收集整理的nginx 获取body参数_分布式实战:Nginx缓存之流量分发层的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 显存VS内存VS显卡:谁才是电脑的真正大
- 下一篇: Keepalived+Nginx实现高可