nginx+tomcat集群+session复制
环境:
centos6.0、pcre-8.12、nginx-upstream-jvm-route-0.1、nginx-1.0.5、apache-tomcat-6.0.33
nginx服务器地址:172.16.1.170
tomcat1:172.16.1.171
tomcat2:172.16.1.172
1.安装nginx
[root@asdasda zhangfang]# tar pcre-8.12.tar.gz
[root@asdasda pcre-8.12]# ./config
[root@asdasda pcre-8.12]# make && make install
下载下面的插件安装,否则nginx无法识别tomcat中jvmRoute,从而无法达到session复制的效果。
[root@asdasdazhangfang]#wgethttp://friendly.sinaapp.com//LinuxSoft/nginx-upstream-jvm-route-0.1.tar.gz
[root@asdasda zhangfang]# tar zxf nginx-upstream-jvm-route-0.1.tar.gz
[root@asdasda zhangfang]# tar nginx-1.0.15.tar.gz
patch -p0 <../nginx_upstream_jvm_route/jvm_route.patch
[[email protected]]#./configure --prefix=/usr/local/nginx --with-http_stub_status_module --add-module=../nginx_upstream_jvm_route/
[root@asdasda nginx-1.0.15]# make && make install
2.配置nginx
[root@asdasda nginx-1.0.15]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
worker_connections 5000;
}
http {
upstream backend {
server 172.16.1.171:8080 srun_id=tomcat1; //在tomcat的配置里会配置jvmRoute为tomcat1
server 172.16.1.172:8080 srun_id=tomcat2;
jvm_route $cookie_JSESSIONID|sessionid reverse;
}
include mime.types;
default_type application/octet-stream;
#charset gb2312;
charset UTF-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 20m;
limit_rate 1024k;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
#gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server
{
listen 80;
server_name 172.16.1.170;
index index.jsp index.htm index.html;
root /usr/local/nginx/html/wanka-website/;
location / {
proxy_pass http://backend;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
Root /usr/local/nginx/html/wanka-website/; 所有的静态文件gif、jpg等都在本地打开,存放的目录为/usr/local/nginx/html/wanka-webiste,保存时间为30天
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
}
}
注意nginx配置文件红色部分,如果不指定root访问172.16.1.170时,出现图片无法显示的错误。
启动nginx
[root@asdasda nginx-1.0.15]# /usr/local/nginx/sbin/nginx
3.分别在172.16.1.171和172.16.1.172安装tomcat,
首先安装jdk
[root@localhost zhangfang]# chmod +x jdk-6u24-linux-x64.bin
[root@localhost zhangfang]# ./jdk-6u24-linux-x64.bin
[root@localhost zhangfang]# mkdir /usr/java/
改名为jdk并且移动到/usr/java/
[root@localhost zhangfang]# ls /usr/java/
Jdk
在/etc/profile文件末尾添加
export JAVA_HOME=/usr/java/jdk
export JAVA_BIN=/usr/java/jdk/bin
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
[root@localhost zhangfang]# source /etc/profile
[root@localhost zhangfang]# java -version
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)
说明jdk安装成功
安装tomcat
[root@localhost zhangfang]# tar zxf apache-tomcat-6.0.33.tar.gz
[root@localhost zhangfang]# mv apache-tomcat /usr/local/tomcat/
5.tomcat集群配置
172.16.1.171
root@localhost zhangfang]# vim /usr/local/tomcat/conf/server.xml
修改为<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"> #t172.16.1.172上面修改为tomcat2
在<Host段添加
<Context path="" docBase="wanka-website" debug="0" reloadable="true"/>
指定项目的名称路径在/usr/local/tomcat/webapps/wanka-website
在</Host>下方添加
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="224.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="172.16.1.171" 本机ip
port="4001"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" />
</Sender>
<Interceptor
className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor
className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptor
className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener
className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
6.session配置
7.修改web应用里面WEB-INF目录下的web.xml文件,加入标签
8.<distributable/>
直接加在</web-app>之前
开启网卡组播功能:
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
分别启动tomcat
将wanka-website项目分别传到nginx的网站目录下,和tomcat网站目录下
测试
看到首页说明成功
测试会话是否同步
在万卡网站申请一个账户,名字随便,用来我们做测试,假如用户登陆到172.16.1.171上面,
关掉172.16.1.171
[root@localhost classes]# /usr/local/tomcat/bin/shutdown.sh
我们再次刷新页面
用户登陆信息不变说明会话保持成功
我们可以看下tomcat日志
[root@localhost ~]# tail -f /usr/local/tomcat/logs/catalina.out
INFO:Verificationcomplete.Memberdisappeared[org.apache.catalina.tribes.membership.MemberImpl[tcp://{172, 16, 1, 171}:4001,{172, 16, 1, 171},4001, alive=131086,id={120 116 60 36 -116 31 70 25 -83 124 -28 -25 -3 65 -75 90 }, payload={}, command={66 65 66 89 45 65 76 69 88 ...(9)}, domain={}, ]]