Openresty+Lua+Redis实现高性能缓存

一、背景

当我们的程序需要提供较高的并发访问时,往往需要在程序中引入缓存技术,通常都是使用Redis作为缓存,但是要再更进一步提升性能的话,就需要尽可能的减少请求的链路长度,比如可以将访问Redis缓存从Tomcat服务器提前Nginx

原本访问缓存逻辑

User---> Nginx -> Tomcat -> Redis 

User---> Nginx -> Redis 

二、介绍

1 OpenResty 介绍

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

官网: OpenResty® - 开源官方站

2 Lua 介绍

Lua 是一个小巧的脚本语言。它是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个由Roberto Ierusalimschy、Waldemar Celes 和 Luiz Henrique de Figueiredo三人所组成的研究小组于1993年开发的。 其设计目的是为了通过灵活嵌入应用程序中从而为应用程序提供灵活的扩展和定制功能。Lua由标准C编写而成,几乎在所有操作系统和平台上都可以编译,运行。Lua并没有提供强大的库,这是由它的定位决定的。所以Lua不适合作为开发独立应用程序的语言。Lua 有一个同时进行的JIT项目,提供在特定平台上的即时编译功能。

推荐教程: Lua 教程 | 菜鸟教程

三、软件安装

1 OpenResty 安装

下载最新版本

上传到虚拟机的/usr/local 目录下,之后解压

这里前提是需要安装c语言编译器和Nginx依赖包(如已经安装过了跳过下面3个命令),否则下面的安装会报错的

yum install -y gcc

yum install -y pcre pcre-devel

yum install -y zlib zlib-devel

yum install -y openssl openssl-devel

进入到解压后的文件夹  openresty-1.25.3.1 中执行

./configure --prefix=/usr/local/openresty

正常的话,出现下面的画面说明执行成功了

然后执行make && make install 

make 

make install 

执行完成后,可以看到在/usr/local 目录下多了一个openresty 目录

2 目录介绍

  1. bin目录:执行文件目录。
  2. lualib目录:这个目录存放的是OpenResty中使用的Lua库,主要分为ngx和resty两个子目录。
  3. nginx目录:这个目录存放的是OpenResty的nginx配置和可执行文件。
  4. luajit目录:luajit目录是LuaJIT的安装根目录,用于提供LuaJIT的运行环境和相关资源。

3 启动Nginx

 nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf

4 访问Nginx

在浏览器中输入虚拟机的地址http://192.168.31.115/

四、Openresty中初试Lua

1 编辑nginx.conf

在server{}中插入下面代码

location /lua {default_type text/html;content_by_lua 'ngx.say("<p>hello,world</p>")';}

2 重启一下Nginx

nginx/sbin/nginx -s stop

nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf

3 访问浏览器

在浏览器中输入虚拟机的IP地址+lua

http://192.168.31.115/lua

正常的话应该可以看到下面的画面

 4 通过lua文件的方式

进入到Nginx目录,创建lua文件夹,并新建一个hello.lua文件

cd nginx

mkdir lua

vim lua/hello.lua

ngx.say("<p>hello,hello,hello</p>")

修改nginx.conf 文件

location /lua {default_type text/html;content_by_lua_file lua/hello.lua;}

重启Nginx,再次刷新网站

5 Openresty连接Redis

参考官网文档:GitHub - openresty/lua-resty-redis: Lua redis client driver for the ngx_lua based on the cosocket API

在/usr/local/openresty/nginx/lua目录下,编辑一个redis.lua 文件,内容如下:

local redis = require "resty.redis"
local red = redis:new()red:set_timeouts(1000, 1000, 1000) -- 1 seclocal ok, err = red:connect("192.168.31.114", 6579)
if not ok thenngx.say("failed to connect: ", err)return
endlocal res, err = red:auth("123456")
if not res thenngx.say("failed to authenticate: ", err)return
endok, err = red:set("dog", "an animal")
if not ok thenngx.say("failed to set dog: ", err)return
endngx.say("set result: ", ok)local res, err = red:get("dog")
if not res thenngx.say("failed to get dog: ", err)return
endif res == ngx.null thenngx.say("dog not found.")return
endngx.say("dog: ", res)

