1. 程式人生 > >灰度釋出常見一般有三種方式

灰度釋出常見一般有三種方式

使用Nginx實現灰度釋出

灰度釋出是指在黑與白之間,能夠平滑過渡的一種釋出方式。AB test就是一種灰度釋出方式,讓一部分使用者繼續用A,一部分使用者開始用B,如果使用者對B沒有什麼反對意見,那麼逐步擴大範圍,把所有使用者都遷移到B上面來。

灰度釋出可以保證整體系統的穩定,在初始灰度的時候就可以發現、調整問題,以保證其影響度。

灰度釋出常見一般有三種方式:

  • Nginx+LUA方式

  • 根據Cookie實現灰度釋出

  • 根據來路IP實現灰度釋出

本文主要將講解根據Cookie和來路IP這兩種方式實現簡單的灰度釋出,Nginx+LUA這種方式涉及內容太多就不再本文展開了。

 

A/B測試流程


 




Nginx根據Cookie實現灰度釋出
 

根據Cookie查詢Cookie鍵為version的值,如果該Cookie值為V1則轉發到hilinux_01,為V2則轉發到hilinux_02。Cookie值都不匹配的情況下預設走hilinux_01所對應的伺服器。

兩臺伺服器分別定義為:

 
  1. hilinux_01  192.168.1.100:8080

  2. hilinux_02  192.168.1.200:8080

  • 用if指令實現

 
  1. upstream hilinux_01 {

  2.    server 192.168.1.100:8080 max_fails=1 fail_timeout=60;

  3. }

  4.  
  5. upstream hilinux_02 {

  6.    server 192.168.1.200:8080 max_fails=1 fail_timeout=60;

  7. }

  8.  
  9. upstream default {

  10.    server 192.168.1.100:8080 max_fails=1 fail_timeout=60;

  11. }

  12.  
  13. server {

  14.  listen 80;

  15.  server_name  www.hi-linux.com;

  16.  access_log  logs/www.hi-linux.com.log  main;

  17.  
  18.  #match cookie

  19.  set $group "default";

  20.    if ($http_cookie ~* "version=V1"){

  21.        set $group hilinux_01;

  22.    }

  23.  
  24.    if ($http_cookie ~* "version=V2"){

  25.        set $group hilinux_02;

  26.    }

  27.  
  28.  location / {                      

  29.    proxy_pass http://$group;

  30.    proxy_set_header   Host             $host;

  31.    proxy_set_header   X-Real-IP        $remote_addr;

  32.    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

  33.    index  index.html index.htm;

  34.  }

  35. }

  • 用map指令實現

在Nginx裡面配置一個對映,$COOKIE_version可以解析出Cookie裡面的version欄位。$group是一個變數,{}裡面是對映規則。

如果一個version為V1的使用者來訪問,$group就等於hilinux_01。在server裡面使用就會代理到http://hilinux_01上。version為V2的使用者來訪問,$group就等於hilinux_02。在server裡面使用就會代理到http://hilinux_02上。Cookie值都不匹配的情況下預設走hilinux_01所對應的伺服器。

 
  1. upstream hilinux_01 {

  2.    server 192.168.1.100:8080 max_fails=1 fail_timeout=60;

  3. }

  4.  
  5. upstream hilinux_02 {

  6.    server 192.168.1.200:8080 max_fails=1 fail_timeout=60;

  7. }

  8.  
  9. upstream default {

  10.    server 192.168.1.100:8080 max_fails=1 fail_timeout=60;

  11. }

  12.  
  13. map $COOKIE_version $group {

  14. ~*V1$ hilinux_01;

  15. ~*V2$ hilinux_02;

  16. default default;

  17. }

  18.  
  19. server {

  20.  listen 80;

  21.  server_name  www.hi-linux.com;

  22.  access_log  logs/www.hi-linux.com.log  main;

  23.  
  24.  location / {                      

  25.    proxy_pass http://$group;

  26.    proxy_set_header   Host             $host;

  27.    proxy_set_header   X-Real-IP        $remote_addr;

  28.    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

  29.    index  index.html index.htm;

  30.  }

  31. }

Nginx根據來路IP實現灰度釋出

如果是內部IP,則反向代理到hilinux_02(預釋出環境);如果不是則反向代理到hilinux_01(生產環境)。

 
  1. upstream hilinux_01 {

  2.    server 192.168.1.100:8080 max_fails=1 fail_timeout=60;

  3. }

  4.  
  5. upstream hilinux_02 {

  6.    server 192.168.1.200:8080 max_fails=1 fail_timeout=60;

  7. }

  8.  
  9. upstream default {

  10.    server 192.168.1.100:8080 max_fails=1 fail_timeout=60;

  11. }

  12.  
  13. server {

  14.  listen 80;

  15.  server_name  www.hi-linux.com;

  16.  access_log  logs/www.hi-linux.com.log  main;

  17.  
  18.  set $group default;

  19.  if ($remote_addr ~ "211.118.119.11") {

  20.      set $group hilinux_02;

  21.  }

  22.  
  23. location / {                      

  24.    proxy_pass http://$group;

  25.    proxy_set_header   Host             $host;

  26.    proxy_set_header   X-Real-IP        $remote_addr;

  27.    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

  28.    index  index.html index.htm;

  29.  }

  30. }

如果你只有單臺伺服器,可以根據不同的IP設定不同的網站根目錄來達到相同的目的。

 
  1. server {

  2.  listen 80;

  3.  server_name  www.hi-linux.com;

  4.  access_log  logs/www.hi-linux.com.log  main;

  5.  
  6.  set $rootdir "/var/www/html";

  7.    if ($remote_addr ~ "211.118.119.11") {

  8.       set $rootdir "/var/www/test";

  9.    }

  10.  
  11.    location / {

  12.      root $rootdir;

  13.    }

  14. }

到此最基本的實現灰度釋出方法就講解完了,如果要做更細粒度灰度釋出可參考ABTestingGateway專案。

ABTestingGateway是新浪開源的一個動態路由系統。ABTestingGateway是一個可以動態設定分流策略的灰度釋出系統,工作在7層,基於nginx和ngx-lua開發,使用redis作為分流策略資料庫,可以實現動態排程功能。

ABTestingGateway:https://github.com/CNSRE/ABTestingGateway