OpenResty第六編:OpenResty接続Redis及びモジュール開発

13104 ワード

Luaモジュール開発
実際の開発過程では、すべてのluaコードを1つのluaファイルに書くことはできません.通常、特定の機能を1つのluaファイルに配置し、luaモジュールで開発します.lualibディレクトリでは、デフォルトでは以下のluaモジュールがあります.
lualib/
├── cjson.so
├── ngx
│   ├── balancer.lua
│   ├── ocsp.lua
│   ├── re.lua
│   ├── semaphore.lua
│   ├── ssl
│   │   └── session.lua
│   └── ssl.lua
├── rds
│   └── parser.so
├── redis
│   └── parser.so
└── resty
    ├── aes.lua
    ├── core
    │   ├── base64.lua
    │   ├── base.lua
    │   ├── ctx.lua
    │   ├── exit.lua
    │   ├── hash.lua
    │   ├── misc.lua
    │   ├── regex.lua
    │   ├── request.lua
    │   ├── response.lua
    │   ├── shdict.lua
    │   ├── time.lua
    │   ├── uri.lua
    │   ├── var.lua
    │   └── worker.lua
    ├── core.lua
    ├── dns
    │   └── resolver.lua
    ├── limit
    │   ├── conn.lua
    │   ├── req.lua
    │   └── traffic.lua
    ├── lock.lua
    ├── lrucache
    │   └── pureffi.lua
    ├── lrucache.lua
    ├── md5.lua
    ├── memcached.lua
    ├── mysql.lua
    ├── random.lua
    ├── redis.lua
    ├── sha1.lua
    ├── sha224.lua
    ├── sha256.lua
    ├── sha384.lua
    ├── sha512.lua
    ├── sha.lua
    ├── string.lua
    ├── upload.lua
    ├── upstream
    │   └── healthcheck.lua
    └── websocket
        ├── client.lua
        ├── protocol.lua
        └── server.lua

これらのモジュールを使用する前に、nginxのプロファイルnginxが必要である.confのhttpモジュールには、次の構成が追加されています.
 lua_package_path "/usr/example/lualib/?.lua;;";  #lua     
 lua_package_cpath "/usr/example/lualib/?.so;;";  #c     

次にluaモジュールを簡単に開発します:vim/usr/example/lualib/module 1.luaはmodule 1.luaファイルには、次のコードが追加されています.
local count = 0  
local function hello()  
   count = count + 1  
   ngx.say("count : ", count)  
end  
  
local _M = {  
   hello = hello  
}  
return _M  

開発時にすべてのデータを局所変数/局所関数にする.パス_Mは露出する関数を導出し,モジュール化パッケージを実現する./usr/example/luaディレクトリの下にtest_を作成module_1.上のmodule 1を参照するluaファイル.luaファイル.vim/usr/example/lua/test_module_1.lua
local module1 = require("module1")    
module1.hello() 

モジュールはrequire(「モジュール名」)でロードされ、マルチレベルディレクトリの場合はrequire(「ディレクトリ1.ディレクトリ2.モジュール名」)でロードする必要があります./user/example/example.confには、次の構成が追加されています.
location /lua_module_1 {  
    default_type 'text/html';  
    lua_code_cache on;  
    content_by_lua_file /usr/example/lua/test_module_1.lua;  
}

ブラウザへのアクセス:http://192.168.100.65/lua_module_1
lua接続redis
lua_resty_redisモジュールアドレス:https://github.com/openresty/lua-resty-redis lua_resty_redis cosocket APIに基づくngx_luaモジュールはLua redisクライアントの駆動を提供する.
test_を作成redis_basic.luaファイル
vim/usr/example/lua/test_redis_basic.lua
 local function close_redis(red)  
    if not red then  
        return  
    end  
  
    local pool_max_idle_time = 10000 --    
    local pool_size = 100 --       
    local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)  
    if not ok then  
        ngx.say("set keepalive error : ", err)  
    end  
end    
  
local redis = require("resty.redis")  
local red = redis:new()  
 
red:set_timeout(1000)  

local ip = "47.74.214.10"  
local port = 6679  
local ok, err = red:connect(ip, port)  
if not ok then  
    ngx.say("connect to redis error : ", err)  
    return close_redis(red)  
end 

local res, err = red:auth("Indo@#1a0e633eb43693102ec")
if not res then
	ngx.say("failed to authenticate: ", err)
	return
end 

ok, err = red:set("msg", "hello world")  
if not ok then  
    ngx.say("set msg error : ", err)  
    return close_redis(red)  
end  
 
local resp, err = red:get("msg")  
if not resp then  
    ngx.say("get msg error : ", err)  
    return close_redis(red)  
end  

if resp == ngx.null then  
    resp = ''  
end  
ngx.say("msg : ", resp)  
  
close_redis(red)  

上のコードは簡単で、接続プールでRedisを接続し、redisを接続した後、setの一対のキー値対(msg,helloword)でredisに接続し、get(msg)、ngxを通過する.say()はブラウザに戻ります.
vim/usr/example/example.conf、次の構成コードを追加します.
location /lua_redis_basic {  
    default_type 'text/html';  
    lua_code_cache on;  
    content_by_lua_file /usr/example/lua/test_redis_basic.lua;  
 }  

ブラウザアクセス:http://192.168.100.65/lua_redis_basic response: msg : hello world
lua_resty_redisはすべてのredis命令をサポートし,それ自体Redisはlua言語操作をサポートする.だからlua_resty_redisモジュールは、すべてのredis操作の機能を向上させることができる.
に注意
多くの場合、Redisはパスワードが設定されており、接続時にパスワードを検証する必要がある場合はlocal resを追加する必要があり、err=red:auth(「Indo@#1 a 0 e 633 eb 43693102 ec」)
詳細については、公式ドキュメントを参照してください.https://github.com/openresty/lua-resty-redisと開涛のブログhttp://jinnianshilongnian.iteye.com/blog/2187328