OpenResty + Lua 動態增加 Zuul 節點
阿新 • • 發佈:2018-11-04
文章目錄
隱憂
在 Spring Cloud 微服務架構體系中,所有請求的前門的閘道器 Zuul 承擔著請求轉發的主要功能,對後端服務起著舉足輕重的作用。當業務體量猛增之後得益於 Spring Cloud 的橫向擴充套件能力,往往加節點、加機器就可以使得系統支撐性獲得大大提升,但是僅僅加服務而不加閘道器是會有效能瓶頸的,實踐經驗得出的結論是單一 Zuul 的處理能力十分有限,因此擴張節點往往是服務連帶 Zuul 一起擴張,然後再在請求上層加一層軟負載,通常是使用 Nginx(Nginx 均分請求到 Zuul 負載層,“完美”地解決了問題),如下圖:
上圖的這種方式在實際執行中存在兩個缺點:
- 在後端 Zuul 執行一段時間之後,其中一臺 Zuul 掛掉了,前端請求的服務會有一部分掛掉。原因很簡單,Nginx 和後端 Zuul 沒有關聯性,Zuul 宕掉以後,Nginx 還是會把請求分發過來。
- 新加機器、對 Zuul 做新的橫向擴充套件,就需要去更改 Nginx 的配置,對新擴充套件的 Zuul 進行配置。
- 解決方案:
OpenResty
整合了 Nginx 與 Lua,實現了可伸縮的 Web 平臺,內部集成了大量精良的 Luα 庫、第三方模組以及多數的依賴項。能夠非常快捷地搭建處理超高併發、擴充套件性極高的動態 Web 應用、Web 服務和動態閘道器。我們可以使用 Luα 指令碼模組與註冊中心構建一個服務動態增減的機制,通過 Lua 獲取註冊中心狀態為 UP
實踐
Spring Cloud 中國社群針對上面說的這種場景開源了相關的 Lua 外掛原始碼(Github 地址)
OpenResty 安裝與配置
1、環境 yum -y install readline-devel pcre-devel openssl-devel gcc 2、下載解壓OpenResty包 wget https://openresty.org/download/openresty-1.13.6.1.tar.gz tar -zxvf openresty-1.13.6.1.tar.gz 3、下載ngx_cache_purge模組,該模組用於清理nginx快取 cd openresty-1.13.6.1/bundle wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz tar -zxvf ngx_cache_purge-2.3.tar.gz 4、下載nginx_upstream_check_module模組,該模組用於upstream健康檢查 wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz tar -zxvf v0.3.0.tar.gz 5、OpenResty配置增加 cd openresty-1.13.6.1 ./configure --prefix=/usr/local/openresty --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2 6、編譯安裝 make make install 7、OpenResty沒有http模組,需要單獨安裝 cd /usr/local/openresty/lualib/resty wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua 8、專案指令碼拷貝到這裡 copy dynamic_eureka_balancer.lua into this dir 9、Nginx配置檔案 vim /usr/local/openresty/nginx/conf/nginx.conf
Nginx 配置如下
http {
#sharing cache area
lua_shared_dict dynamic_eureka_balancer 128m;
init_worker_by_lua_block {
-- 引入 Lua 外掛檔案
local file = require "resty.dynamic_eureka_balancer"
local balancer = file:new({dict_name="dynamic_eureka_balancer"})
-- Eureka server list
balancer.set_eureka_service_url({"127.0.0.1:8888", "127.0.0.1:9999"})
-- The service name that needs to be monitored
balancer.watch_service({"zuul", "client"})
}
upstream springcloud_cn {
server 127.0.0.1:666; # Required, because empty upstream block is rejected by nginx (nginx+ can use 'zone' instead)
balancer_by_lua_block {
--The zuul name that needs to be monitored
local service_name = "zuul"
local file = require "resty.dynamic_eureka_balancer"
local balancer = file:new({dict_name="dynamic_eureka_balancer"})
--balancer.ip_hash(service_name) --IP Hash LB
balancer.round_robin(service_name) --Round Robin LB
}
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://springcloud_cn/;
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_set_header X-Forwarded-Proto $scheme;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
實現原理是使用 Lua 指令碼定時根據配置的服務名與 Eureka 地址,去拉取該服務的資訊,在 Eureka 裡面提供 /eureka/apps/{serviceId}
端點,返回服務的註冊資訊,所以我們只需要取用狀態為 UP
的服務,將它的地址加入 Nginx 負載列表即可。此專案使得 Nginx 與 Zuul 之間擁有一個動態感知能力,不用手動配置 Nginx 負載與 Zuul 負載,這樣對於應用彈性擴充套件是極其友好的。