之前簡單寫過緩存預熱加上二級緩存,感覺還挺好玩的,在b站看到nginx的視頻,也調用lua的模塊,做了灰度發布,自己做了幾個小時,結果最后失敗了,可能是虛擬機的原因,nginx那台經常登不上,下面就是它的主要流程。
Nginx調用Lua指令
Nginx
調用Lua
模塊指令, Nginx的可插拔模塊加載執行, 共11個處理階段
語法 | |
---|---|
set_by_lua set_by_lua_file | 設置Nginx變量,可以實現負載的賦值邏輯 |
access_by_lua access_by_lua_file | 請求訪問階段處理, 用於訪問控制 |
content_by_lua content_by_lua_file | 內容處理器, 接受請求處理並輸出響應 |
變量 | |
---|---|
ngx.var | nginx變量 |
ngx.req.get_headers | 獲取請求頭 |
ngx.req.get_uri_args | 獲取url請求參數 |
ngx.redirect | 重定向 |
ngx.print | 輸出響應內容體 |
ngx.say | 輸出響應內容體,最后輸出一個換行符 |
ngx.header | 輸出響應頭 |
Nginx+Lua實現代碼灰度發布
使用Nginx
結合lua
實現代碼灰度發布
按照一定的關系區別,分不分的代碼進行上線,使代碼的發布能平滑過渡上線
1.用戶的信息cookie等信息區別
2.根據用戶的ip地址, 顆粒度更廣
實踐架構圖
執行過程:
- 1.用戶請求到達前端代理Nginx, 內嵌的lua模塊會解析Nginx配置文件中Lua腳本
- 2.Lua腳本會獲取客戶端IP地址,查看Memcached緩存中是否存在該鍵值
- 3.如果存在則執行@java_test,否則執行@java_prod
- 4.如果是@java_test, 那么location會將請求轉發至新版代碼的集群組
- 5.如果是@java_prod, 那么location會將請求轉發至原始版代碼集群組
- 6.最后整個過程執行后結束
實踐環境准備:
系統 | 服務 | 地址 |
---|---|---|
CentOS7 | Nginx+Lua+Memached | 192.168.1.109 |
CentOS7 | Tomcat集群8080_Prod | 192.168.1.145 |
CentOS7 | Tomcat集群9090_Test | 192.168.1.144 |
1.安裝兩台服務器Tomcat
,分別啟動8080
和9090
端口
[root@tomcat-node1-20 ~]# yum install java -y
[root@tomcat-node1-20 ~]# mkdir /soft/src -p
[root@tomcat-node1-20 ~]# cd /soft/src
[root@nginx ~]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.7/bin/apache-tomcat-9.0.7.tar.gz
[root@tomcat-node1-20 src]# tar xf apache-tomcat-9.0.7.tar.gz -C /soft
[root@tomcat-node1-20 soft]# cp -r apache-tomcat-9.0.7/ tomcat-8080
[root@tomcat-node1-20 bin]# /soft/tomcat-8080/bin/startup.sh
//注意tomcat默認監聽在8080端口, 如果需要啟動9090端口需要修改server.xml配置文件
2.配置Memcached
並讓其支持Lua
調用
//安裝memcached服務
[root@Nginx-Lua ~]# yum install memcached -y
//配置memcached支持lua
[root@Nginx-Lua ~]# cd /soft/src
[root@Nginx-Lua ~]# wget https://github.com/agentzh/lua-resty-memcached/archive/v0.11.tar.gz
[root@Nginx-Lua ~]# tar xf v0.11.tar.gz
[root@Nginx-Lua ~]# cp -r lua-resty-memcached-0.11/lib/resty/memcached.lua /etc/nginx/lua/
//啟動memcached
[root@Nginx-Lua ~]# systemctl start memcached
[root@Nginx-Lua ~]# systemctl enable memcached
3.配置負載均衡調度
#必須在http層
lua_package_path "/etc/nginx/lua/memcached.lua";
upstream java_prod {
server 192.168.1.145:8080;
}
upstream java_test {
server 192.168.1.144:9090;
}
server {
listen 80;
server_name 192.168.1.109;
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello ,lua scripts")';
}
location /myip {
default_type 'text/plain';
content_by_lua '
clientIP = ngx.req.get_headers()["x_forwarded_for"]
ngx.say("Forwarded_IP:",clientIP)
if clientIP == nli then
clientIP = ngx.var.remote_addr
ngx.say("Remote_IP:",clientIP)
end
';
}
location / {
default_type 'text/plain';
content_by_lua_file /etc/nginx/lua/dep.lua;
}
location @java_prod {
proxy_pass http://java_prod;
include proxy_params;
}
location @java_test {
proxy_pass http://java_test;
include proxy_params;
}
}
//nginx反向代理tomcat,必須配置頭部信息否則返回400錯誤
[root@nginx-lua conf.d]# cat ../proxy_params
proxy_redirect default;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 32k;
proxy_buffering on;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;
4.編寫Nginx
調用灰度發布Lua
腳本
[root@nginx ~]# cat /etc/nginx/lua/dep.lua
--獲取x-real-ip
clientIP = ngx.req.get_headers()["X-Real-IP"]
--如果IP為空-取x_forwarded_for
if clientIP == nil then
clientIP = ngx.req.get_headers()["x_forwarded_for"]
end
--如果IP為空-取remote_addr
if clientIP == nil then
clientIP = ngx.var.remote_addr
end
--定義本地,加載memcached
local memcached = require "resty.memcached"
--實例化對象
local memc, err = memcached:new()
--判斷連接是否存在錯誤
if not memc then
ngx.say("failed to instantiate memc: ", err)
return
end
--建立memcache連接
local ok, err = memc:connect("127.0.0.1", 11211)
--無法連接往前端拋出錯誤信息
if not ok then
ngx.say("failed to connect: ", err)
return
end
--獲取對象中的ip-存在值賦給res
local res, flags, err = memc:get(clientIP)
--
--ngx.say("value key: ",res,clientIP)
if err then
ngx.say("failed to get clientIP ", err)
return
end
--如果值為1則調用local-@java_test
if res == "1" then
ngx.exec("@java_test")
return
end
--否則調用local-@java_prod
ngx.exec("@java_prod")
return
5.使用Memcache set IP
, 測試灰度發布
//telnet傳入值
[root@nginx conf.d]# telnet 127.0.0.1 11211
# set對應IP
set 211.161.160.201 0 0 1
# 輸入1
1