NGINX 实现jar包灰度发布
1.目标
实现jar包在服务器上部署不影响用户使用
2.思路
-
部署多个服务,通过NGINX进行负载均衡,避免服务不可用,但任存在服务调用会出现成功/失败的切换
解决方案:通过心跳检测,尽可能实时的观察服务状态,如果服务不可用(在重新部署或其他故障),自动进行故障转移,将请求分发到可用服务上
-
Java热部署
本质通过运行时动态加载新的class文件实现,但实现困难,没有完整的支持java -jar的部署方式,并且生产环境基本不会用热部署,只有在debug/调试时用的多
3.方案
使用淘宝旗下Tengine的ngx_http_upstream_check_module 模块来增加NGINX的功能实现心跳检测
操作流程
-
通过rz命令上传zip文件
-
解压
[root@node-1 ~]# unzip nginx_upstream_check_module-master\ \(1\).zip
-
打上模块补丁,重新编译nginx
-
切换到nginx 未编译前文件的目录
-
打模块补丁
patch -p1 < /root/nginx_upstream_check_module-master/check_1.12.1+.patch
-
添加心跳检测模块
./configure --add-module=/root/nginx_upstream_check_module-master
-
重新编译
make
-
备份旧文件
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
-
使用新文件
cp ./objs/nginx /usr/local/nginx/sbin/
-
-
nginx配置
http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; upstream haTest { server 127.0.0.1:9001; server 127.0.0.1:9002; check interval=3000 rise=2 fall=5 timeout=1000 type=http; check_keepalive_requests 1; check_http_send "GET /test HTTP/1.0\r\n\r\nHost: 127.0.0.1\r\n\r\n"; # 配置为服务只要不宕机,任何回复都认为存活 check_http_expect_alive http_2xx http_3xx http_4xx http_5xx; } #gzip on; server { listen 80; server_name 192.168.198.120; #charset koi8-r; #access_log logs/host.access.log main; location /api/ { proxy_redirect off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://haTest; } # 配置心跳检测的前端界面 location /status { check_status; access_log off; } } }
-
4.效果
5.小结
在部署时无感,dev部署所有请求打向ready,若出现fall counts 则 rise counts清空,fall counts超过一定限制status由 up改为down