Skip to content

Commit

Permalink
完成基线版本-nginx+lua 优化
Browse files Browse the repository at this point in the history
  • Loading branch information
supermy committed Apr 15, 2015
1 parent abb00f1 commit a08d590
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 58 deletions.
3 changes: 2 additions & 1 deletion common/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ docker build -t jamesmo/kafka:0.8 mykafka

docker build -t jamesmo/rabbitmq_base:3.5 myrabbitmq
docker build -t jamesmo/rabbitmq:3.5 myrabbitmq/rabbitmq
#docker build -t jamesmo/rabbitmq:3.5 myredis

docker build -t jamesmo/myflume:latest myflume

docker build -t jamesmo/myredis:3.5 myredis
docker build -t jamesmo/mysql:latest mysql

docker build -t jamesmo/storm_base:0.9.3 mystorm/storm
docker build -t jamesmo/storm-nimbus:0.9.3 mystorm/storm-nimbus
Expand Down
31 changes: 1 addition & 30 deletions common/mysql/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,7 @@ max_allowed_packet = 16M \n\
wait_timeout = 500 \n\
interactive_timeout = 500 \n\
connect_timeout = 20 \n\
\n\
back_log = 512 \n\
max_connections = 2048 \n\
max_connect_errors = 128 \n\
open_files_limit = 8192 \n\
\n\
key_buffer_size = 1024M \n\
\n\
sort_buffer_size = 32M \n\
read_buffer_size = 32M \n\
join_buffer_size = 32M \n\
read_rnd_buffer_size = 8M \n\
bulk_insert_buffer_size = 32M \n\
\n\
thread_cache_size = 64 \n\
thread_stack = 512k \n\
\n\
query_cache_limit = 4M \n\
query_cache_size = 128M \n\
query_cache_min_res_unit= 2k \n\
\n\
#table_cache = 2048 \n\
tmp_table_size = 256M \n\
max_heap_table_size = 256M \n\
max_tmp_tables = 512 \n\
bulk_insert_buffer_size = 16M \n\
skip-name-resolve \n\
slow_query_log \n\
long_query_time = 0.1 \n\
' /etc/mysql/my.cnf
' /etc/mysql/my.cnf

RUN more /etc/mysql/my.cnf
12 changes: 12 additions & 0 deletions web+app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
web(极限性能挖掘,可快速扩充)
====================

介绍
---------------------
nginx(tengine)+tomcat+mysql+memcached 集群
一键构造,一键启动,一键数据初始化。

> nginx 整合HttpRedis2Module/lua-resty-redis完成,http方式访问redis;
>
>
> ## end
2 changes: 2 additions & 0 deletions web+app/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docker build -t jamesmo/mynginx:2.1 mynginx
docker build -t jamesmo/mytomcat:7 mytomcat
22 changes: 14 additions & 8 deletions web+app/fig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
#http://192.168.59.103/java/myweb/
#http://192.168.59.103/java/dbtest/
web:
image: mynginx_web
image: jamesmo/mynginx:2.1
links:
- mytomcat1
- mytomcat2
- myredis
ports:
- "80:80"
volumes_from:
Expand All @@ -25,25 +26,25 @@ data:

#启动tomcat
mytomcat1:
image: mytomcat_app
image: jamesmo/mytomcat:7
environment:
TOMCAT_PASS: admin
links:
- dbinit
- db:mysql_base
- db:mysql
- memcache1
- memcache2
ports:
- "8080:8080"

#启动tomcat
mytomcat2:
image: mytomcat_app
image: jamesmo/mytomcat:7
environment:
TOMCAT_PASS: admin
links:
- dbinit
- db:mysql_base
- db:mysql
- memcache1
- memcache2
ports:
Expand All @@ -65,9 +66,14 @@ memcache2:
ports:
- "11211"

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


db:
image: mysql_base
image: jamesmo/mysql:latest
environment:
MYSQL_USER: java
MYSQL_PASSWORD: java
Expand All @@ -77,10 +83,10 @@ db:
- "3306"

dbinit:
image: mysql_base
image: jamesmo/mysql:latest
entrypoint: /bin/bash
volumes:
- .:/host
command: -c "sleep 8; mysql -u java --password=java -h mysql javatest < /host/init.sql; exit 0"
links:
- db:mysql_base
- db:mysql
4 changes: 4 additions & 0 deletions web+app/mynginx/nginx.d/lua.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
location /hello {
default_type 'text/plain';

access_by_lua_file "/usr/local/nginx/conf/nginx.d/redis-ip.lua";


content_by_lua 'ngx.say("hello, lua")';
}
20 changes: 10 additions & 10 deletions web+app/mynginx/nginx.d/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ http {

gzip on;

# upstream backendjava {
# server mytomcat1:8080 weight=10;
# server mytomcat2:8080 weight=10;
# session_sticky;
# }
upstream backendjava {
server mytomcat1:8080 weight=10;
server mytomcat2:8080 weight=10;
session_sticky;
}


# set search paths for pure Lua external libraries (';;' is the default path):
Expand All @@ -73,11 +73,11 @@ http {
}


# location /java {
# proxy_pass http://backendjava/;
# root /data/www;
# index index.html index.htm index.jsp;
# }
location /java {
proxy_pass http://backendjava/;
root /data/www;
index index.html index.htm index.jsp;
}

#error_page 404 /404.html;

Expand Down
44 changes: 44 additions & 0 deletions web+app/mynginx/nginx.d/redis-ip.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
ip_bind_time = 300 --封禁IP时间
ip_time_out = 60 --指定ip访问频率时间段
connect_count = 100 --指定ip访问频率计数最大值

--连接redis
local redis = require "resty.redis"
local cache = redis.new()
local ok , err = cache.connect(cache,"192.168.59.103","6379")
cache:set_timeout(60000)

--如果连接失败,跳转到脚本结尾
if not ok then
goto A
end

--查询ip是否在封禁段内,若在则返回403错误代码
--因封禁时间会大于ip记录时间,故此处不对ip时间key和计数key做处理
is_bind , err = cache:get("bind_"..ngx.var.remote_addr)
if is_bind == 1 then
ngx.exit(403)
goto A
end

start_time , err = cache:get("time_"..ngx.var.remote_addr)
ip_count , err = cache:get("count_"..ngx.var.remote_addr)

--如果ip记录时间大于指定时间间隔或者记录时间或者不存在ip时间key则重置时间key和计数key
--如果ip时间key小于时间间隔,则ip计数+1,且如果ip计数大于ip频率计数,则设置ip的封禁key为1
--同时设置封禁key的过期时间为封禁ip的时间
if start_time == ngx.null or os.time() - start_time > ip_time_out then
res , err = cache:set("time_"..ngx.var.remote_addr , os.time())
res , err = cache:set("count_"..ngx.var.remote_addr , 1)
else
ip_count = ip_count + 1
res , err = cache:incr("count_"..ngx.var.remote_addr)
if ip_count >= connect_count then
res , err = cache:set("bind_"..ngx.var.remote_addr,1)
res , err = cache:expire("bind_",ip_bind_time)
end
end

--结尾标记
::A::
local ok, err = cache:close()
3 changes: 2 additions & 1 deletion web+app/mysql/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#转移到common/mysql
#FROM mysql:latest
FROM mysql:latest
MAINTAINER jamesmo <[email protected]>
18 changes: 18 additions & 0 deletions web+app/mysql/fig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
db:
image: mysql:latest
environment:
MYSQL_USER: java
MYSQL_PASSWORD: java
MYSQL_DATABASE: javatest
MYSQL_ROOT_PASSWORD: mysql
ports:
- "3306:3306"

dbinit:
image: mysql:latest
entrypoint: /bin/bash
volumes:
- .:/host
command: -c "sleep 8; mysql -ujava -pjava -hmysql_base javatest < /host/init.sql; exit 0"
links:
- db:mysql_base
2 changes: 2 additions & 0 deletions web+app/mysql/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
create table testdata (id int not null auto_increment primary key,foo varchar(25),bar int);
insert into testdata values(null, 'hello', 12345);
8 changes: 0 additions & 8 deletions web+app/status.sh

This file was deleted.

0 comments on commit a08d590

Please sign in to comment.