再修改nginx.conf 

location /lua {default_type text/html;content_by_lua_file lua/redis.lua;}

访问浏览器

到这里,我们已经成功使用Nginx通过lua脚本访问到了Redis,但是这种写法仍然有一个巨大的问题,就是每次请求都会重新连接Redis,性能非常低下,我们测试一下这样写的接口性能

6 解决Redis重复连接问题

再修改nginx.conf 

require("my/cache").go()

五、真实案例

1 案例背景

应用程序中有一个接口/goods-center/getGoodsDetails 希望通过nginx先查询Redis缓存,缓存中没有就去应用服务中查询,然后把查询到结果缓存到Redis中

2 Nginx获取请求的参数

编辑conf/nginx.conf 中的server,添加下面配置

        location /goods-center/getGoodsDetails {default_type application/json;content_by_lua_file lua/item.lua;}

然后在/usr/local/openresty/nginx/lua 目录中创建item.lua,添加下面lua代码

local args = ngx.req.get_uri_args()ngx.say(args["goodsId"])

重新加载nginx配置

sbin/nginx -s reload

浏览器演示

3 转发请求到后端服务

定义一些工具类,方便后续写代码时调用,在/usr/local/openresty/lualib/mylua/common.lua

在common.lua中添加http get工具方法

local function http_get(path,params)local resp = ngx.location.capture(path,{method = ngx.HTTP_GET,args = params})if not resp thenngx.log(ngx.ERR)ngx.exit(404)endreturn resp.body
endlocal _M = {http_get = http_get
}return _M

编辑/usr/local/openresty/nginx/lua/item.lua 文件

-- 导入common包
local common = require('mylua.common')
local http_get = common.http_getlocal args = ngx.req.get_uri_args()-- 查询商品信息
local itemJson = http_get("/goods-center/getGoodsDetails",args)ngx.say(itemJson)

