http路由

路由是指路由器从一个接口上收到数据包,根据数据包的目的地址进行定向并转发到另一个接口的过程。

而这里的http路由其实等同于web开发中,根据http相关参数(比如url、http method)分配到对应的处理程序。

借用web框架的示意图,其作用如下

路由匹配

这里我们先简化一下内容,假设我们已有 upstream ip、port,现在只需能区分各种请求怎么样对应到这些不同的upstream上,不必关心能否做改写请求啊、熔断啊等等复杂情况

那么怎么实现路由匹配呢?

通常为以下两种方式

  1. 字典+正则表达式

    字典用于匹配精确的结果(比如 url == /login 情况),字典的特性保证这类匹配具有超高性能

    正则表达式用于匹配复杂模糊的结果(比如 url 以 .html 为后缀的所有请求), 当然多项正则表达式只能依次遍历,性能肯定存在问题(为了缓解性能问题,通常会使用缓存做优化)

  2. 前缀树

    前缀树,又称字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串。

    其由于插入和查询的效率很高,非常适合路由匹配的情况

    虽然理论hash性能最好,前缀树仍需查找,效率会低一些,

    但毕竟通常开发都会使用比较复杂的路由, 所以效率肯定比上面的 字典+正则表达式 要高很多

路由匹配实践

这里由于篇幅关系,只介绍 字典+正则表达式 简单实现,前缀树 则介绍apisix 中实现的库,毕竟算法要强悍的性能,纯lua实现是不太可靠的,必须得上c才行,这里就避免c的复杂度吧。

字典+正则表达式 简单实现


worker_processes 1; #nginx worker 数量
error_log logs/error.log; #指定错误日志文件路径
events {
worker_connections 1024;
} http {
log_format main '$remote_addr [$time_local] $status $request_time $upstream_status $upstream_addr $upstream_response_time';
access_log logs/access.log main buffer=16384 flush=3; #access_log 文件配置 upstream nature_upstream {
server 127.0.0.1:6699; #upstream 配置为 hello world 服务 # 一样的balancer
balancer_by_lua_block {
local balancer = require "ngx.balancer"
local upstream = ngx.ctx.api_ctx.upstream
local ok, err = balancer.set_current_peer(upstream.host, upstream.port)
if not ok then
ngx.log(ngx.ERR, "failed to set the current peer: ", err)
return ngx.exit(ngx.ERROR)
end
}
} init_by_lua_block {
local path_exact = {} -- 精确匹配字典
local path_reg = {} -- 正则遍历集合 -- 添加路由的方法
add_router = function(type, path, upstream)
if type == 'exact' then
path_exact[path] = upstream
else
table.insert(path_reg, {reg = path, upstream = upstream})
end
end -- 匹配方法,优先精确匹配
router_match = function()
local p = ngx.var.uri
local upstream = path_exact[p]
if upstream == nil then
for k, v in pairs(path_reg) do
if ngx.re.find(p, v.reg) then
return v.upstream
end
end
end return upstream
end -- 添加测试数据
add_router('exact' , '/aa/d', {host = '127.0.0.1', port = 6698})
add_router('reg' , '/aa/*', {host = '127.0.0.1', port = 6699})
} server {
#监听端口,若你的8699端口已经被占用,则需要修改
listen 8699 reuseport; location / { # 在access阶段匹配路由
access_by_lua_block {
local upstream = router_match()
if upstream then
ngx.ctx.api_ctx = { upstream = upstream }
else
ngx.exit(404)
end
} proxy_http_version 1.1;
proxy_pass http://nature_upstream; #转发到 upstream
}
} #为了大家方便理解和测试,我们引入一个hello world 服务
server {
#监听端口,若你的6699端口已经被占用,则需要修改
listen 6699;
location / {
default_type text/html; content_by_lua_block {
ngx.say("HelloWorld")
}
}
}
}

启动服务并测试

