一、概念
openresty是一个基于nginx的可伸缩的web平台,是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统。
二、安装openresty
1、添加仓库执行命令
yum install yum-utilsyum-configg-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum install openresty -y
/usr/local/openresty
三、安装nginx
nginx默认已经安装好了。
在usr/local/openresty/nginx修改/usr/local/openresty/nginx/conf/nginx.conf,将配置文件使用的根设置为root,目的就是将来要使用lua脚本的 时候 ,直接可以加载在root下的lua脚本。
cd /usr/local/openresty/nginx/conf
vim nginx.conf
修改
启动openresty
cd /usr/local/openresty/nginx/sbin
./nginx
停止openresty
./nginx -s stop
重启openresty
./nginx -s reload
测试访问openresty是否可用,访问地址http://你的ip
四、部署项目
1、创建mysql数据库
2、创建redis数据库
3、创建/root/lua目录,在该目录下创建update_content.lua: 目的就是连接mysql 查询数据 并存储到redis中。update_content.lua代码如下:
-- 响应类型
ngx.header.content_type="application/json;charset=utf8"
-- json转换模块
local cjson = require("cjson")
-- mysql连接模块
local mysql = require("resty.mysql")
-- 获取所有请求参数
local uri_args = ngx.req.get_uri_args()
-- 获取请求参数中的id
local categoryid = uri_args["id"]-- 创建数据库对象
local db = mysql:new()
-- 超时时间 1000毫秒
db:set_timeout(1000)
-- 连接数据库属性
local props = {
host = "192.168.43.129",
port = 33306,
database = "dn",
user = "root",
password = "123456"
}
-- 连接数据库
local res = db:connect(props)
-- 定义查询语句
local select_sql = "select url,pic from tb_content where status ='1' and category_id='"..categoryid.."'"
res = db:query(select_sql)
db:close()ngx.say("{\"res\":cjson.encode(res)}")
local redis = require("resty.redis")
local red = redis:new()
red:set_timeout(2000)local ip = "192.168.43.129"
local port = 6379
red:connect(ip,port)
red:set("content_"..categoryid,cjson.encode(res))
red:close()ngx.say("{\"flag\":true}")
之后再创建read_content.lua、read_new_content.lua、remove_content.lua。
read_content.lua代码如下:
--设置响应头类型
ngx.header.content_type="application/json;charset=utf8"
--获取请求中的参数ID
local uri_args = ngx.req.get_uri_args();
local id = uri_args["id"];
--引入redis库
local redis = require("resty.redis");
--创建redis对象
local red = redis:new()
--设置超时时间
red:set_timeout(2000)
--连接
local ok, err = red:connect("192.168.43.129", 6379)
--获取key的值
local rescontent=red:get("content_"..id)
--输出到返回响应中
ngx.say(rescontent)
--关闭连接
red:close()
read_new_content.lua代码如下:
ngx.header.content_type="application/json;charset=utf8"
local uri_args = ngx.req.get_uri_args();
local categoryid = uri_args["id"];
--获取本地缓存
local cache_ngx = ngx.shared.dis_cache;
--根据ID 获取本地缓存数据
local contentCache = cache_ngx:get('content_cache_'..categoryid);if contentCache == "" or contentCache == nil
then
local redis = require("resty.redis");
local red = redis:new()
red:set_timeout(2000)
red:connect("192.168.43.129", 6379)
local rescontent=red:get("content_"..categoryid);
if ngx.null == rescontent or rescontent == ""
then
local cjson = require("cjson");
local mysql = require("resty.mysql");
local db = mysql:new();
db:set_timeout(2000)
local props = {
host = "192.168.43.129",
port = 33306,
database = "dn",
user = "root",
password = "123456"
}
local res = db:connect(props);
local select_sql = "select url,pic from tb_content where status ='1' and category_id='"..categoryid.."'";
res = db:query(select_sql);
local responsejson = cjson.encode(res);
red:set("content_"..categoryid,responsejson);
-- ngx.say(" === ");
ngx.say(responsejson);
-- ngx.say(" === ");
-- ngx.say("{\"响应来自\":\"mysql\"}")
db:close();
else
cache_ngx:set('content_cache_'..categoryid, rescontent, 10*60);
-- ngx.say("{\"响应来自\":\"redis缓存\"}")
ngx.say(rescontent);
end
red:close();
else
ngx.say(contentCache)
-- ngx.say("{\"响应来自\":\"nginx本地缓存\"}");
end
remove_content.lua代码如下:
ngx.header.content_type="application/json;charset=utf8"
local uri_args = ngx.req.get_uri_args();
local categoryid = uri_args["id"];
--获取本地缓存
local cache_ngx = ngx.shared.dis_cache;
--清除本地缓存数据
cache_ngx:set('content_cache_'..categoryid,nil);local redis = require("resty.redis");
local red = redis:new();
red:connect("192.168.43.129",6379);
red:set("content_"..categoryid,"");ngx.say("{\"msg\":\"缓存被清除\"}")
修改/usr/local/openresty/nginx/conf/nginx.conf文件: 添加头信息,和 location信息
![](https://img-blog.csdnimg.cn/direct/566521097aac462d8a7435309cd1af3c.png)
![](https://img-blog.csdnimg.cn/direct/ad1bc50f52604bfba3680f35850cc0f4.png)
![](https://img-blog.csdnimg.cn/direct/b1616a06ca5b48efa1c7abf3dac23a10.png)
![](https://img-blog.csdnimg.cn/direct/28856fcb28bf460ea979cb7890717ca2.png)