侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Nginx 实现灰度发布的三种方法总结

linux  /  管理员 发布于 7年前   247

Nginx 实现灰度发布的三种方法总结

灰度发布的主要原理是访问路由的控制,重点是保证每次访问的是同一个节点。

方式一:通过调节负载均衡权重

        负载均衡 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。

        负载均衡,英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。

简单配置如下:

http {   upstream cluster {     ip_hash; #如果你的系统中没有使用第三方缓存管理工具 ,建议使用此方式    server 192.168.1.210:80 weight=5;     server 192.168.1.211:80 weight=3;     server 192.168.1.212:80 weight=1;   }     server {     listen 80;    location / {     proxy_next_upstream   error timeout;  proxy_redirect     off;  proxy_set_header    Host $host;  #proxy_set_header    X-Real-IP $remote_addr;  proxy_set_header    X-Real-IP $http_x_forwarded_for;  proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;  client_max_body_size  100m;  client_body_buffer_size 256k;  proxy_connect_timeout  180;  proxy_send_timeout   180;  proxy_read_timeout   180;  proxy_buffer_size    8k;  proxy_buffers      8 64k;  proxy_busy_buffers_size 128k;  proxy_temp_file_write_size 128k;  proxy_pass http://cluster;     }   } } 

这种方式灰度发布通过weight来实现,但是这种方式只适合修改节点的行为,而且要求应用都是一模一样的,其实质作用是,节点增加或删除之后,对负载能力的调节,最终目的是为了让流量最终保持均衡。

方式二.使用nginx+lua实现web项目的灰度发布

location / { content_by_lua '      myIP = ngx.req.get_headers()["X-Real-IP"]      if myIP == nil then        myIP = ngx.req.get_headers()["x_forwarded_for"]      end      if myIP == nil then        myIP = ngx.var.remote_addr      end      if myIP == "公司出口IP" then        ngx.exec("@client")      else        ngx.exec("@client_test")      end    ';} location @client{  proxy_next_upstream   error timeout;  proxy_redirect     off;  proxy_set_header    Host $host;  #proxy_set_header    X-Real-IP $remote_addr;  proxy_set_header    X-Real-IP $http_x_forwarded_for;  proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;  client_max_body_size  100m;  client_body_buffer_size 256k;  proxy_connect_timeout  180;  proxy_send_timeout   180;  proxy_read_timeout   180;  proxy_buffer_size    8k;  proxy_buffers      8 64k;  proxy_busy_buffers_size 128k;  proxy_temp_file_write_size 128k;  proxy_pass http://client;}location @client_test{  proxy_next_upstream   error timeout;  proxy_redirect     off;  proxy_set_header    Host $host;  #proxy_set_header    X-Real-IP $remote_addr;  proxy_set_header    X-Real-IP $http_x_forwarded_for;  proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;  client_max_body_size  100m;  client_body_buffer_size 256k;  proxy_connect_timeout  180;  proxy_send_timeout   180;  proxy_read_timeout   180;  proxy_buffer_size    8k;  proxy_buffers      8 64k;  proxy_busy_buffers_size 128k;  proxy_temp_file_write_size 128k;  proxy_pass http://client_test;} 

由于使用了nginx+lua模块,这种方式适合很多场景,非常强大,但是问题是你可能需要学习很多lua的语法。

 方式三.使用http头信息判断+权重(灰度值)

http请求传输过程中,会自动带上User-Agent,Host,Referer,Cookie等信息。我们只需要判断ip地址段,用户代理,Cookie中的信息等。我们这里以Cookie为例。

当然,这里需要解决两个问题:

①首次访问静态页面可能不会产生cookie

②我们需要通过代码动态设置路由

③通过weight控制灰度值

我们可以通过一个例子来解决上述中的②与③的问题

upstream tts_V6 {    server 192.168.3.81:5280 max_fails=1 fail_timeout=60;}upstream tts_V7 {    server 192.168.3.81:5380 max_fails=1 fail_timeout=60;}upstream default {  #通过upstream default + weight节点控制权重    server 192.168.3.81:5280 max_fails=1 fail_timeout=60 weight=5;    server 192.168.3.81:5380 max_fails=1 fail_timeout=60 weight=1;}server {    listen 80;    server_name test.taotaosou.com;    access_log logs/test.taotaosou.com.log main buffer=32k;    #match cookie    set $group "default";    if ($http_cookie ~* "tts_version_id=tts1"){ #动态控制路由        set $group tts_V6;    }    if ($http_cookie ~* "tts_version_id=tts2"){        set $group tts_V7;    }    location / {        proxy_pass http://$group;        proxy_set_header  Host       $host;        proxy_set_header  X-Real-IP    $remote_addr;        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;        index index.html index.htm;    } }

对于问题①,我们可以在index页面通过script来访问动态页面:

如

<script src="https://test.taotaosou.com/cookieinfo.php" /><script>

此外,我们还要在cookieinfo.php中判断和生成cookie

<?phpif(!session_id()){ session_start();}if(!isset($_COOKIE["tts_version_id"])){ $cookieValue = $_SERVER['SERVER_PORT']==5280?"tts1":"tts2"; setcookie("tts_version_id", $cookieValue, time()+3600, "/");}?>

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


  • 上一条:
    Mybatis mapper动态代理的原理解析
    下一条:
    nginx源码分析configure脚本详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客