nginx+lua+redis高並發應用建設

來源:互聯網
上載者:User

標籤:

ngx_lua將lua嵌nginx,讓nginx運行lua指令碼。高並發,非堵塞過程中的各種請求。

url要求nginxserver,然後lua查詢redis,返回json資料。


一.安裝lua-nginx-module

參見《LNMLGC》架構


二.安裝redis2-nginx-module模組

get https://github.com/openresty/echo-nginx-module
get https://github.com/openresty/redis2-nginx-module
get https://github.com/agentzh/set-misc-nginx-module.git

#
./configure --prefix=/usr/local/nginx   \
--with-debug                            \
--with-http_stub_status_module          \
--with-http_ssl_module                  \
--with-pcre=../pcre-8.21                \
--add-module=../ngx_devel_kit-0.2.19    \
--add-module=../lua-nginx-module-0.9.8  \
--add-module=../echo-nginx-module       \
--add-module=../redis2-nginx-module     \
--add-module=../set-misc-nginx-module

# make
# make install

三.安裝lua-redis-parser
# git clone https:
//github.com/agentzh/lua-redis-parser.git
# export LUA_INCLUDE_DIR=/usr/local/include/luajit-2.0
# make CC=gcc
# make install CC=gcc

四.安裝json
# wget http:
//files.luaforge.net/releases/json/json/0.9.50/json4lua-0.9.50.zip
# unzip json4lua-0.9.50.zip
# cp json4lua-0.9.50/json/json.lua /usr/local/lua/lib/

五.安裝redis-lua
# git clone https:
//github.com/nrk/redis-lua.git
# cp redis-lua/src/redis.lua /usr/local/lua/lib/

六.配置
...http {    ...    upstream redis_pool {        server localhost:6379;        keepalive 1024 single;        //定義串連池大小,當串連數達到此數後。興許的串連為短串連    }    server {        ...        location /get_redis{            #internal;            set_unescape_uri $key $arg_key;            redis2_query hgetall $key;            redis2_pass redis_pool;        }        location /json {            content_by_lua_file conf/test.lua;        }    }}

# vi test.lua,放在nginx.conf同檔案夾下
local json = require("json")local parser = require("redis.parser")local res = ngx.location.capture("/get_redis",{  args = { key = ngx.var.arg_key }})if res.status == 200 then  reply = parser.parse_reply(res.body)  value = json.encode(reply)  ngx.say(value)  a = json.decode(value)  ngx.say(a[2])end

七.測試
# redis-cli
127.0.0.1:6379> HMSET ttlsa www www.ttlsa.com mail mail.ttlsa.com
OK
127.0.0.1:6379> hgetall ttlsa
1) "www"
2) "www.ttlsa.com"
3) "mail"
4) "mail.ttlsa.com"

# curl http://localhost/json?

key=ttlsa

["www","www.ttlsa.com","mail","mail.ttlsa.com"]

www.ttlsa.com

nginx+lua+redis高並發應用建設

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.