Dynamic Routing Based On Redis
Dynamic Routing Based On Redis
Ngnix技术研究系列2-基于Redis实现动态路由
上篇博文我们写了个引子:
Ngnix技术研究系列1-通过应用场景看Nginx的反向代理
发现了新大陆,OpenResty
OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
OpenResty 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。
OpenResty的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。
回到我们的原始需求:
http://api.***.com/action => http://192.168.0.11/api/action
Header: *** Header: ***
Body: *** Body: ***
通过Actiton获取对应的后端服务器地址
Action和服务器的对应关系(路由表)存储在Redis中.(实时更新实时获取)
根据请求的Action动态解析对应的内网服务器地址,再实现服务的转发。
Nginx原生的路由配置无法实现上述动态路由配置,因为我们要访问Redis,获取到反向代理的路由信息,再实现服务的转发。详细的实现步骤:
1. 解析URL中的Action
2.访问Redis,Key=Action Value=内网服务器地址
3.Http请求转发到内网服务器
明确了具体的实现方案后,我们需要先详细的研究一下OpenResty和Lua
http://openresty.org/cn/
https://www.tutorialspoint.com/lua/
大致了解OpenResty是什么,能做什么,同时能简单写一些Lua脚本。
然后,我们重点看OpenResty提供的样例:
Dynamic Routing Based On Redis
通过这个样例,我们就可以模仿着写一个我们自己的配置和脚本,实现上面的动态路由的需求。
Come On。
一、解析URL中的Action
首先,要解析并拆分URL字符串,各种百度和Google,需要写一段Lua代码,实现字符串按"/"拆分

其实就是定义了一个split_path函数。
然后我们将上面的Split.lua文件,配置到Nginx的配置文件中
/usr/local/openresty/nginx/conf/nginx.conf

注:split.lua文件我们放在了
/usr/local/openresty/nginx/lua
编辑/usr/local/openresty/nginx/conf/nginx.conf文件

