基於Lua的Spring Cloud網關高可用通用Ngnix插件

項目github地址:點擊跳轉


場景痛點

這裏寫圖片描述

在Spring Cloud微服務架構體系中,我們往往會部署一個Zuul集羣來橫向擴展我們的微服務應用,集羣的上層是Nginx軟負載,在實際情況中,往往會遇到Zuul宕機的尷尬事情,這時候從Nginx到這臺機器的請求就會全部失效。此項目針對此痛點,用lua腳本實現定時拉取特定服務地址,動態無感知增減Zuul在Nginx中的負載節點。

如果您希望實現從Nginx直接到普通服務的動態節點負載,在下文配置服務名與Eureka註冊中心地址即可。

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/servers --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/servers/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/servers/nginx/conf/nginx.conf

Nginx配置


http {
    #sharing cache area
    lua_shared_dict dynamic_eureka_balancer 128m;

    init_worker_by_lua_block {
        -- init eureka balancer
        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;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章