一、架构

环境:

192.168.189.131:tomcat服务

192.168.189.132:tomcat服务

192.168.189.130:OpenResty服务、redis服务

流程:

请求到达openresty,openresty从redis获取白名单,然后判断请求地址是否再白名单,在白名单转到192.168.189.132服务否则转到192.168.189.131服务

在redis中动态设置白名单,实现服务切换

二、配置(openresty、redis、tomcat安装忽略)

1、在openresty根目录创建目录gray(作为工作空间),在gray目录创建conf(存放nginx配置文件nginx.conf)、logs(存放日志文件)、lua(存放lua脚本)

2、配置nginx.conf

user root;
worker_processes 1;
error_log logs/error.log; events {
worker_connections 1024;
} http {
#添加;;标识默认路径下的lualib
lua_package_path "$prefix/lualib/?.lua;;";
lua_package_cpath "$prefix/lualib/?.so;;"; upstream prod1 {
server 192.168.189.131:8080;
} upstream prod2 {
server 192.168.189.132:8080;
} server {
listen 80;
server_name localhost;
location / {
#为每个请求执行gray.lua脚本
content_by_lua_file lua/gray.lua;
}
location @prod1 {
proxy_pass http://prod1;
}
location @prod2 {
proxy_pass http://prod2;
} } }

3、配置gray.lua

local redis=require "resty.redis";

local red=redis:new();

red:set_timeout(1000);
--redis连接
local ok,err=red:connect("192.168.189.130", 6379); if not ok then
ngx.say("failed to connect redis ",err);
return;
end
--获取请求ip
local local_ip = ngx.req.get_headers()["X-Real-IP"];
if local_ip == nil then
local_ip = ngx.req.get_headers()["x_forwarded_for"];
end
if local_ip == nil then
local_ip = ngx.var.remote_addr;
end local_ip=ngx.var.remote_addr;

--redis中获取白名单
local ip_lists=red:get("gray");

--判断是否在白名单然后转到对应服务
if string.find(ip_lists,local_ip) == nil then
ngx.exec("@prod1");
else
ngx.exec("@prod2");
end local ok,err=red:close();

注意:

redis配置注释掉bind 127.0.0.1、设置protected-mode 为no;否则通过lua连接redis出错

#bind 127.0.0.1

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
# "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no

4、启动openresty

在openresty/nginx/sbin执行:./nginx -p /root/data/program/openresty/gray  (-p表示指定空间)

5、演示效果:

访问192.168.189.131服务:

访问192.168.189.132服务:

redis中白名单gray:

请求地址192.168.189.130不在白名单,因此lua脚本执行@prod1,对应server 192.168.189.131:8080

redis设置白名单gray:

请求地址192.168.189.130在白名单,lua脚本执行@prod2,对应server 192.168.189.132:8080

Openresty+redis实现灰度发布的更多相关文章

  1. nginx+lua+redis实现灰度发布_test

    nginx+lua+redis实现灰度发布: 灰度发布是指在黑白之间能够平滑过渡的一种方式 AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见, ...

  2. Openresty+Lua+Redis灰度发布

    灰度发布,简单来说,就是根据各种条件,让一部分用户使用旧版本,另一部分用户使用新版本.百度百科中解释:灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式.AB test就是一种灰度发布方式,让一部分 ...

  3. 01 . OpenResty简介部署,优缺点,压测,适用场景及用Lua实现服务灰度发布

    简介 OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高的动态 ...

  4. Nginx配之负载均衡、缓存、黑名单和灰度发布

    一.Nginx安装(基于CentOS 6.5) 1.yum命令安装 yum install nginx –y(若不能安装,执行命令yum install epel-release) 2. 启动.停止和 ...

  5. Nginx配置之负载均衡、限流、缓存、黑名单和灰度发布

    一.Nginx安装(基于CentOS 6.5) 1.yum命令安装 yum install nginx –y(若不能安装,执行命令yum install epel-release) 2. 启动.停止和 ...

  6. 【Nginx】实现负载均衡、限流、缓存、黑白名单和灰度发布,这是最全的一篇了!

    写在前面 在<[高并发]面试官问我如何使用Nginx实现限流,我如此回答轻松拿到了Offer!>一文中,我们主要介绍了如何使用Nginx进行限流,以避免系统被大流量压垮.除此之外,Ngin ...

  7. 使用Nginx实现灰度发布

    灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式.AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B ...

  8. springcloud灰度发布实现方案

    Nepxion Discovery是一款对Spring Cloud Discovery服务注册发现.Ribbon负载均衡.Feign和RestTemplate调用.Hystrix或者阿里巴巴Senti ...

  9. 使用Nginx实现灰度发布(转)

    灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式.AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B ...

随机推荐

  1. windows、ubuntu、centos7下mysql 的安装与使用

    一.windows 及ubuntu下安装 windows可以傻瓜式安装,另一种空闲了下来写,也不麻烦 ubuntu: apt-get install mysql 强烈推荐使用ubuntu从这儿就很方便 ...

  2. Maven的课堂笔记3

    8 仓库管理 仓库可以分为三种:1.本地仓库(本机).2.私服(公司局域网内的maven服务器).3.中央仓库(互联上,例如 struts2官网,或者hibernate官网) 可以根据maven坐标定 ...

  3. Spring事务银行转账示例

    https://www.imooc.com/video/9331 声明式事务 @Transactiona() 编程式事务 非模板式(不使用TransactionTemplate) http://cai ...

  4. React 组件

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  5. String()与toString的区别

    1..toString()可以将所有的的数据都转换为字符串,但是要排除null 和 undefined 代码示例: var a = null.toString()--报错 var b = underf ...

  6. Ionic package Error: Registry returned 404 for GET on

    Ionic package Error: Registry returned 404 for GET on https://registry.npmjs.org/org.apache.cordova. ...

  7. __slots__用法以及优化

    其实也是无意之中又看到这个东西,这次索性再记一下,免得下次忘记又再看一遍,往复循环浪费了太多时间. __slots__其实我做项目这么久还没有主动使用过.下面reference有提到这么一句话 War ...

  8. ElasticSearch 6.x head插件安装

    一.下载node.js yum install -y nodejs 二.安装npm npm install -g cnpm --registry=https://registry.npm.taobao ...

  9. python自动化运维笔记1 —— 系统性能信息模块psutil

    一.系统基础信息模块 1.1 系统性能信息模块psutil psutil是一个跨平台库(http://code.google.com/p/psutil/),能够轻松实现获取系统运行的进程和系统利用率( ...

  10. Delphi2007精简版加载Borland.Studio.Together.dll错误解决办法

    安装Delphi2007精简版,启动提示Borland.Studio.Together.dll加载错误,错误信息如下: Failed to load IDE add in 'C:\Program Fi ...