Mac下使用ABTestingGateway快速搭建灰度網關

Mac下使用ABTestingGateway快速搭建灰度網關

ABTestingGateway簡介

ABTestingGateway 是新浪開源的一個能夠動態設置分流策略的灰度發佈系統,工做在7層,基於nginx和ngx-lua開發,使用 redis 做爲分流策略數據庫,能夠實現動態調度功能。

nginx是目前使用較多的7層服務器,能夠實現高性能的轉發和響應;ABTestingGateway 是在 nginx 轉發的框架內,在轉向 upstream 前,根據 用戶請求特徵 和 系統的分流策略 ,查找出目標upstream,進而實現分流。nginx

環境搭建

1.安裝openresty

其是由Nginx核心加不少第三方模塊組成,其最大的亮點是默認集成了Lua開發環境,使得Nginx能夠做爲一個Web Server使用。藉助於Nginx的事件驅動模型和非阻塞IO,能夠實現高性能的Web應用程序。並且OpenResty提供了大量組件如Mysql、Redis、Memcached等等,使在Nginx上開發Web應用更方便更簡單。目前在京東如實時價格、秒殺、動態服務、單品頁、列表頁等都在使用Nginx+Lua架構,其餘公司如淘寶、去哪兒網等。

安裝命令:brew install openrestygit

brew默認將openresty安裝在/usr/local/Cellar/openresty/目錄下github

2.安裝luajit

JIT = Just In Time即時編譯,是動態編譯的一種形式,是一種優化虛擬機運行的技術。
程序運行一般有兩種方式,一種是靜態編譯,一種是動態解釋,即時編譯混合了這兩者。Java和.Net/mono中都使用了這種技術。

安裝命令:brew install LuaJITredis

brew默認將openresty安裝在/usr/local/Cellar/luajit/目錄下sql

3.安裝redis

安裝命令:brew install redis數據庫

啓動命令:brew services start redisjson

ABTestingGateway搭建

項目下載:

命令:git clone https://github.com/CNSRE/ABTestingGateway.gitapi

下載後目錄結果以下服務器

ABTestingGateway目錄結構

repo中的utils/conf文件夾中有灰度系統部署所需的最小示例架構

運行灰度網關

1. 進入ABTestingGateway目錄
2. 建立logs目錄,mkdir logs
3. 啓動redis
4. 
# 啓動upstream server,其中stable爲默認upstream
4. /usr/local/Cellar/openresty/1.13.6.2/nginx/sbin/nginx -p `pwd` -c conf/stable.conf
5. /usr/local/Cellar/openresty/1.13.6.2/nginx/sbin/nginx -p `pwd` -c conf/beta1.conf
6. /usr/local/Cellar/openresty/1.13.6.2/nginx/sbin/nginx -p `pwd` -c conf/beta2.conf
7. /usr/local/Cellar/openresty/1.13.6.2/nginx/sbin/nginx -p `pwd` -c conf/beta3.conf
8. /usr/local/Cellar/openresty/1.13.6.2/nginx/sbin/nginx -p `pwd` -c conf/beta4.conf

# 啓動灰度系統,proxy server,灰度系統的配置也寫在conf/nginx.conf中
9. /usr/local/Cellar/openresty/1.13.6.2/nginx/sbin/nginx -p `pwd` -c conf/nginx.conf

簡單驗證:添加分流策略組

$ curl 127.0.0.1:8080/ab_admin?action=policygroup_set -d '{"1":{"divtype":"uidsuffix","divdata":[{"suffix":"1","upstream":"beta1"},{"suffix":"3","upstream":"beta2"},{"suffix":"5","upstream":"beta1"},{"suffix":"0","upstream":"beta3"}]},"2":{"divtype":"arg_city","divdata":[{"city":"BJ","upstream":"beta1"},{"city":"SH","upstream":"beta2"},{"city":"XA","upstream":"beta1"},{"city":"HZ","upstream":"beta3"}]},"3":{"divtype":"iprange","divdata":[{"range":{"start":1111,"end":2222},"upstream":"beta1"},{"range":{"start":3333,"end":4444},"upstream":"beta2"},{"range":{"start":7777,"end":2130706433},"upstream":"beta2"}]}}'

