Skip to content

Commit

Permalink
整合nginx-lua-redis2 and nginx-lua-mysql
Browse files Browse the repository at this point in the history
为动态调控路由技术准备;
tengine的监控检查考虑使用。
  • Loading branch information
supermy committed Apr 15, 2015
1 parent 9442ab8 commit abb00f1
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 61 deletions.
82 changes: 70 additions & 12 deletions web+app/mynginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,29 @@ RUN sed -i '1a \
deb-src http://mirrors.163.com/debian/ wheezy main non-free contrib \n \
deb-src http://mirrors.163.com/debian/ wheezy-proposed-updates main contrib non-free \n \
deb-src http://mirrors.163.com/debian-security/ wheezy/updates main contrib non-free \n \
deb http://ftp.sjtu.edu.cn/debian/ wheezy main non-free contrib \n \
deb http://ftp.sjtu.edu.cn/debian/ wheezy-proposed-updates main contrib non-free \n \
deb http://ftp.sjtu.edu.cn/debian-security/ wheezy/updates main contrib non-free \n \
deb-src http://ftp.sjtu.edu.cn/debian/ wheezy main non-free contrib \n \
deb-src http://ftp.sjtu.edu.cn/debian/ wheezy-proposed-updates main contrib non-free \n \
deb-src http://ftp.sjtu.edu.cn/debian-security/ wheezy/updates main contrib non-free \n \
' /etc/apt/sources.list

RUN cat /etc/apt/sources.list
# sohu 更新服务器:
#RUN sed -i '1a \
# deb http://mirrors.sohu.com/debian/ wheezy main non-free contrib \
# deb http://mirrors.sohu.com/debian/ wheezy-proposed-updates main non-free contrib \
# deb http://mirrors.sohu.com/debian-security/ wheezy/updates main contrib non-free \n \
# deb-src http://mirrors.sohu.com/debian/ wheezy main non-free contrib \
# deb-src http://mirrors.sohu.com/debian/ wheezy-proposed-updates main non-free contrib \
# deb-src http://mirrors.sohu.com/debian-security/ wheezy/updates main contrib non-free \n \
# ' /etc/apt/sources.list

# deb http://ftp.sjtu.edu.cn/debian/ wheezy main non-free contrib \n \
# deb http://ftp.sjtu.edu.cn/debian/ wheezy-proposed-updates main contrib non-free \n \
# deb http://ftp.sjtu.edu.cn/debian-security/ wheezy/updates main contrib non-free \n \
# deb-src http://ftp.sjtu.edu.cn/debian/ wheezy main non-free contrib \n \
# deb-src http://ftp.sjtu.edu.cn/debian/ wheezy-proposed-updates main contrib non-free \n \
# deb-src http://ftp.sjtu.edu.cn/debian-security/ wheezy/updates main contrib non-free \n \

RUN apt-get -qq update
RUN cat /etc/apt/sources.list

RUN apt-get -qqy install gcc libpcre3 libpcre3-dev openssl libssl-dev make wget libreadline-dev libncurses-dev graphicsmagick
RUN apt-get -qqy update && \
apt-get -qqy install gcc libpcre3 libpcre3-dev openssl libssl-dev make wget libreadline-dev libncurses-dev graphicsmagick

WORKDIR /tmp
RUN wget http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
Expand All @@ -36,10 +46,56 @@ RUN tar zxf lua-5.1.5.tar.gz
WORKDIR /tmp/lua-5.1.5
RUN make linux && make install

#配置 openrestry
WORKDIR /tmp
RUN wget http://luajit.org/download/LuaJIT-2.0.3.tar.gz
RUN wget http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz
RUN wget http://openresty.org/download/ngx_openresty-1.7.10.1.tar.gz

# mysql depends bit
RUN wget http://bitop.luajit.org/download/LuaBitOp-1.0.2.tar.gz

