溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Openresty中如何實現(xiàn)模塊開發(fā)以及連接Redis

發(fā)布時間:2021-12-07 15:13:28 來源:億速云 閱讀:334 作者:小新 欄目:大數(shù)據(jù)

這篇文章給大家分享的是有關(guān)Openresty中如何實現(xiàn)模塊開發(fā)以及連接Redis的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

Lua模塊開發(fā)

在實際的開發(fā)過程中,不可能把所有的lua代碼寫在一個lua文件中,通常的做法將特定功能的放在一個lua文件中,即用lua模塊開發(fā)。在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模塊

現(xiàn)在來簡單的開發(fā)一個lua模塊:

vim /usr/example/lualib/module1.lua

在module1.lua文件加上以下的代碼:

local count = 0  
local function hello()  
   count = count + 1  
   ngx.say("count : ", count)  
end  

local _M = {  
   hello = hello  
}  

return _M

開發(fā)時將所有數(shù)據(jù)做成局部變量/局部函數(shù);通過 _M導出要暴露的函數(shù),實現(xiàn)模塊化封裝。

在/usr/example/lua目錄下創(chuàng)建一個test_module_1.lua 文件,在該文件中引用上面的module1.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://116.196.177.123/lua_module_1,瀏覽器顯示:

count : 1
count : 2
count : 3

...

安裝redis

linux下安裝:
cd /usr/servers

$ wget http://download.redis.io/releases/redis-3.2.6.tar.gz
$ tar xzf redis-3.2.6.tar.gz
$ cd redis-3.2.6
$ make

啟動redis:

nohup /usr/servers/redis-3.2.6/src/redis-server  /usr/servers/redis-3.2.6/redis.conf &

查看是否啟動:

ps -ef |grep redis

終端顯示:

root     20985 14268  0 18:49 pts/0    00:00:00 /usr/servers/redis-3.2.6/src/redis-server 127.0.0.1:6379

可見redis已經(jīng)啟動。

lua連接redis

lua_resty_redis模塊地址:https://github.com/openresty/lua-resty-redis

lua-resty-redis - Lua redis client driver for the ngx_lua based on the cosocket API

lua_resty_redis 它是一個基于cosocket API的為ngx_lua模塊提供Lua redis客戶端的驅(qū)動。

創(chuàng)建一個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 = "127.0.0.1"  
local port = 6379  
local ok, err = red:connect(ip, port)  
if not ok then  
    ngx.say("connect to redis error : ", err)  
    return close_redis(red)  
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://116.196.177.123/lua_redis_basic

瀏覽器顯示:

msg : hello world

lua_resty_redis支持所有的redis指令,本身Redis就支持lua語言操作。所以lua_resty_redis模塊能夠提高所有的redis操作的功能。

在很多時候,Redis是設(shè)置了口令的,連接時,如果需要驗證口令,需要添加 local res, err = red:auth(“foobared”),示例代碼如下:

  local redis = require "resty.redis"
    local red = redis:new()

    red:set_timeout(1000) -- 1 sec

    local ok, err = red:connect("127.0.0.1", 6379)
    if not ok then
        ngx.say("failed to connect: ", err)
        return
    end

    local res, err = red:auth("foobared")
    if not res then
        ngx.say("failed to authenticate: ", err)
        return
    end

感謝各位的閱讀!關(guān)于“Openresty中如何實現(xiàn)模塊開發(fā)以及連接Redis”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI