openresty入门及技术指南

介绍       

       OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web 平台,由中国人章亦春发起,提供了很多高质量的第三方模块。 OpenResty 是一个基于 Nginx Lua 的高性能 web 平台,由中国人章亦春发起,其内部集成了大量精良的Lua库、第三方模块以及大多数的依赖项。用于方便搭建能处理超高并发、扩展性极高的动态 Web 应用、 web服务和动态网关OpenResty 简单理解成就相当于封装了 NGINX ,并且集成了 LUA脚本,开发人员只需要简单的使用其提供了 模块就可以实现相关的逻辑,而不像之前,还需要在 NGINX 中编写 lua 的脚本。
 

安装部署

我们在linux环境使用docker安装方式,可参考《Docker入门及技术指南》https://blog.csdn.net/yan_dk/article/details/89427641

1. 拉取一个 Openresty 的镜像
docker pull openresty/openresty
2. 随便构建一个容器
docker run -p 90:90 -d --name openresty openresty/openresty
3. 进入容器,查看配置文件的路径
docker exec -it openresty bash
cd /etc/nginx/conf.d
4. 退出容器,复制容器中配置文件到宿主机
# docker cp openresty:/etc/nginx/conf.d/default.conf /docker/openresty/conf/default.conf
# docker stop openresty
# docker rm openresty
# docker run -p 90:90 -d --name openresty -v /docker/openresty/conf/default.conf:/etc/nginx/conf.d/default.conf -v /docker/www:/docker/www --privileged=true openresty/openresty
其中:"/docker/www"是web应用的根目录
5. 修改配置文件
server {
    listen       90;
    server_name  localhost;

    charset utf-8;
    #access_log  /var/log/nginx/host.access.log  main;
	root /docker/www/webserver;
	index index.html;
}
6.浏览器访问http://{ip}:90

 访问http://{ip}:90/home/index.html

这是建立的一个简单测试页面的例子。

应用实例

openresty集成lua+redis缓存

应用场景需求说明:商城首页是用户频繁访问页面,我们使用缓存加载首页数据,会大大提高性能;

解决方案:使用openresty内置的ngnix的缓存结合lua脚本、使用redis,将首次访问数据库获得首页常用数据加载到redis缓存中,同时写入ngnix缓存,这样通过ngnix端缓存、redis缓存多级缓存,分解压力,实现了性能调优。

首先安装redis

# docker pull redis

# docker run --privileged=true --name redis -p 6371:6379 -v /docker/redis/data:/data -v /docker/redis/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --requirepass '123456'  --appendonly yes

//进入容器
# docker exec -it redis bash

//查看容器的ip地址
# docker inspect redis | grep IPAddress
"IPAddress": "172.17.0.4",

注意:映射本地与容器的目录和配置文件 ,宿主机端口:容器端口

redis.conf如下:

#bind 127.0.0.1
bind 0.0.0.0
port 6379
protected-mode no
requirepass 123456

安装数据库mysql

# docker pull mysql:5.7

# docker run -p 3301:3306 -d --name mysql5.7 -e MYSQL_ROOT_PASSWORD=123456 --privileged=true mysql:5.7 

# docker exec -it mysql5.7 bash

# docker inspect mysql5.7 | grep IPAddress

我们建立一个lmrs_product_categorys产品分类表,存储产品分类数据,模拟首页需要加载的产品分类数据。

建立lua脚本lmrs_home_index.lua

ngx.say("test555")
ngx.header.content_type = "application/json;charset=utf8"
local cache_ngx = ngx.shared.dis_cache;
local contentCache = cache_ngx:get("lmrs_home_index");
if contentCache == "" or contentCache == nil then
	local redis = require("resty.redis");
    local red = redis:new()
    red:set_timeout(3000)
    red:connect("172.17.0.4", 6379)
--	127.0.0.1
	red:auth("123456")
    local rescontent = red:get("lmrs_home_index");
    if "null" == rescontent or false == rescontent or "" == rescontent or nil==rescontent or null==rescontent then
		local cjson = require("cjson");
        local mysql = require("resty.mysql");
        local db = mysql:new();
        db:set_timeout(2000)
        local props = {
            host = "127.0.0.1",
            port = 3312,
            database = "lmrs_2008_shops",
            user = "root",
            password = "123456"
        }
        local res = db:connect(props);
        local select_sql = "select * from lmrs_product_categorys"
        res = db:query(select_sql);
        local responsejson = cjson.encode(res);
        red:set("lmrs_home_index", responsejson);
        ngx.say("lmrs_home_index-responsejson=",responsejson);
        db:close()
    else
        cache_ngx:set("lmrs_home_index", rescontent, 10 * 60);
        ngx.say("lmrs_home_index-rescontent=",rescontent)
	end
--ngx.say('type(ngx.null)=',type(ngx.null))
--ngx.say('ngx.null=',ngx.null)
 red:close()
else
    ngx.say(contentCache)
end

配置ngnix.conf文件

lua_shared_dict dis_cache 10m;
server {
    listen       90;
    server_name  localhost;

    charset utf-8;
    #access_log  /var/log/nginx/host.access.log  main;
	root /docker/www/webserver;
	index index.html;
	
	location /lmrs_home_index { 
	  content_by_lua_file /docker/www/lua/lmrs_home_index.lua; 
	}
...
}

浏览器访问:http://ip:90,页面随意做的就不展示了,页面中主要通过读取缓存加载产品分类数据并显示。

我们用postman做测试请求发送/lmrs_home_index,模拟获取首页产品分类数据

得到相应结果

这是从数据库中读取的数据,并加载到缓存中,我们可以看看redis缓存中是否已经存在了,验证一下执行结果,就知道了。

猜你喜欢

转载自blog.csdn.net/yan_dk/article/details/117261025
今日推荐