一、安装3台tomcat
192.168.1.236
192.168.1.237
192.168.1.238
启动tomcat 登录验证
二、在236上安装nginx
配置nginx.conf
#user root root;
worker_processes 8;
error_log /home/xinr/nginx/logs/nginx_error.log crit;
pid /home/xinr/nginx/logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
include /home/xinr/nginx/conf/proxy.conf; #一定要指向代理文件
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /home/xinr/nginx/logs/access.log access;
upstream mytomcats {
server 192.168.1.236:8080;
server 192.168.1.237:8080;
server 192.168.1.238:8080;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root /home/xinr/tomcat/webapps/ROOT;
index index.html index.htm ;
}
#本地动静分离反向代理配置
#所有jsp的页面均交由tomcat或resin处理
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://mytomcats;
}
#所有静态文件由nginx直接读取不经过tomcat或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
location /nginxstatus {
stub_status on;
access_log on;
auth_basic "nginxstatus";
auth_basic_user_file /usr/local/nagois/etc/htpasswd.users;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
proxy.conf
#!nginx (-)
# proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #获取真实ip
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #获取代理者的真实ip
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 512k;
proxy_buffers 32 512k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 512k;
启动nginx 访问IP 刷新发现sessions id 一直变化,那么接下来咱们就开始配置sessions共享。
三、配置memcached
后端三台tomcat服务器,每台都装memcached
kryo序列化tomcat配置
安装mamcached共享session
1.tar zxvf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure --prefix=/usr/local/libevent-1.4.14b
make && make install
2. tar zxvf memcached-1.4.7.tar.gz
cd memcached-1.4.7
./configure --prefix=/usr/local/memcached-1.4.7 --with-libevent=/usr/local/libevent-1.4.14b/
make && make install
3.启动memcached:
cd /usr/local/memcached-1.4.7/
./bin/memcached -d -m 256 -u xinr -p 11211 -c 1024 -P /tmp/memcached.pid -d #-vv 控制台输出 #-d 后台运行
查看端口
netstat -an|grep 11211
4. 添加mem和msm的依赖jar包
以下这些包都放在$CATALINA_HOME/lib目录下。
couchbase-client-1.0.3
javolution-5.4.3.1
kryo-1.03
kryo-serializers-0.10
memcached-2.6
memcached-session-manager-1.5.1
memcached-session-manager-tc7-1.5.1
minlog-1.2
msm-kryo-serializer-1.5.1
reflectasm-1.09-sources
spymemcached-2.10.3-javadoc
***这里我是用的tomcat-7.0.61 jdk1.7.0_45
5.接下来是修改tomcat的配置文件$CATALINA_HOME/conf/context.xml,调整成新的session存储方式。配置文件中加入以下内容:
我是用的是 non-sticky sessions + kryo 例子
non-sticky sessions例子的配置. 在这个例子不需要配置 ,session将会在tomcat之间循环,不会仅仅使用一个tomcat。下面是non-sticky sessions 配置 (所有的tomcat都这样) ;
vi /usr/local/tomcat/conf/context.xml 在下面两个字段<Context> </Context>之间添加以下内容
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.1.236:11211 n2:192.168.1.237:11211 n3:192.168.1.238:11211" #由于我们有三台tomcat、memcached,故有三个节点
sticky="false"
lockingMode="auto"
sessionBackupAsync="false"
sessionBackupTimeout="100"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
copyCollectionsForSerialization="ture"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
6.在$CATALINA_HOME/conf/logging.properties文件中添加
de.javakaffee.web.msm.level=FINE,就可以在catalina.out的日志中看到详细的session存取情况。
重启tomcat
7.在tomcat1.2.3根目录创建测试文件
[root@localhost lib]# vi index.jsp /输入如下内容
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
out.println("This is Tomcat Server 236!");
%>
[root@localhost lib]# vi index.jsp /输入如下内容
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
out.println("This is Tomcat Server 237!");
%>
[root@localhost lib]# vi index.jsp /输入如下内容
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
out.println("This is Tomcat Server 238!");
%>
反复刷新浏览器,如果SessionID一直不变,下面的SessionPort的内容在不断变化则说明配置
成功
192.168.1.236
192.168.1.237
192.168.1.238
启动tomcat 登录验证
二、在236上安装nginx
配置nginx.conf
#user root root;
worker_processes 8;
error_log /home/xinr/nginx/logs/nginx_error.log crit;
pid /home/xinr/nginx/logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
include /home/xinr/nginx/conf/proxy.conf; #一定要指向代理文件
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /home/xinr/nginx/logs/access.log access;
upstream mytomcats {
server 192.168.1.236:8080;
server 192.168.1.237:8080;
server 192.168.1.238:8080;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root /home/xinr/tomcat/webapps/ROOT;
index index.html index.htm ;
}
#本地动静分离反向代理配置
#所有jsp的页面均交由tomcat或resin处理
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://mytomcats;
}
#所有静态文件由nginx直接读取不经过tomcat或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
location /nginxstatus {
stub_status on;
access_log on;
auth_basic "nginxstatus";
auth_basic_user_file /usr/local/nagois/etc/htpasswd.users;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
proxy.conf
#!nginx (-)
# proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #获取真实ip
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #获取代理者的真实ip
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 512k;
proxy_buffers 32 512k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 512k;
启动nginx 访问IP 刷新发现sessions id 一直变化,那么接下来咱们就开始配置sessions共享。
三、配置memcached
后端三台tomcat服务器,每台都装memcached
kryo序列化tomcat配置
安装mamcached共享session
1.tar zxvf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure --prefix=/usr/local/libevent-1.4.14b
make && make install
2. tar zxvf memcached-1.4.7.tar.gz
cd memcached-1.4.7
./configure --prefix=/usr/local/memcached-1.4.7 --with-libevent=/usr/local/libevent-1.4.14b/
make && make install
3.启动memcached:
cd /usr/local/memcached-1.4.7/
./bin/memcached -d -m 256 -u xinr -p 11211 -c 1024 -P /tmp/memcached.pid -d #-vv 控制台输出 #-d 后台运行
查看端口
netstat -an|grep 11211
4. 添加mem和msm的依赖jar包
以下这些包都放在$CATALINA_HOME/lib目录下。
couchbase-client-1.0.3
javolution-5.4.3.1
kryo-1.03
kryo-serializers-0.10
memcached-2.6
memcached-session-manager-1.5.1
memcached-session-manager-tc7-1.5.1
minlog-1.2
msm-kryo-serializer-1.5.1
reflectasm-1.09-sources
spymemcached-2.10.3-javadoc
***这里我是用的tomcat-7.0.61 jdk1.7.0_45
5.接下来是修改tomcat的配置文件$CATALINA_HOME/conf/context.xml,调整成新的session存储方式。配置文件中加入以下内容:
我是用的是 non-sticky sessions + kryo 例子
non-sticky sessions例子的配置. 在这个例子不需要配置 ,session将会在tomcat之间循环,不会仅仅使用一个tomcat。下面是non-sticky sessions 配置 (所有的tomcat都这样) ;
vi /usr/local/tomcat/conf/context.xml 在下面两个字段<Context> </Context>之间添加以下内容
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.1.236:11211 n2:192.168.1.237:11211 n3:192.168.1.238:11211" #由于我们有三台tomcat、memcached,故有三个节点
sticky="false"
lockingMode="auto"
sessionBackupAsync="false"
sessionBackupTimeout="100"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
copyCollectionsForSerialization="ture"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
6.在$CATALINA_HOME/conf/logging.properties文件中添加
de.javakaffee.web.msm.level=FINE,就可以在catalina.out的日志中看到详细的session存取情况。
重启tomcat
7.在tomcat1.2.3根目录创建测试文件
[root@localhost lib]# vi index.jsp /输入如下内容
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
out.println("This is Tomcat Server 236!");
%>
[root@localhost lib]# vi index.jsp /输入如下内容
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
out.println("This is Tomcat Server 237!");
%>
[root@localhost lib]# vi index.jsp /输入如下内容
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
out.println("This is Tomcat Server 238!");
%>
反复刷新浏览器,如果SessionID一直不变,下面的SessionPort的内容在不断变化则说明配置
成功