$ openresty -p ~/openresty-test -c openresty.conf #启动
$ curl --request GET 'http://127.0.0.1:8699/aa/d' #测试精确匹配
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
</body>
</html>
$ curl --request GET 'http://127.0.0.1:8699/aa/xxx' #测试正则匹配
HelloWorld
$ curl --request GET 'http://127.0.0.1:8699/dd/xxx' #测试不存的路由
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
</body>
</html>

核心原理其实就是 router_match 这个函数的内容,

不过上述简单实现肯定无法支持以下的一些复杂场景

  1. 正则冲突 (一般会引入优先级顺序支持,或者默认加入的顺序)
  2. host隔离
  3. 参数匹配
  4. 自定义条件匹配
  5. ……

所以一个完整的路由实现都很复杂,毕竟支持的场景挺多的

使用 lua-resty-radixtree 路由库

引入库

以下内容更新到 openresty-dev-1.rockspec

-- 依赖包
dependencies = {
"lua-resty-radixtree >= 2.8.2",
}

然后执行

luarocks install openresty-dev-1.rockspec --tree=deps --only-deps --local
代码调整

这里只列举变动的部分

http {

    init_by_lua_block {

        -- 初始化路由
local radix = require("resty.radixtree")
local r = radix.new({
{paths = {'/aa/d'}, metadata = {host = '127.0.0.1', port = 6698}},
{paths = {'/aa/*'}, metadata = {host = '127.0.0.1', port = 6699}}
}) -- 匹配路由
router_match = function()
local upstream, err = r:match(ngx.var.uri, {})
if err then
log.error(err)
end
return upstream
end
} }

启动服务并测试

$ openresty -p ~/openresty-test -c openresty.conf #启动
$ curl --request GET 'http://127.0.0.1:8699/aa/d' #测试精确匹配
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
</body>
</html>
$ curl --request GET 'http://127.0.0.1:8699/aa/xxx' #测试正则匹配
HelloWorld
$ curl --request GET 'http://127.0.0.1:8699/dd/xxx' #测试不存的路由
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
</body>
</html>

可以看到效果一样

更多复杂使用请参阅 https://github.com/api7/lua-resty-radixtree

目录

最新文章

  1. CentOS修改mysql 用户root的密码并允许远程登录
  2. Jenkins进阶系列之——12详解Jenkins节点配置
  3. hdu 5755(高斯消元——模线性方程组模板)
  4. 使用HTML5构建下一代的Web Form
  5. 获取oracle 表字段,表名,以及主键之类等等的信息
  6. BZOJ1660: [Usaco2006 Nov]Bad Hair Day 乱发节
  7. 初学python之路-day07-字符编码
  8. 《JavaScript Dom 编程艺术》读书笔记-第11章
  9. Java的并发及锁
  10. Ajax与CORS通信
  11. Codeforces.1096E.The Top Scorer(组合)
  12. 在Struts.xml中的result元素指的是:指定动作类的动作方法执行完后的结果视图.
  13. 如何在IntelliJ IDEA中使用Git .ignore插件忽略不必要提交的文件
  14. [No0000CC]眼袋和黑眼圈的应对方法——疏筋穴
  15. python三大框架之一flask中cookie和session的相关操作
  16. A Magic Lamp -- hdu -- 3183
  17. JavaScript -- Math
  18. Gridview中的选择、删除、编辑、更新、取消留着备用。
  19. python网络编程-协程(协程说明,greenlet,gevent)
  20. Nodejs + express + ejs

热门文章

  1. 中小型企业综合项目(Nginx+LVS+Tomcat+MGR+Nexus+NFS)
  2. scrapy 如何使用代理 以及设置超时时间
  3. CSS布局秘籍(1)-任督二脉BFC/IFC
  4. Codeforces Round #781(C. Tree Infection)
  5. java学习之EL和JSTL
  6. NC-UClient下载安装应用详解
  7. R数据分析:孟德尔随机化实操
  8. 数据结构(二):括号匹配(C++,栈)
  9. 【devexpress】Gridcontorl分组时自定义底部页脚求和功能
  10. 这可能是最全的SpringBoot3新版本变化了!