修改/usr/local/openresty/nginx/conf/nginx.conf 添加下面代码

    	location /nginx-cache/goods-center/getGoodsDetails {default_type application/json;content_by_lua_file lua/item.lua;}location ~ ^/goods-center/ {proxy_pass http://192.168.31.112:9527;}

解释一下上面代码,将/nginx-cache/goods-center/getGoodsDetails 请求通过lua脚本处理,转发到/goods-center/getGoodsDetails ,再通过~ ^/goods-center/  反向代理到应用服务器上http://192.168.31.112:9527;

演示:

4 优先查询Redis (官方并发会报错)

官网文档:GitHub - openresty/lua-resty-redis: Lua redis client driver for the ngx_lua based on the cosocket API

这应该是我到目前为止最想吐槽的开源软件了,按照官网的文档操作简直就是个玩具,无法商用

只要超过1个线程去压测就会报bad request 错误,这个在官网的局限性一栏中有提到,但是不明白为什么不解决,这个问题不解决,就无法商用,而且每次请求都会创建连接,性能巨差,关键这个问题在网上都很少有人提出过这个问题,包括一些教学视频,都是点到为止,根本没有测试过并发场景能不能用,我只要一并发测试就GG,怎么改都不行,翻了很多文档,都没有解决方案,如果有人有方案可以在评论区分享一下,互相学习

GitHub - openresty/lua-resty-redis: Lua redis client driver for the ngx_lua based on the cosocket API

先看代码

/usr/local/openresty/lualib/mylua/common.lua

local redis = require('resty.redis')
local red = redis:new()
red:set_timeouts(1000,1000,1000)local function get_from_redis(key)ngx.log(ngx.INFO,"redis init start .............")local ok,err = red:connect("192.168.31.114",6579)-- 连接失败if not ok thenngx.log(ngx.ERR,"connect redis error",err)return nilend-- 认证失败local res, err = red:auth("123456")if not res thenngx.say(ngx.ERR,"failed to authenticate: ", err)return nilendlocal resp,err = red:get(key)if not resp thenngx.log(ngx.ERR,"get from redis error ",err," key: ",key)return nilend-- 数据为空if resp == ngx.null thenngx.log(ngx.ERR,"this key is nil, key: ",key)return nilend-- 设置连接超时时间和连接池大小red:set_keepalive(600000, 100)return respendlocal function http_get(path,params)local resp = ngx.location.capture(path,{method = ngx.HTTP_GET,args = params})if not resp thenngx.log(ngx.ERR)ngx.exit(404)endreturn resp.body
endlocal _M = {http_get = http_get,get_from_redis = get_from_redis
}return _M

/usr/local/openresty/nginx/lua/item.lua

-- 导入common包
local _M = {}common = require('mylua.common')
http_get = common.http_get
get_from_redis = common.get_from_redisfunction _M.get_data()local args = ngx.req.get_uri_args()-- 先查询Redislocal itemJson = get_from_redis("goods-center:goodsInfo:" .. args["goodsId"])ngx.log(ngx.INFO,"get from redis itemJson,  ",itemJson)if itemJson == nil then-- redis 没有,则查询服务器信息itemJson = http_get("/goods-center/getGoodsDetails",args)endngx.say(itemJson)
endreturn _M

修改/usr/local/openresty/nginx/conf/nginx.conf 添加下面代码

    server {location /nginx-cache/goods-center/getGoodsDetails {default_type application/json;content_by_lua_block {require("lua/item").get_data()}}location ~ ^/goods-center/ {proxy_pass http://192.168.31.112:9527;}

单线程测试(没有报错,且有500多的吞吐量)

2个线程测试,有40%+ 的错误率,报错详情截图给了,线程越多报错越多

2024/02/04 21:56:06 [error] 21662#0: *390686 lua entry thread aborted: runtime error: /usr/local/openresty/lualib/resty/redis.lua:166: bad request
stack traceback:
coroutine 0:[C]: in function 'connect'/usr/local/openresty/lualib/resty/redis.lua:166: in function 'connect'/usr/local/openresty/lualib/mylua/common.lua:9: in function 'get_from_redis'./lua/item.lua:12: in function 'get_data'content_by_lua(nginx.conf:49):2: in main chunk, client: 192.168.31.32, server: localhost, request: "GET /nginx-cache/goods-center/getGoodsDetails?goodsId=10000 HTTP/1.1", host: "192.168.31.115"

5 使用ngx.shared.redis_pool连接池(并发不会报错)

/usr/local/openresty/lualib/mylua/common.lua

-- 引入 lua-resty-redis 模块
local redis = require "resty.redis"-- 获取 OpenResty 全局字典对象(连接池)
local redis_pool = ngx.shared.redis_pool-- Redis 连接池的最大连接数
local max_connections = 100-- Redis 服务器地址和端口
local redis_host = "192.168.31.114"
local redis_port = 6579-- 获取 Redis 连接
local function get_redis_connection()local red = redis_pool:get(redis_host)if not red thenngx.log(ngx.ERR, "create new : ", err)-- 创建一个新的 Redis 连接red = redis:new()-- 设置连接超时时间red:set_timeout(1000,1000,1000)-- 连接 Redis 服务器local ok, err = red:connect(redis_host, redis_port)if not ok thenngx.log(ngx.ERR, "Failed to connect to Redis: ", err)return nil, errendlocal res, err = red:auth("123456")if not res thenngx.say(ngx.ERR,"failed to authenticate: ", err)return nilend-- 将连接放入连接池redis_pool:set(redis_host, red, 600)endreturn red
endlocal function get_from_redis(key)local redis_conn, err = get_redis_connection()if not redis_conn thenngx.log(ngx.ERR, "Failed to get Redis connection: ", err)ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)end-- 获取失败local resp,err = redis_conn:get(key)if not resp thenngx.log(ngx.ERR,"get from redis error ",err," key: ",key)return nilend-- 数据为空if resp == ngx.null thenngx.log(ngx.ERR,"this key is nil, key: ",key)return nilend-- 设置连接超时时间redis_conn:set_keepalive(600000, max_connections)return resp
endlocal function http_get(path,params)local resp = ngx.location.capture(path,{method = ngx.HTTP_GET,args = params})if not resp thenngx.log(ngx.ERR)ngx.exit(404)endreturn resp.body
endlocal _M = {http_get = http_get,get_from_redis = get_from_redis
}return _M

/usr/local/openresty/nginx/lua/item.lua

-- 导入common包
local _M = {}common = require('mylua.common')
http_get = common.http_get
get_from_redis = common.get_from_redisfunction _M.get_data()local args = ngx.req.get_uri_args()-- 先查询Redislocal itemJson = get_from_redis("goods-center:goodsInfo:" .. args["goodsId"])ngx.log(ngx.INFO,"get from redis itemJson,  ",itemJson)if itemJson == nil then-- redis 没有,则查询服务器信息itemJson = http_get("/goods-center/getGoodsDetails",args)endngx.say(itemJson)
endreturn _M

修改/usr/local/openresty/nginx/conf/nginx.conf 添加下面代码

    lua_shared_dict redis_pool 100m;server {location /nginx-cache/goods-center/getGoodsDetails {default_type application/json;content_by_lua_block {require("lua/item").get_data()}}location ~ ^/goods-center/ {proxy_pass http://192.168.31.112:9527;}

6 压测详情

1 命中缓存压测

命中缓存的情况吞吐量1400多,并且采用本章第5小结这种线程池的方式连接不会报错,基本上可以商用了,唯一缺点是并发量没有达到预期,通过排查原因发现,大量的连接池并未真正生效,仍然有大量的创建连接,可能这是影响性能的主要因素,如果有同学解决了这个问题可以在评论区分享一下

2 未命中缓存压测

未命中缓存,会请求后端Tomcat服务器,Tomcat服务器会查询MySQL,这边的吞吐量测试数据为313,也不怎么高,排查了一下原因,仍然是Redis一直在不停的创建连接,这个问题目前还没有找到解决方案

六、总结

通过Nginx+lua 的方式,在Nginx这层就去查询Redis缓存,看起来的确是个非常棒的方案,但是缺点是操作起来特别麻烦,需要开发人员了解Nginx + Lua  还要了解Openresty 如何集成Nginx + Lua  + Redis,还要掌握在这种方式下,能够使用好Redis的连接池。最关键的是目前这种技术的文档并不完善,代码在某些地方不是特别的成熟,网上能找到的资料都很少而且都比较皮毛,不够深入,然而开发人员却需要深入地了解他们,才能比较好的驾驭这种方式,这次探究仍然有一个遗留问题就是Openresty + Lua + Redis 连接池的方式,连接池看起来有时候会不生效,也不是每次都不生效,有一定的概率,从而导致性能并不高,这个需要后面再研究解决它

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/452287.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java社区养老年人服务系统springboot+vue

为了帮助用户更好的了解和理解程序的开发流程与相关内容&#xff0c;本文将通过六个章节进行内容阐述。 第一章&#xff1a;描述了程序的开发背景&#xff0c;程序运用于现实生活的目的与意义&#xff0c;以及程序文档的结构安排信息&#xff1b; 第二章&#xff1a;描述了程序…

12. Threejs案例-绘制颜色渐变圆柱体

12. Threejs案例-绘制颜色渐变圆柱体 实现效果 知识点 CylinderGeometry (圆柱缓冲几何体) 一个用于生成圆柱几何体的类。 构造器 CylinderGeometry(radiusTop : Float, radiusBottom : Float, height : Float, radialSegments : Integer, heightSegments : Integer, open…

记录Git无法连接Github(443报错)的一种可能——代理问题

参考文章&#xff1a; Git安装配置与使用&#xff08;超级详细&#xff09;_git配置-CSDN博客 github代理报错_valueerror: unable to determine socks version from-CSDN博客 速通 如果在使用 git 时遇到了这样的报错&#xff1a; OpenSSL SSL_connect: SSL_ERROR_SYSCAL…

python Flask 写一个简易的 web 端程序(附demo)

python Flask 写一个简易的 web 端程序 &#xff08;附demo&#xff09; 介绍简单介绍装饰器 app.route("/") 进阶增加接口设置端口 静态网页核心代码完整代码 介绍 Flask 是一个用于构建 Web 应用程序的轻量级 Python Web 框架。它设计简单、易于学习和使用&#x…

MySQL学习记录——삼 库的操作

文章目录 1、创建数据库2、字符集和校验集3、基本操作4、备份与恢复5、连接情况 1、创建数据库 开两个窗口&#xff0c;一个用来访问数据目录/var/lib/mysql&#xff0c;一个用来打开mysql&#xff1a;mysql -u root -p。 创建用的命令 create databse d1; d1是名字&#xff…

LeetCode--代码详解 2.两数相加

2.两数相加 题目 难度&#xff1a;中等 给你两个 非空 的链表&#xff0c;表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的&#xff0c;并且每个节点只能存储 一位 数字。 请你将两个数相加&#xff0c;并以相同形式返回一个表示和的链表。 你可以假设除了数…

认识Tomcat (一)

认识Tomcat &#xff08;一&#xff09; 一、服务器 1.1 服务器简介 ​ 硬件服务器的构成与一般的PC比较相似&#xff0c;但是服务器在稳定性、安全性、性能等方面都要求更高&#xff0c;因为CPU、芯片组、内存、磁盘系统、网络等硬件和普通PC有所不同。 ​ 软件服务器&…

PAT-Apat甲级题1007(python和c++实现)

PTA | 1007 Maximum Subsequence Sum 1007 Maximum Subsequence Sum 作者 CHEN, Yue 单位 浙江大学 Given a sequence of K integers { N1​, N2​, ..., NK​ }. A continuous subsequence is defined to be { Ni​, Ni1​, ..., Nj​ } where 1≤i≤j≤K. The Maximum Su…

WordPress可以做企业官网吗?如何用wordpress建公司网站?

我们在国内看到很多个人博客网站都是使用WordPress搭建&#xff0c;但是企业官网的相对少一些&#xff0c;那么WordPress可以做企业官网吗&#xff1f;如何用wordpress建公司网站呢&#xff1f;下面boke112百科就跟大家简单说一下。 WordPress是一款免费开源的内容管理系统&am…

算法学习打卡day47|单调栈系列题目

单调栈题目思路 通常是一维数组&#xff0c;要寻找任一个元素的右边或者左边第一个比自己大或者小的元素的位置&#xff0c;此时我们就要想到可以用单调栈了。时间复杂度为O(n)。单调栈的本质是空间换时间&#xff0c;因为在遍历的过程中需要用一个栈来记录右边第一个比当前元…

力扣面试150 只出现一次的数字Ⅱ 哈希 统计数位 DFA有穷自动机

Problem: 137. 只出现一次的数字 II 文章目录 思路&#x1f496; 哈希&#x1f496; 位数统计&#x1f496; DFA 状态机 思路 &#x1f468;‍&#x1f3eb; 参考 &#x1f496; 哈希 ⏰ 时间复杂度: O ( n ) O(n) O(n) &#x1f30e; 空间复杂度: O ( n ) O(n) O(n) cl…

5分钟快速掌握 XML (Extensible Markup Language)

背景 在Java开发的过程中&#xff0c;我们经常需要和配置文件打交道&#xff0c;其中接触最多的就是XML。从最初学习 JavaWeb 时在 Tomcat 中配置servlet&#xff0c;到后来接触Spring框架并在XML中编写各种配置&#xff0c;XML一直是不可或缺的一部分。然而&#xff0c;XML的…