nginx+lua+redis 灰度發(fā)布實(shí)現(xiàn)方案
背景: 公司要把現(xiàn)有的某傳統(tǒng)項(xiàng)目進(jìn)行微服務(wù)化,拆分后要分批次預(yù)發(fā)布,實(shí)現(xiàn)某部分使用戶使用微服務(wù)模塊,其他用戶使用傳統(tǒng)項(xiàng)目。待微服務(wù)穩(wěn)定、無bug后全部用戶遷移至微服務(wù)系統(tǒng)。
以上為背景,實(shí)現(xiàn)此方案使用nginx+lua+redis灰度發(fā)布方案。nginx+lua實(shí)現(xiàn)反向代理,獲取客戶端ip,redis存放ip信息(ip為訪問微服務(wù)允許的地址).
有兩種方案可以實(shí)現(xiàn)
第一種:nginx+lua獲取用戶ip,然后再用lua編寫程序直接訪問redis集群,查詢ip信息返回結(jié)果;
第二種:nginx+lua獲取用戶ip,然后用lua編寫程序用http請(qǐng)求到redis緩存服務(wù)(單獨(dú)微服務(wù)),緩存服務(wù)返回ip地址;
開始是考慮第一種方案,用為openresty沒有太好的redis cluster的支持包,資料少;如果用第二種方案redis緩存服務(wù)可以單獨(dú)拎出來,不但可以為nginx使用,還可以為其他服務(wù)所用。
兩種方案都會(huì)說下,不過第一種方案用的是單機(jī)redis
我假設(shè)都有了OpenResty和redis環(huán)境
第一種方案:
在nginx.conf http塊中添加代碼
//外部配置
include lua.conf;
//新系統(tǒng)地址
upstream new{
server 192.168.1.103:8081;
}
//老系統(tǒng)地址
upstream old{
server 192.168.1.103:8080;
}lua.conf代碼
//引入redis模塊,只能聯(lián)單機(jī)
local redis = require "resty.redis"
local cache = redis.new()
cache:set_timeout(60000)
//鏈接
local ok, err = cache.connect(cache, '192.168.19.10', 6379)
// 這里如果鏈接redis失敗,則轉(zhuǎn)發(fā)到@old對(duì)應(yīng)的服務(wù)器 (傳統(tǒng)服務(wù))
if not ok then
ngx.exec("@old")
return
end
//如果nginx只有一層分發(fā)層,這下面四行代碼可以不寫
local local_ip = ngx.req.get_headers()["X-Real-IP"]
if local_ip == nil then
local_ip = ngx.req.get_headers()["x_forwarded_for"]
end
//從remote_addr變量中拿到客戶端ip,同樣nginx只有一層的時(shí)候此變量為客戶端ip,多層不是
if local_ip == nil then
local_ip = ngx.var.remote_addr
end
//在redis中根據(jù)客戶端ip獲取是否存在值; redis中存放的是key:ip val:ip, 存放的ip訪問微服務(wù)
local intercept = cache:get(local_ip)
//如果存在則轉(zhuǎn)發(fā)到@new對(duì)應(yīng)的服務(wù)器(微服務(wù))
if intercept == local_ip then
ngx.exec("@new")
return
end
//如果不存在,則轉(zhuǎn)發(fā)到@old對(duì)應(yīng)的服務(wù)器(傳統(tǒng)服務(wù))
ngx.exec("@old")
//關(guān)閉客戶端
local ok, err = cache.close()
if not ok then
ngx.say("failed to close:", err)
return
end
邏輯較簡單,但是有些問題1:redis集群要配置多ip,防止宕機(jī)問題 2:鏈接問題,如果有線程池最好了。這里不再多說
第二種方案:
nginx.conf不變
lua.conf 中的redistest.lua 改為httptest.lua
httptest.lua代碼如下
//這里緩存服務(wù)地址
backend = "http://192.168.1.156:8080"
//緩存服務(wù)訪問路徑
local method = "httptest"
local requestBody = "/"..method
//模塊
local http = require("resty.http")
local httpc = http.new()
//設(shè)置超時(shí)時(shí)間
httpc:set_timeout(1000)
//發(fā)送請(qǐng)求
local resp, err = httpc:request_uri(backend, {--
method = "GET", --
path = requestBody,
keepalive = false
})
//如果請(qǐng)求失敗訪問老系統(tǒng)
if not resp then--
ngx.exec("@old")----
return--
end
//緩存服務(wù)取回ip
local isHave = resp.body
//關(guān)閉連接
httpc:close()
//請(qǐng)求ip
local local_ip = ngx.var.remote_addr
//命中則訪問微服務(wù)
if isHave == local_ip then
ngx.exec("@new")
return
end
//沒命中訪問老系統(tǒng)
ngx.exec("@old")
這里的緩存地址只有一個(gè),實(shí)際中有多個(gè),可以采用隨機(jī)取值去訪問一個(gè)。超時(shí)時(shí)間一定要設(shè)置,如果緩存系統(tǒng)一直沒響應(yīng)或者緩存服務(wù)宕機(jī)則直接訪問老系統(tǒng)。
例子中在緩存服務(wù)中只是存儲(chǔ)了真實(shí)ip,實(shí)際中是存儲(chǔ)的IP網(wǎng)段,nginx拿到真實(shí)ip字符拆分然后去匹配的。
這里有兩點(diǎn)優(yōu)化沒有寫出.
1.http可以用連接池代替;
2.可以在nginx內(nèi)部使用緩存,把命中的ip都存起來,再有鏈接可以先走本地緩存,再走緩存服務(wù)可以提高性能。
目前http沒有找到相關(guān)連接池,所以一直在非連接池下運(yùn)行,性能還可以。
另一點(diǎn)直接在nginx中使用了內(nèi)存進(jìn)行緩存。
在nginx.conf http塊中添加代碼
結(jié)構(gòu)為: lua_shared_dict [name][size]
lua_shared_dict rediscache 100m;
更改httptest.lua的代碼如下:
backend = "http://192.168.1.156:8080"
//加載共享內(nèi)存
local cache_ngx = ngx.shared.rediscache
local local_ip = ngx.var.remote_addr
//優(yōu)先從本地緩存中去取
local cacheip = cache_ngx:get(local_ip)
//本地緩存中不存在,去緩存服務(wù)中去取,然后加載到本地緩存
if cacheip == "" or cacheip == nil then
local http = require("resty.http")
local httpc = http.new()
httpc:set_timeout(1000)
local method = "httptest"
local requestBody = "/" .. method
local resp, err = httpc:request_uri(backend, {
method = "GET",
path = requestBody,
keepalive=false
})
if not resp then
ngx.exec("@new")
return
end
cacheip = resp.body
httpc:close()
//加載到本地緩存,設(shè)置過期時(shí)間
cache_ngx:set(local_ip, cacheip, 10*60)
end
if cacheip == local_ip then
ngx.exec("@new")
return
end
ngx.exec("@old")到此這篇關(guān)于nginx+lua+redis 灰度發(fā)布實(shí)現(xiàn)方案的文章就介紹到這了,更多相關(guān)nginx lua redis 灰度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- nginx+lua+redis實(shí)現(xiàn)降級(jí)的示例代碼
- Nginx + lua 實(shí)現(xiàn)WAF的詳細(xì)過程
- Nginx Lua Waf 插件一鍵部署的操作示例
- Nginx如何安裝配置Lua支持
- Nginx Lua 根據(jù)參數(shù)請(qǐng)求轉(zhuǎn)發(fā)的實(shí)現(xiàn)
- Nginx Lua 緩存配置的實(shí)現(xiàn)步驟
- 使用nginx+lua進(jìn)行token鑒權(quán)的方法
- Nginx中使用Lua腳本與圖片的縮略圖處理的實(shí)現(xiàn)
- Nginx配置中使用Lua腳本的實(shí)現(xiàn)步驟
相關(guān)文章
訪問nginx顯示未找到站點(diǎn)的問題分析及解決方案
當(dāng)我們?cè)诎惭b好nginx準(zhǔn)備訪問80端口時(shí),突然出現(xiàn)您的請(qǐng)求在Web服務(wù)器中沒有找到對(duì)應(yīng)的站點(diǎn),所以本文給大家介紹了訪問nginx顯示未找到站點(diǎn)的問題分析及解決方案,需要的朋友可以參考下2024-03-03
Nginx服務(wù)器的反向代理proxy_pass配置方法講解
這篇文章主要介紹了Nginx服務(wù)器的反向代理proxy_pass配置方法講解,包括經(jīng)常被提到的url的/問題的相關(guān)說明,需要的朋友可以參考下2016-01-01
nginx啟動(dòng)后,訪問報(bào)403錯(cuò)誤問題及解決
Nginx出現(xiàn)Permission denied錯(cuò)誤可能由權(quán)限或文件路徑問題導(dǎo)致,檢查日志發(fā)現(xiàn)啟動(dòng)用戶為root,需修改nginx.conf的user指令與啟動(dòng)用戶一致,并重啟服務(wù),同時(shí)需確認(rèn)文件路徑是否正確2025-09-09
Nginx配置多個(gè)端口進(jìn)行監(jiān)聽的實(shí)現(xiàn)
隨著容器的應(yīng)用越來越多,將nginx部署在容器中也是常有之事,本文主要介紹了Nginx配置多個(gè)端口進(jìn)行監(jiān)聽的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07

