## 一、功能介绍
1. 基于openresty的灰度发布
2. 服务平滑切换
### 二、目录介绍
1. admin: 灰度策略管理源码, 无需修改
2. diversion: 灰度流量分发源码, 无需修改
3. doc: ABTestingGateWay自带的文档
4. lib/resty & lib/lua-resty-core: openresty的lib库,无需修改
5. lib/abtesting/adapter: 灰度策略应用源码, 无需修改
6. lib/diversion & lib/userinfo: 灰度流量分发的应用源码,这里需要改动
目前支持按city, ip, uid, url进行流量分发
比如如果要按uid进行流量分发,那么就改动 lib/abtesting/userinfo中的uidParser.lua,
```bash
local _M = {
_VERSION = '0.01'
}
_M.get = function()
local u = ngx.req.get_headers()["uid"]
return u
end
return _M
```
其中, uid为请求头中用户id的字段。如果请求头中用的是user_id,那么把uid换成user_id即可。
一般情况下,我们都是根据uid进行流量分发,只需改动这里即可。
7. maker/openresty: 预置的openresty安装包,后续安装时用到,见下文openresty环境部署
8. utils: ABTestingGateway自带的部署示例,参考 doc/README.md文档
9. xx_utils: 基于utils结合实际工程项目的一个部署示例,里面除了灰度部署,还增加了服务平滑切换脚本及各种辅助脚本。
我们直接把xx改成我们的项目代号,稍微改动里面的配置,就可以实现灰度部署和服务平滑切换两大功能了。
下面会详细讲解这个目录下的文件。
### 三、xx_utils目录详解
我们重点关注 xx_utils/conf xx_utils/logs 和xx_utils目录下的各种脚本文件,其他目录无需修改
1. xx_utils/conf: 该目录是openresty_nginx配置目录, 即nginx配置文件全在这个目录下。
1.1 nginx.conf
nginx的总入口文件, 这里 include了四个文件:
(1)default.conf: 这个主要是灰度策略管理接口的配置文件, 无需修改
(2)upstream.conf: 这个是灰度分发upstream配置文件, 需要修改, 下面会详解
(3)vhost.conf: 这个是灰度分发接口配置文件, 需要修改, 下面会详解
(4)console.*.com.conf: 这个是无需参与灰度分发的普通nginx配置文件,可任意配置多个
1.2 vhost.conf
灰度分发接口配置文件
server已配置好了, 根据项目不同这里只需要修改三个地方:
(1)sever_name即域名 (2)location(即要进行流量分发的接口) (3)证书文件目录,参考后面"certbot配置免费证书"
比如前面提到的按uid进行流量分发,如果请求头中的uid未配置在灰度服中,则请求会走api.conf;
否则会走对应的灰度服配置文件, 比如 api_master.conf或者api_master_backup.conf,
具体走哪个灰度服,取决于灰度策略的命令配置,这个下节会详细讲解。
***这里注意还要起一个6380端口的redis进程, 把conf/redis6380.conf拷贝为/etc/redis6380.conf,
执行 redis-server redis6380.conf 即可。
1.3 upstream.conf
灰度分发upstream配置文件
这个文件已配置好,建议大家可以不用修改, 只是配置了upstream, 如果不够,可以再加,建议按端口号递增
1.4 api.conf
请求未命中灰度服时,会默认走这个配置文件, 这个文件内部又include了 conf.d/*.conf。
其实conf.d下只有一个.conf文件,但是有多个.conf.bak文件,这里主要是配合switch-api-server脚本进行服务平滑切换时用,
如果切换到master, 那么就会用 xx.conf.bak替换xx.conf; 如果切换到slave,那么就会切换到xx.conf.backup.bak切换xx.conf。
如何利用switch-api-server进行服务切换,后面会讲解。
1.5 api_master.conf/api_master_backup.conf
请求命中灰度服时走的配置文件, 具体走哪个配置文件, 取决于灰度策略的命令配置,这个下节会详细讲解。
2. start-all-nginx.sh: openrest_nginx启动脚本
stop-all-nginx.sh: openresty_nginx停止脚本
restart-all-nginx.sh: openresty_nginx重启脚本
3. switch2-nginx.sh: 从openresty_nginx(带灰度发布功能)切换为系统自带的nginx(无灰度发布功能)
switch2-openresty.sh: 从系统自带的nginx(无灰度发布功能)切换为openresty_nginx(带灰度发布功能)
renew-certbot.sh: 延长cetbot https免费证书有效期脚本
cron-renew-certbot.sh: 延长cetbot https免费证书有效期的定时任务脚本
4. switch-api-server.sh: 服务平滑切换脚本, 用法如下:
4.1 switch-api-server.sh query : 查询当前正式服是master还是slave, 脚本执行结果为master 或者 slave
4.2 switch-api-server.sh master: 切换 master 为正式服
switch-api-server.sh slave: 切换 slave 为正式服
切换原理参考1.4
4.3 record-switch.txt: 服务切换记录文件
### 四、安装部署
##1. 拷贝整个ABTestingGateway目录到服务器上某个目录下
##2. openresty环境部署
ubuntu环境下的安装:
2.1. apt-get install libpcre3-dev libssl-dev perl make build-essential curl
2.2. tar -xvf openresty-1.15.8.tar (make/openresty目录下)
2.3 安装zlib: `sudo apt-get install zlib1g-dev`
2.4. cd openresty-1.15.8, 然后 ./configure
./configure默认, --prefix=/usr/local/openresty 程序会被安装到/usr/local/openresty目录。
您可以指定各种选项,比如
`
./configure --prefix=/usr/local/openresty \
--with-luajit \
--without-http_redis2_module \
--with-http_iconv_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_stub_status_module
--with-http_postgres_module
`
2.5. make & make install
2.6. apt-get install lua5.2 lua5.2-doc liblua5.2-dev
2.7. apt-get install luajit
2.8. apt-get install libnginx-mod-http-lua
2.9. apt-get install lua-cjson
到此,openresty环境安装成功
##3. 修改xx_utils/conf下的配置文件和xx_utils下的swicth_api_server.sh脚本
##4. 运行命令 sudo sh restart-all-nginx.sh, 开启 openresty_nginx
##5. 灰度策略管理命令:
5.1 下发灰度策略,将指定uid加入到指定灰度服中
curl 127.0.0.1:8080/ab_admin?action=policygroup_set -d '{"1":{"divtype":"uidappoint","divdata":[{"uidset":[5809, 128, 123],"upstream":"api_master_backup"}]}}'
说明: uidset中为uid列表, upstream为upstream.conf中定义的某一个
5.2 将5.1的灰度策略生效
curl "127.0.0.1:8080/ab_admin?action=runtime_set&hostname=api.****.com&policygroupid=3"
说明: policygroupid为5.1命令响应中的policygroupid
5.3 删除灰度策略
curl "127.0.0.1:8080/ab_admin?action=policygroup_del&policygroupid=2"
5.4 去使能已生效的灰度策略
curl "127.0.0.1:8080/ab_admin?action=runtime_del&hostname=api.****.com"
相关资源会以文件的形式上传