{"desc":"success ","code":200,"data":{"groupid":0,"group":[0,1,2]}}

分流規則簡介

  • ABTestingGateway灰度系統目前支持的策略有ip段分流、用戶uid段分流、uid尾數分流、uid白名單分流
  • 優先級由數字表示,從1開始,級別爲1的策略優先級最高

如下共添加了3個分流規則,分別是用戶uid尾數分流、城市分流、ip段分流,具體請看json中的註釋
stable服務爲默認的,未匹配到規則則分流到stable服務器上

{
    "1": {
        "divtype": "uidsuffix", 
        "divdata": [
            {
                "suffix": "1",    //http請求中header X-Uid尾數爲1分流到beta1服務器
                "upstream": "beta1"
            }, 
            {
                "suffix": "3",    //http請求中header X-Uid尾數爲3分流到beta2服務器
                "upstream": "beta2"
            }, 
            {
                "suffix": "5", 
                "upstream": "beta1"
            }, 
            {
                "suffix": "0", 
                "upstream": "beta3"
            }
        ]
    }, 
    "2": {
        "divtype": "arg_city", 
        "divdata": [
            {
                "city": "BJ",      // url上參數city=BJ,分流到beta1
                "upstream": "beta1"
            }, 
            {
                "city": "SH", 
                "upstream": "beta2"
            }, 
            {
                "city": "XA", 
                "upstream": "beta1"
            }, 
            {
                "city": "HZ", 
                "upstream": "beta3"
            }
        ]
    }, 
    "3": {
        "divtype": "iprange", 
        "divdata": [
            {
                "range": {
                    "start": 1111, 
                    "end": 2222
                }, 
                "upstream": "beta1"
            }, 
            {
                "range": {
                    "start": 3333, 
                    "end": 4444
                }, 
                "upstream": "beta2"
            }, 
            {
                "range": {
                    "start": 7777, 
                    "end": 2130706433
                }, 
                "upstream": "beta2"
            }
        ]
    }
}

簡單驗證:設置運行時策略

$ curl "127.0.0.1:8080/ab_admin?action=runtime_set&hostname=api.weibo.cn&policygroupid=0"

# 分流
$ curl 127.0.0.1:8030 -H 'X-Uid:39' -H 'X-Real-IP:192.168.1.1'
this is stable server

$ curl 127.0.0.1:8030 -H 'X-Uid:30' -H 'X-Real-IP:192.168.1.1'
this is beta3 server

$ curl 127.0.0.1:8030/?city=BJ -H 'X-Uid:39' -H 'X-Real-IP:192.168.1.1'
this is beta1 server
注意

報錯:

2018/07/11 22:00:39 [error] 65912#7287282: *34 lua entry thread aborted: runtime error: ../lib/lua-resty-core/lib/ngx/semaphore.lua:64: dlsym(RTLD_DEFAULT, ngx_http_lua_ffi_semaphore_new): symbol not found
stack traceback:
coroutine 0:
    [C]: in function 'require'
    .../Nginx/ABTestingGateway/utils/../diversion/diversion.lua:11: in function <.../Nginx/ABTestingGateway/utils/../diversion/diversion.lua:1>, client: 127.0.0.1, server: api.weibo.cn, request: "GET / HTTP/1.1", host: "127.0.0.1:8030"

緣由:

brew安裝的openresty版本與ABTestingGateway不一致致使的,將/usr/local/Cellar/openresty/1.13.6.2/lualib (1.13.6.2替換爲您安裝的版本)下的相關的lua文件拷貝覆蓋ABTestingGateway/lualib下的文件,重啓nginx便可

相關文章
相關標籤/搜索