博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx+Lua 定制流量分发策略案例
阅读量:2394 次
发布时间:2019-05-10

本文共 2703 字,大约阅读时间需要 9 分钟。

准备3台机器 eshop-cache01、eshop-cache02、eshop-cache03,用 eshop-cache01 和 eshop-cache02 作为应用层 Nginx服务器,用 eshop-cache03 作为分发层 Nginx。在 eshop-cache03,也就是分发层 Nginx 中,编写 Lua脚本,完成基于 商品id 的流量分发策略
1> 获取请求参数,比如 productId
2> 对 productId 进行 hash
3> hash值 对应用服务器数量取模,获取到一个应用服务器
4> 利用 http 发送请求到应用层 nginx
5> 获取响应后返回
基于商品id的定向流量分发的策略,Lua脚本来编写和实现
作为一个流量分发的 Nginx,会发送 http请求到后端的应用 Nginx 上面去,所以要先引入 lua http lib包(一个网络请求的库)
GitHub访问地址 : 
# cd /opt/modules/openresty/lualib/resty
# wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua
--2018-05-10 21:23:25--  https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua
正在解析主机 raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.72.133
正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|151.101.72.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1150 (1.1K) [text/plain]
正在保存至: “http_headers.lua”

100%[==============================================================================================================================================>] 1,150       --.-K/s 用时 0s

2018-05-10 21:23:27 (262 MB/s) - 已保存 “http_headers.lua” [1150/1150])
# wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua
--2018-05-10 21:24:33--  https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua
正在解析主机 raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.72.133
正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|151.101.72.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:29686 (29K) [text/plain]
正在保存至: “http.lua”

100%[==============================================================================================================================================>] 29,686      --.-K/s 用时 0.1s

2018-05-10 21:24:34 (239 KB/s) - 已保存 “http.lua” [29686/29686])
创建 hellow.lua 脚本
`hellow.lua
-- 获取访问参数中的 productId
local uri_args = ngx.req.get_uri_args()
local productId = uri_args["productId"]

-- 通过对 productId 的 hash 计算,获取对应服务器地址
local host = { "192.168.31.19", "192.168.31.187" }
local hash = ngx.crc32_long(productId)
hash = (hash % 2) + 1
backend = "http://" .. host[hash]

-- 将访问路径拼接成相应的访问地址
local method = uri_args["method"]
local fullUri = backend .. "/" .. method .. "?productId=" .. productId

-- 重新进行访问
local http = require("resty.http")
local httpc = http.new()
local resp, err = httpc:request_uri(fullUri, {
    method = "GET"
})

-- 访问失败报错
if not resp then
    ngx.say("request error :", err)
    return
end

-- 访问成功返回对应信息
ngx.say(resp.body)

-- 关闭访问请求
httpc:close()
重新加载所有配置,包括 Lua 脚本
# cd /opt/modules/openresty/nginx
# ./sbin/nginx -t
# ./sbin/nginx -s reload
基于商品id的定向流量分发策略的 Lua脚本就开发完了,如果请求的是固定的某一个商品,那么就一定会将流量打到固定的一个应用 Nginx 上面去 

转载地址:http://yogab.baihongyu.com/

你可能感兴趣的文章
java框架基础 静态代理和动态代理
查看>>
jQuery ajax开发基于json
查看>>
oracle数据库
查看>>
oracle中间的数据类型
查看>>
论文划分
查看>>
vscode利用cmake调试
查看>>
zcash挖矿
查看>>
zcash挖矿指南
查看>>
区块链术语解释
查看>>
./configure,make,make install的作用
查看>>
学术论文录用结果通知(Notification)
查看>>
Theorem等数学化的论述
查看>>
PKI和X509证书
查看>>
使用HttpClient爬取国内疫情数据
查看>>
引用传递和值传递有什么区别
查看>>
C++从入门到放肆!
查看>>
C++是什么?怎么学?学完了能得到什么?
查看>>
初学C语言没有项目练手怎么行,这17个小项目收下不谢
查看>>
学好C语言,你只需要这几句口诀!
查看>>
选择大于努力!0基础学好C语言编程,首先要掌握的是什么?
查看>>