elasticsearch入門系列">elasticsearch入門系列
848
2022-05-30
Lua的模塊開發(fā)
在實(shí)際的開發(fā)過程中,不可能把所有的LUA代碼寫在一個lua的文件中,通常的做法將特定功能的放在一個lua的文件中,即用LUA模塊開發(fā)。在lualib目錄下,默認(rèn)有以下的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導(dǎo)入要暴露的函數(shù),實(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()
通過要求(“模塊名”)來加載模塊,如果是多級目錄,則需要通過要求(“目錄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-基于cosocket API的ngx_lua的Lua Redis客戶端驅(qū)動程序
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后,通過設(shè)置一對鍵值對(味精,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:世界你好
lua_resty_redis支持所有的Redis的指令,本身的Redis就支持LUA語言操作。所以lua_resty_redis模塊能夠提高所有的Redis的操作的功能。
在很多時候,Redis是設(shè)置了密碼的,連接時,如果需要驗(yàn)證密碼,需要添加本地資源,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)注的官方文檔https://github.com/openresty/lua-resty-redis
狀語從句:開濤的博客http://jinnianshilongnian.iteye.com/blog/2187328
方志朋簡介:SpringCloud中國社區(qū)聯(lián)合創(chuàng)始人,博客訪問量突破一千萬,愛好開源,熱愛分享,活躍于各大社區(qū),保持著非常強(qiáng)的學(xué)習(xí)驅(qū)動力,終身學(xué)習(xí)踐行者,終身學(xué)習(xí)受益者。目前就職于國內(nèi)某家知名互聯(lián)網(wǎng)保險公司,擔(dān)任DEVOPS工程師,對微服務(wù)領(lǐng)域和持續(xù)集成領(lǐng)域研究較深,精通微服務(wù)框架SpringCloud
Redis
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。