nginx+lua+redis初体验

1.下载nginx、lua、redis

nginx下载地址 wget  http://nginx.org/download/nginx-1.8.0.tar.gz

lua下载地址 wget http://www.lua.org/ftp/lua-5.1.5.tar.gz

redis下载地址 wget https://github.com/antirez/redis/archive/2.8.23.tar.gz

2.安装lua、luajit、redis

安装lua

tar zxf lua-5.1.5.tar.gz

yum install -y readline readline-devel

cd lua-5.1.5

make linux && make install

然后输入lua,就会进入lua命令行

安装luajit

wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz

tar zxf LuaJIT-2.0.4.tar.gz

cd LuaJIT-2.0.4

make && make install
安装redis

tar zxf 2.8.23.tar.gz

cd  redis-2.8.23

make && make install

然后输入redis-server,打开redis服务

3.获取nginx依赖模块

mkdir -p /home/modules && cd /home/modules

git clone https://github.com/openresty/lua-nginx-module.git

git clone https://github.com/simpl/ngx_devel_kit.git

git clone https://github.com/openresty/redis2-nginx-module.git

git clone https://github.com/openresty/set-misc-nginx-module.git

git clone https://github.com/openresty/echo-nginx-module.git

4.安装nginx

tar zxf nginx-1.8.0.tar.gz

cd nginx-1.8.0

yum -y install pcre-devel openssl openssl-devel

./configure --prefix=/usr/local/nginx --add-module=/home/modules/ngx_devel_kit --add-module=/home/modules/lua-nginx-module --add-module=/home/modules/redis2-nginx-module --add-module=/home/modules/set-misc-nginx-module --add-module=/home/modules/echo-nginx-module
make && make install

5.安装lua-cjson

wget http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz

tar zxf lua-cjson-2.1.0.tar.gz

cd lua-cjson-2.1.0

修改Makefile文件 LUA_INCLUDE_DIR =   $(PREFIX)/include/luajit-2.0

make && make install

6.利用resty.redis模块写个demo

写个test.lua文件放在 /home/modules/lua-resty-redis/lib/,并赋予执行权限

 local redis = require "resty.redis"
local red = redis:new()
local json = require("cjson")
red:set_timeout() -- 1 sec
local ok, err = red:connect("127.0.0.1", )
local user = {} --table
user["name"] = "confused"
user["age"] =
local str = json.encode(user)
if not ok then
ngx.say("failed to connect: ", err)
return
end
red:init_pipeline() --利用管道操作
red:set("user", str)
red:get("user")
local results, err = red:commit_pipeline()
if not results then
ngx.say("failed to commit the pipelined requests: ", err)
return
end
for i, res in ipairs(results) do
if type(res) == "table" then
if res[] == false then
ngx.say("failed to run command ", i, ": ", res[])
else
ngx.say("ok ", i, ": ", res[])
end
else
ngx.say("ok ", i, ": ", res)
end
end

7.访问链接

配置nginx.conf

在http块中增加一个变量

lua_package_path "/home/modules/lua-resty-redis/lib/?.lua;;";

增加一个location

location /test {
            default_type "text/html";
            content_by_lua_file /home/modules/lua-resty-redis/lib/test.lua;
         }
重启nginx killall nginx && /usr/local/nginx/sbin/nginx

利用curl访问 curl localhost/test

你会得到ok 1: OK ok 2: {"name":"confused","age":24}

说明你成功了

结语

这只是自己的一个调研,准备用nginx+lua写通知接口(处理简单数据的读写,存储在redis),效率很高,可以看下openresty的性能评测,此篇作为入门篇,若有错误,望指正

参考链接

https://openresty.org/cn/

https://github.com/openresty/lua-resty-redis

上一篇:nginx+lua+redis实现灰度发布_test


下一篇:[.net 面向对象编程基础] (12) 面向对象三大特性——继承