forked from supermy/mytools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
整合nginx-lua-redis2 and nginx-lua-mysql
为动态调控路由技术准备; tengine的监控检查考虑使用。
- Loading branch information
Showing
9 changed files
with
395 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#docker build -t jamesmo/mynginx:2.1 . | ||
|
||
#http://192.168.59.103:8080/redisfoo | ||
#http://192.168.59.103:8080/redisget?key=one | ||
#http://192.168.59.103:8080/redisset?key=one&val=first_value | ||
|
||
#http://192.168.59.103:8080/lua-redis-test | ||
#http://192.168.59.103:8080/lua-mysql-test | ||
|
||
#192.168.0.119 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
location /hello1 { | ||
default_type 'text/plain'; | ||
|
||
echo "hello jamesmo!"; | ||
|
||
} | ||
|
||
#2. 异步请求其他echo请求 | ||
|
||
location /hello2 { | ||
default_type 'text/plain'; | ||
|
||
echo "hello 2222!"; | ||
|
||
echo_location_async /hello1; | ||
|
||
} | ||
|
||
#3. 输出GET请求参数,假设参数名是name,这里并对name参数进行解码 | ||
|
||
location /hello3 { | ||
default_type 'text/plain'; | ||
|
||
set_unescape_uri $name $arg_name; | ||
|
||
set_if_empty $name "None"; | ||
|
||
echo "hello, $name!"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
location /lua-mysql-test { | ||
content_by_lua ' | ||
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) -- 1 sec | ||
|
||
-- or connect to a unix domain socket file listened | ||
-- by a mysql server: | ||
-- local ok, err, errno, sqlstate = | ||
-- db:connect{ | ||
-- path = "/path/to/mysql.sock", | ||
-- database = "ngx_test", | ||
-- user = "ngx_test", | ||
-- password = "ngx_test" } | ||
|
||
local ok, err, errno, sqlstate = db:connect{ | ||
host = "192.168.59.103", | ||
port = 3306, | ||
database = "javatest", | ||
user = "java", | ||
password = "java", | ||
max_packet_size = 1024 * 1024 } | ||
|
||
if not ok then | ||
ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate) | ||
return | ||
end | ||
|
||
ngx.say("connected to mysql.") | ||
|
||
local res, err, errno, sqlstate = | ||
db:query("drop table if exists cats") | ||
if not res then | ||
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".") | ||
return | ||
end | ||
|
||
res, err, errno, sqlstate = | ||
db:query("create table cats " | ||
.. "(id serial primary key, " | ||
.. "name varchar(5))") | ||
if not res then | ||
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".") | ||
return | ||
end | ||
|
||
ngx.say("table cats created.") | ||
|
||
res, err, errno, sqlstate = | ||
db:query("insert into cats (name) " | ||
.. "values (\'Bob\'),(\'\'),(null)") | ||
if not res then | ||
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".") | ||
return | ||
end | ||
|
||
ngx.say(res.affected_rows, " rows inserted into table cats ", | ||
"(last insert id: ", res.insert_id, ")") | ||
|
||
-- run a select query, expected about 10 rows in | ||
-- the result set: | ||
res, err, errno, sqlstate = | ||
db:query("select * from cats order by id asc", 10) | ||
if not res then | ||
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".") | ||
return | ||
end | ||
|
||
local cjson = require "cjson" | ||
ngx.say("result: ", cjson.encode(res)) | ||
|
||
-- put it into the connection pool of size 100, | ||
-- with 10 seconds max idle timeout | ||
local ok, err = db:set_keepalive(10000, 100) | ||
if not ok then | ||
ngx.say("failed to set keepalive: ", err) | ||
return | ||
end | ||
|
||
-- or just close the connection right away: | ||
-- local ok, err = db:close() | ||
-- if not ok then | ||
-- ngx.say("failed to close: ", err) | ||
-- return | ||
-- end | ||
'; | ||
|
||
default_type 'text/plain'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
location /lua-redis-test { | ||
content_by_lua ' | ||
local redis = require "resty.redis" | ||
local red = redis:new() | ||
|
||
red:set_timeout(1000) -- 1 sec | ||
|
||
-- or connect to a unix domain socket file listened | ||
-- by a redis server: | ||
-- local ok, err = red:connect("unix:/path/to/redis.sock") | ||
|
||
local ok, err = red:connect("192.168.59.103", 6379) | ||
if not ok then | ||
ngx.say("failed to connect: ", err) | ||
return | ||
end | ||
|
||
ok, err = red:set("dog", "an animal") | ||
if not ok then | ||
ngx.say("failed to set dog: ", err) | ||
return | ||
end | ||
|
||
ngx.say("set result: ", ok) | ||
|
||
local res, err = red:get("dog") | ||
if not res then | ||
ngx.say("failed to get dog: ", err) | ||
return | ||
end | ||
|
||
if res == ngx.null then | ||
ngx.say("dog not found.") | ||
return | ||
end | ||
|
||
ngx.say("dog: ", res) | ||
|
||
red:init_pipeline() | ||
red:set("cat", "Marry") | ||
red:set("horse", "Bob") | ||
red:get("cat") | ||
red:get("horse") | ||
local results, err = red:commit_pipeline() | ||
if not results then | ||
ngx.say("failed to commit the pipelined requests: ", err) | ||
return | ||
end | ||
|
||
for i, res in ipairs(results) do | ||
if type(res) == "table" then | ||
if not res[1] then | ||
ngx.say("failed to run command ", i, ": ", res[2]) | ||
else | ||
-- process the table value | ||
end | ||
else | ||
-- process the scalar value | ||
end | ||
end | ||
|
||
-- put it into the connection pool of size 100, | ||
-- with 10 seconds max idle time | ||
local ok, err = red:set_keepalive(10000, 100) | ||
if not ok then | ||
ngx.say("failed to set keepalive: ", err) | ||
return | ||
end | ||
|
||
-- or just close the connection right away: | ||
-- local ok, err = red:close() | ||
-- if not ok then | ||
-- ngx.say("failed to close: ", err) | ||
-- return | ||
-- end | ||
'; | ||
|
||
default_type text/html; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
location /hello { | ||
default_type 'text/plain'; | ||
content_by_lua 'ngx.say("hello, lua")'; | ||
} |
Oops, something went wrong.