RUN tar zxf LuaJIT-2.0.3.tar.gz
RUN tar zxf lua-cjson-2.1.0.tar.gz
RUN tar zxf ngx_openresty-1.7.10.1.tar.gz
RUN tar zxf LuaBitOp-1.0.2.tar.gz


WORKDIR /tmp/LuaJIT-2.0.3
RUN make && make install

WORKDIR /tmp/lua-cjson-2.1.0
RUN make && make install

WORKDIR /tmp/ngx_openresty-1.7.10.1
RUN ./configure --prefix=/usr/local/openresty --with-luajit && make && make install


WORKDIR /tmp/LuaBitOp-1.0.2
RUN make && make install


#配置 openrestry end


RUN echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf

WORKDIR /tmp/tengine-2.1.0
RUN cd /tmp/tengine-2.1.0 && ./configure --with-http_lua_module && make && make install
RUN \
cd /tmp/tengine-2.1.0 &&\
./configure \
--with-ld-opt='-Wl,-rpath,/usr/local/lib/' \
--add-module=/tmp/ngx_openresty-1.7.10.1/bundle/redis2-nginx-module-0.11/ \
--add-module=/tmp/ngx_openresty-1.7.10.1/bundle/ngx_devel_kit-0.2.19/ \
--add-module=/tmp/ngx_openresty-1.7.10.1/bundle/set-misc-nginx-module-0.28/ \
--add-module=/tmp/ngx_openresty-1.7.10.1/bundle/echo-nginx-module-0.57/ \
--add-module=/tmp/ngx_openresty-1.7.10.1/bundle/ngx_lua-0.9.15/ \
&& make && make install


ADD nginx.conf /usr/local/nginx/conf/nginx.conf


WORKDIR /root
RUN rm -rf /tmp/tengine-*
RUN rm -rf /tmp/lua-*
Expand All @@ -58,5 +114,7 @@ EXPOSE 80 443

CMD ["/etc/my_init.d/nginx.sh"]

#userage
#docker run -d -p 8080:80 --name test -v /home/utgard/www/:/data/www/ mynginx_app
# build
# docker build -t jamesmo/mynginx:2.1 .
# userage
# docker run -d -p 8080:80 --name test -v /home/utgard/www/:/data/www/ mynginx_web
10 changes: 10 additions & 0 deletions web+app/mynginx/b.sh
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
22 changes: 21 additions & 1 deletion web+app/mynginx/fig.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#启动web
web:
build: .
image: jamesmo/mynginx:2.1
links:
- myredis:myredis
- mydb:mydb
ports:
- "80:80"
volumes_from:
Expand All @@ -14,6 +17,23 @@ data:
- /Users/moyong/docker-share/www/:/data/www/
- /Users/moyong/docker-share/logs/web/:/usr/local/nginx/logs/
#- /Users/moyong/docker-share/conf/nginx/:/usr/local/nginx/conf/nginx.d/ #配置文件,fix.me

myredis:
image: redis:latest
ports:
- "6379:6379"


mydb:
image: mysql:latest
environment:
MYSQL_USER: java
MYSQL_PASSWORD: java
MYSQL_DATABASE: javatest
MYSQL_ROOT_PASSWORD: mysql
ports:
- "3306:3306"

# echo 'hello'>index.html
#docker run -d -p 8080:80 --name testnginx -v /home/utgard/www/:/data/www/ mynginx_app
#docker run -d -p 8080:80 --name testnginx -v /var/www/:/data/www/ mynginx_app
Expand Down
31 changes: 31 additions & 0 deletions web+app/mynginx/nginx.d/echo.conf
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!";

}
94 changes: 94 additions & 0 deletions web+app/mynginx/nginx.d/lua-mysql.conf
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';
}
79 changes: 79 additions & 0 deletions web+app/mynginx/nginx.d/lua-redis.conf
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;
}
4 changes: 4 additions & 0 deletions web+app/mynginx/nginx.d/lua.conf
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")';
}
Loading

0 comments on commit abb00f1

Please sign in to comment.