http {
include mime.types;
default_type application/octet-stream; sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
init_worker_by_lua_file /usr/local/openresty/nginx/lua/split.lua;
server {
listen 80;
location = /redis {
internal;
set_unescape_uri $key $arg_key;
redis2_query get $key;
redis2_pass RedisServer:6379;
}
location / {
set $target '';
access_by_lua '
local parameters = split_path(ngx.var.uri)
local action = parameters[1]

location中的access_by_lua '这一段负责执行Lua脚本和方法
local action = parameters[1]
这样,我们便解析到了Action,注:Lua中数组的下标从1开始
二.访问Redis,Key=Action Value=内网服务器地址
继续编辑Nginx配置文件

location / {
set $target '';
access_by_lua '
local parameters = split_path(ngx.var.uri)
local action = parameters[1]
if(#parameters == 0) then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
local key = action
local res = ngx.location.capture(
"/redis", { args = { key = key } }
)if res.status ~= 200 then
ngx.log(ngx.ERR, "redis server returned bad status: ",
res.status)
ngx.exit(res.status)
end
if not res.body then
ngx.log(ngx.ERR, "redis returned empty body")
ngx.exit(500)
end
local parser = require "redis.parser"
local server, typ = parser.parse_reply(res.body)
if typ ~= parser.BULK_REPLY or not server then
ngx.log(ngx.ERR, "bad redis response: ", res.body)
ngx.exit(500)
end
if server == "" then
server = "default.com"
end

三.Http请求转发到内网服务器
继续编辑Nginx.Conf文件

init_worker_by_lua_file /usr/local/openresty/nginx/lua/split.lua;
server {
listen 80;
location = /redis {
internal;
set_unescape_uri $key $arg_key;
redis2_query get $key;
redis2_pass RedisServer:6379;
} location / {
set $target '';
access_by_lua '
local parameters = split_path(ngx.var.uri)
local action = parameters[1]
if(#parameters == 0) then
ngx.exit(ngx.HTTP_FORBIDDEN)
end local key = action
local res = ngx.location.capture(
"/redis", { args = { key = key } }
) if res.status ~= 200 then
ngx.log(ngx.ERR, "redis server returned bad status: ",
res.status)
ngx.exit(res.status)
end if not res.body then
ngx.log(ngx.ERR, "redis returned empty body")
ngx.exit(500)
end local parser = require "redis.parser"
local server, typ = parser.parse_reply(res.body)
if typ ~= parser.BULK_REPLY or not server then
ngx.log(ngx.ERR, "bad redis response: ", res.body)
ngx.exit(500)
end if server == "" then
server = "default.com"
end
server = server .. "/api/" .. action
if ngx.var.QUERY_STRING ~= nil and ngx.var.QUERY_STRING ~= "" then
server = server .."&"..ngx.var.QUERY_STRING
end ngx.var.target = server
';
resolver 8.8.8.8;
proxy_pass http://$target;
}
}

至此,我们完成了Nginx的配置文件。
启动Nginx测试:
sudo /usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/Nginx.conf
PostMan发起一次请求:
查看Nginx日志

以上就是使用Nginx+lua+redis实现动态路由。分享给大家
Dynamic Routing Based On Redis的更多相关文章
- Hinton's paper Dynamic Routing Between Capsules 的 Tensorflow , Keras ,Pytorch实现
Tensorflow 实现 A Tensorflow implementation of CapsNet(Capsules Net) in Hinton's paper Dynamic Routing ...
- 【论文笔记】Dynamic Routing Between Capsules
Dynamic Routing Between Capsules 2018-09-16 20:18:30 Paper:https://arxiv.org/pdf/1710.09829.pdf%20 P ...
- Dynamic Routing Between Capsules
目录 概 主要内容 损失函数 代码 Sabour S, Frosst N, Hinton G E, et al. Dynamic Routing Between Capsules[C]. neural ...
- Redis Installation、Configuration、Program Based On Redis Learning
目录 . Redis 简介 . Redis安装配置 . 编程使用Redis . 使用Lua脚本 1. Redis 简介 0x1: Redis是什么 Redis是一款Nosql类型的基于key-valu ...
- dynamic routing between captual
对于人脑 决策树形式 对于CNN 层级与层级间的传递 人在识别物体的时候会进行坐标框架的设置 CNN无法识别,只能通过大量训练 胶囊 :一个神经元集合,有一个活动的向量,来表示物体的各类信息,向量的长 ...
- Paper | SkipNet: Learning Dynamic Routing in Convolutional Networks
目录 1. 概括 2. 相关工作 3. 方法细节 门限模块的结构 训练方法 4. 总结 作者对residual network进行了改进:加入了gating network,基于上一层的激活值,得到一 ...
- Ngnix技术研究系列2-基于Redis实现动态路由
上篇博文我们写了个引子: Ngnix技术研究系列1-通过应用场景看Nginx的反向代理 发现了新大陆,OpenResty OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台 ...
- Nginx技术研究系列2-基于Redis实现动态路由
上篇博文我们写了个引子: Ngnix技术研究系列1-通过应用场景看Nginx的反向代理 发现了新大陆,OpenResty OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台 ...
- nginx HttpLuaModule
http://wiki.nginx.org/HttpLuaModule#Directives Name ngx_lua - Embed the power of Lua into Nginx This ...
随机推荐
- 请教MAC OS下PHP的mcrypt怎么安装
安装方法一: 通过Homebrew安装mcrypt,安装成功 [Shell] 纯文本查看 复制代码 brew install mcrypt MCrypt是一个功能强大的加密算法扩展库,它包括有22种算 ...
- git命令行解决冲突文件步骤
原文https://blog.csdn.net/zwl18210851801/article/details/79106448 亲测有用,解决git冲突的好办法 方法一(推荐使用): git pull ...
- php使用curl模拟多线程发送请求
每个PHP文件的执行是单线程的,但是php本身也可以用一些别的技术实现多线程并发比如用php-fpm进程,这里用curl模拟多线程发送请求.php的curl多线程是通过不断调用curl_multi_e ...
- Ros使用Arduino 3用rosserial创建一个subscriber
在前面的一节中,我们已经使用arduino创建了一个publisher节点,接下来将会用arduino来创建一个subscriber,接收电脑传去的信息并做出相应的反应. 1启动Arduino 将ar ...
- B-Tree外存数据结构 _(外存储器—磁盘)第一部分
1.外存储器—磁盘 计算机存储设备一般分为两种:内存储器(main memory)和外存储器(external memory).内存存取速度快,但容量小,价格昂贵,而且不能长期保存数据(在不通电情况下 ...
- 2018AVA: A Video Dataset of Spatio-temporally Localized Atomic Visual Actions
论文标题:AVA: A Video Dataset of Spatio-temporally Localized Atomic Visual Actions 来源/作者机构情况: 谷歌,http:// ...
- luogu P4403 [BJWC2008]秦腾与教学评估
题目 一道神奇的题qwq 首先看题很容易想到把所有的点存下来然后暴力枚举...于是RE 20分 所以要找一种不用开那么大的数组的解法(然而我自己是不可能想出来的qwq 注意一个地方,人数为奇数的位置“ ...
- 9-51单片机ESP8266学习-AT指令(单片机采集温湿度数据通过8266发送给AndroidTCP客户端显示)
http://www.cnblogs.com/yangfengwu/p/8798512.html 补充:今天答应了一个朋友写一下如果单片机发过的是字符串应该怎么解析,答应了今天写,哦哦哦是明天了,闲话 ...
- 安全提示:IIS不要开启“WebDAV”扩展(转载)
在IIS设置里,有一个“Web服务扩展”的设置,其中包括“WebDAV”扩展.许多人都不明白,这个“WebDAV”扩展是干嘛用的,要不要开启呢?有不少人的想法是“开启吧,以免影响网站运行,启用总比不启 ...
- NOIp2018停课刷题记录
Preface 老叶说了高中停课但是初中不停的消息后我就为争取民主献出一份力量 其实就是和老师申请了下让我们HW的三个人听课结果真停了 那么还是珍惜这次机会好好提升下自己吧不然就\(AFO\)了 Li ...