lua resty模块利用 实现(转)

http://hi.baidu.com/learsu/item/d64977e85601e70c8d3ea8be

使用模块

httpLuaModule http://wiki.nginx.org/HttpLuaModule

lua-resty-memcached https://github.com/agentzh/lua-resty-memcached

lua-resty-mysql https://github.com/agentzh/lua-resty-mysql

lua-resty-redis https://github.com/agentzh/lua-resty-redis

lua-cjson http://www.kyne.com.au/~mark/software/lua-cjson.php

nginx配置文件

mysql使用cobar做分布式。cobar前面有nginx tcp model(也可使用haproxy tcp proxy)。只公布测试conf。用到生产环境请先修改。

worker_processes? 4;

events {

worker_connections? 10240;

}

http {

include?????? mime.types;

default_type? text/html;

sendfile??????? on

keepalive_timeout? 60;

server {

listen?????? 80;

server_name? localhost;

location /get {

content_by_lua '

local id = tonumber(ngx.var.arg_id) or 0

local res = ngx.location.capture("/get_memc?id="..id)

if res.status == 200 then

ngx.print(res.body)

end

';

}

location /get_memc {

content_by_lua '

local id = tonumber(ngx.var.arg_id) or 0

mod_id = id % 1024

local memcache_host = "192.168.0.1"

local memcache_port = 11211

if mod_id <= 512 then

memcache_host = "192.168.0.1"

memcache_port = 11211

elseif mod_id > 512 and mod_id <= 768 then

memcache_host = "192.168.0.2"

memcache_port = 11211

else

memcache_host = "192.168.0.3"

memcache_port = 11211

end

local memcached = require "resty.memcached"

local memc, err = memcached:new()

if not memc then

local res = ngx.location.capture("/get_mysql?id="..id)

if res.status == 200 then

ngx.print(res.body)

end

return

end

memc:set_timeout(1000) -- 1 sec

local ok, err = memc:connect(memcache_host, memcache_port)

if not ok then

local res = ngx.location.capture("/get_mysql?id="..id)

if res.status == 200 then

ngx.print(res.body)

end

return

end

local res, flags, err = memc:get(id)

if err then

local res = ngx.location.capture("/get_mysql?id="..id)

if res.status == 200 then

ngx.print(res.body)

end

return

end

if not res then

local res = ngx.location.capture("/get_mysql?id="..id)

if res.status == 200 then

ngx.print(res.body)

end

return

end

ngx.say("this info from memcache:", res)

memc:set_keepalive(0, 2048)

';

}

location /get_mysql {

content_by_lua '

local id = tonumber(ngx.var.arg_id) or 0

local mysql = require "resty.mysql"

local db, err = mysql:new()

if not db then

ngx.say("failed to instantiate mysql: ", err)

return

end

db:set_timeout(1000)

local ok, err, errno, sqlstate = db:connect{

host = "192.168.1.1",

port = 8066,

database = "test",

user = "root",

password = "123456",

max_packet_size = 1024 * 1024 }

if not ok then

ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)

return

end

res, err, errno, sqlstate =

db:query("select * from test where id="..id)

if not res then

ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")

return

end

local cjson = require "cjson"

ngx.say("this info from mysql :", cjson.encode(res))

local ok, err = db:set_keepalive(0, 512)

if not ok then

ngx.say("failed to set keepalive: ", err)

return

end

';

}

location /db2memc {

content_by_lua '

local id = tonumber(ngx.var.arg_id) or 0

local mysql = require "resty.mysql"

local db, err = mysql:new()

if not db then

ngx.say("failed to instantiate mysql: ", err)

return

end

db:set_timeout(1000)

local ok, err, errno, sqlstate = db:connect{

host = "192.168.1.1",

port = 8066,

database = "test",

user = "root",

password = "123456",

max_packet_size = 1024 * 1024 }

if not ok then

ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)

return

end

res, err, errno, sqlstate =

db:query("select * from test where id="..id)

if not res then

ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")

return

end

local cjson = require "cjson"

ngx.say("this is select from DB : ", cjson.encode(res))

local ok, err = db:set_keepalive(0, 512)

if not ok then

ngx.say("failed to set keepalive: ", err)

return

end

local memcached = require "resty.memcached"

local memc, err = memcached:new()

if not memc then

ngx.say("failed to instantiate memc: ", err)

return

end

memc:set_timeout(1000) -- 1 sec

mod_id = id % 1024

local memcache_host = "192.168.0.1"

local memcache_port = 11211

if mod_id <= 512 then

memcache_host = "192.168.0.1"

memcache_port = 11211

elseif mod_id > 512 and mod_id <= 768 then

memcache_host = "192.168.0.2"

memcache_port = 11211

else

memcache_host = "192.168.0.3"

memcache_port = 11211

end

local ok, err = memc:connect(memcache_host, memcache_port)

if not ok then

ngx.say("failed to connect: ", err)

return

end

local ok, err = memc:set(id, cjson.encode(res))

if not ok then

ngx.say("failed to set memcache error: ", err)

return

end

local res, flags, err = memc:get(id)

if err then

ngx.say("failed to get memcache: ", err)

return

end

if not res then

ngx.say(id.." not found")

return

end

ngx.say("<br>this is from memcached "..id..": ", res)

memc:set_keepalive(0, 1024)

';

}

location /memc {

content_by_lua '

local cmd = ngx.var.arg_cmd or "set"

local id = tonumber(ngx.var.arg_id) or 0

local value = ngx.var.arg_value or "nui"

local memcached = require "resty.memcached"

local memc, err = memcached:new()

if not memc then

ngx.say("failed to instantiate memc: ", err)

return

end

mod_id = id % 1024

local memcache_host = "192.168.0.1"

local memcache_port = 11211

if mod_id <= 512 then

memcache_host = "192.168.0.1"

memcache_port = 11211

elseif mod_id > 512 and mod_id <= 768 then

memcache_host = "192.168.0.2"

memcache_port = 11211

else

memcache_host = "192.168.0.3"

memcache_port = 11211

end

memc:set_timeout(1000) -- 1 sec

local ok, err = memc:connect(memcache_host, memcache_port)

if not ok then

ngx.say("failed to connect: ", err)

return

end

if cmd == "set" then

local ok, err = memc:set(id, value)

if not ok then

ngx.say("failed to set memcache:", err)

return

end

ngx.say("set memcache successfull")

else

local res, flags, err = memc:get(id)

if err then

ngx.say("failed to get memcache: ", err)

return

end

if not res then

ngx.say("nui")

return

end

ngx.say(res)

end

memc:set_keepalive(0, 1024)

';

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值