lua開發--lua模塊和redis

lua模塊開發

在實際開發中,不可能把所有代碼寫到一個大而全的lua文件中,需要進行分模塊開發;而且模塊化是高性能Lua應用的關鍵。使用require第一次導入模塊後,所有Nginx 進程全局共享模塊的數據和代碼,每個Worker進程需要時會得到此模塊的一個副本(Copy-On-Write),即模塊可以認爲是每Worker進程共享而不是每Nginx Server共享;另外注意之前我們使用init_by_lua中初始化的全局變量是每請求複製一個;如果想在多個Worker進程間共享數據可以使用ngx.shared.DICT或如Redis之類的存儲。

在/usr/example/lualib中已經提供了大量第三方開發庫如cjson、redis客戶端、mysql客戶端:

cjson.so

resty/

   aes.lua

   core.lua

   dns/

   lock.lua

   lrucache/

   lrucache.lua

   md5.lua

   memcached.lua

   mysql.lua

   random.lua

   redis.lua

   ……

需要注意在使用前需要將庫在nginx.conf中導入:

   #lua模塊路徑,其中”;;”表示默認搜索路徑,默認到/usr/servers/nginx下找  
    lua_package_path "/usr/example/lualib/?.lua;;";  #lua 模塊  
    lua_package_cpath "/usr/example/lualib/?.so;;";  #c模塊   

使用方式是在lua中通過如下方式引入

    local cjson = require(“cjson”)  
    local redis = require(“resty.redis”)   

接下來我們來開發一個簡單的lua模塊。

vim /usr/example/lualib/module1.lua  
    local count = 0  
    local function hello()  
       count = count + 1  
       ngx.say("count : ", count)  
    end  

    local _M = {  
       hello = hello  
    }  

    return _M  

開發時將所有數據做成局部變量/局部函數;通過 _M導出要暴露的函數,實現模塊化封裝。

接下來創建test_module_1.lua.

vim /usr/example/lua/test_module_1.lua  
  local module1 = require("module1")  

    module1.hello()  

使用 local var = require(“模塊名”),該模塊會到lua_package_path和lua_package_cpath聲明的的位置查找我們的模塊,對於多級目錄的使用require(“目錄1.目錄2.模塊名”)加載。

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.1.2/lua_module_1進行測試,會得到類似如下的數據,count會遞增

count : 1

count :2

……

count :N

此時可能發現count一直遞增,假設我們的worker_processes 2,我們可以通過kill -9 nginx worker process殺死其中一個Worker進程得到count數據變化。

假設我們創建了vim /usr/example/lualib/test/module2.lua模塊,可以通過local module2 = require(“test.module2”)加載模塊

基本的模塊開發就完成了,如果是隻讀數據可以通過模塊中聲明local變量存儲;如果想在每Worker進程共享,請考慮競爭;如果要在多個Worker進程間共享請考慮使用ngx.shared.DICT或如Redis存儲。


常見的lua開發庫

對於開發來說需要有好的生態開發庫來輔助我們快速開發,而Lua中也有大多數我們需要的第三方開發庫如Redis、Memcached、Mysql、Http客戶端、JSON、模板引擎等。

一些常見的Lua庫可以在github上搜索,https://github.com/search?utf8=%E2%9C%93&q=lua+resty

Redis客戶端

lua-resty-redis是爲基於cosocket API的ngx_lua提供的Lua redis客戶端,通過它可以完成Redis的操作。默認安裝OpenResty時已經自帶了該模塊,使用文檔可參考https://github.com/openresty/lua-resty-redis

在測試之前請啓動Redis實例:

nohup /usr/servers/redis-2.8.19/src/redis-server  /usr/servers/redis-2.8.19/redis_6660.conf &

基本操作

編輯test_redis_baisc.lua

    local function close_redis(red)  
        if not red then  
            return  
        end  
        local ok, err = red:close()  
        if not ok then  
            ngx.say("close redis error : ", err)  
        end  
    end  

    local redis = require("resty.redis")  

    --創建實例  
    local red = redis:new()  
    --設置超時(毫秒)  
    red:set_timeout(1000)  
    --建立連接  
    local ip = "127.0.0.1"  
    local port = 6660  
    local ok, err = red:connect(ip, port)  
    if not ok then  
        ngx.say("connect to redis error : ", err)  
        return close_redis(red)  
    end  
    --調用API進行處理  
    ok, err = red:set("msg", "hello world")  
    if not ok then  
        ngx.say("set msg error : ", err)  
        return close_redis(red)  
    end  

    --調用API獲取數據  
    local resp, err = red:get("msg")  
    if not resp then  
        ngx.say("get msg error : ", err)  
        return close_reedis(red)  
    end  
    --得到的數據爲空處理  
    if resp == ngx.null then  
        resp = ''  --比如默認值  
    end  
    ngx.say("msg : ", resp)  

    close_redis(red)  

基本邏輯很簡單,要注意此處判斷是否爲nil,需要跟ngx.null比較。

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;  
    }  

3、訪問如http://192.168.1.2/lua_redis_basic進行測試,正常情況得到如下信息

msg : hello world

連接池

建立TCP連接需要三次握手而釋放TCP連接需要四次握手,而這些往返時延僅需要一次,以後應該複用TCP連接,此時就可以考慮使用連接池,即連接池可以複用連接。

我們只需要將之前的close_redis函數改造爲如下即可:

    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  

即設置空閒連接超時時間防止連接一直佔用不釋放;設置連接池大小來複用連接。

此處假設調用red:set_keepalive(),連接池大小通過nginx.conf中http部分的如下指令定義:

#默認連接池大小,默認30

lua_socket_pool_size 30;

#默認超時時間,默認60s

lua_socket_keepalive_timeout 60s;

注意:

1、連接池是每Worker進程的,而不是每Server的;

2、當連接超過最大連接池大小時,會按照LRU算法回收空閒連接爲新連接使用;

3、連接池中的空閒連接出現異常時會自動被移除;

4、連接池是通過ip和port標識的,即相同的ip和port會使用同一個連接池(即使是不同類型的客戶端如Redis、Memcached);

5、連接池第一次set_keepalive時連接池大小就確定下了,不會再變更;

5、cosocket的連接池http://wiki.nginx.org/HttpLuaModule#tcpsock:setkeepalive

pipeline

pipeline即管道,可以理解爲把多個命令打包然後一起發送;MTU(Maxitum Transmission Unit 最大傳輸單元)爲二層包大小,一般爲1500字節;而MSS(Maximum Segment Size 最大報文分段大小)爲四層包大小,其一般是1500-20(IP報頭)-20(TCP報頭)=1460字節;因此假設我們執行的多個Redis命令能在一個報文中傳輸的話,可以減少網絡往返來提高速度。因此可以根據實際情況來選擇走pipeline模式將多個命令打包到一個報文發送然後接受響應,而Redis協議也能很簡單的識別和解決粘包。

1、修改之前的代碼片段

    red:init_pipeline()  
    red:set("msg1", "hello1")  
    red:set("msg2", "hello2")  
    red:get("msg1")  
    red:get("msg2")  
    local respTable, err = red:commit_pipeline()  

    --得到的數據爲空處理  
    if respTable == ngx.null then  
        respTable = {}  --比如默認值  
    end  

    --結果是按照執行順序返回的一個table  
    for i, v in ipairs(respTable) do  
       ngx.say("msg : ", v, "<br/>")  
    end  

通過init_pipeline()初始化,然後通過commit_pipieline()打包提交init_pipeline()之後的Redis命令;返回結果是一個lua table,可以通過ipairs循環獲取結果;

2、配置相應location,測試得到的結果

msg : OK
msg : OK
msg : hello1
msg : hello2

3、Redis Lua腳本

利用Redis單線程特性,可以通過在Redis中執行Lua腳本實現一些原子操作。如之前的red:get(“msg”)可以通過如下兩種方式實現:

1、直接eval:

local resp, err = red:eval("return redis.call('get', KEYS[1])", 1, "msg"); 

2、script load然後evalsha SHA1 校驗和,這樣可以節省腳本本身的服務器帶寬:

local sha1, err = red:script("load",  "return redis.call('get', KEYS[1])");  
if not sha1 then  
   ngx.say("load script error : ", err)  
   return close_redis(red)  
end  
ngx.say("sha1 : ", sha1, "<br/>")  
local resp, err = red:evalsha(sha1, 1, "msg"); 

首先通過script load導入腳本並得到一個sha1校驗和(僅需第一次導入即可),然後通過evalsha執行sha1校驗和即可,這樣如果腳本很長通過這種方式可以減少帶寬的消耗。

此處僅介紹了最簡單的redis lua腳本,更復雜的請參考官方文檔學習使用。

另外Redis集羣分片算法該客戶端沒有提供需要自己實現,當然可以考慮直接使用類似於Twemproxy這種中間件實現。

Memcached客戶端使用方式和本文類似,本文就不介紹了。

轉載 http://jinnianshilongnian.iteye.com/blog/2187328

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章