nginx+tomcat集群+session复制
ulimit -SHn 65535 echo "ulimit -SHn 65535" >>/etc/rc.local
环境:
redhat5.4、pcre-8.12、nginx-upstream-jvm-route-0.1、nginx-1.0.5、apache-tomcat-6.0.33
nginx服务器地址:192.168.0.40
tomcat1:192.168.0.20
tomcat2:192.168.0.30拓扑如下:
1.安装nginx
tar zxf pcre-8.12.tar.gz
cdpcre-8.12
./configure
make;make install下载下面的插件安装,否则nginx无法识别tomcat中jvmRoute,从而无法达到session复制的效果。
wget http://friendly.sinaapp.com//LinuxSoft/nginx-upstream-jvm-route-0.1.tar.gz
tarxzfnginx-upstream-jvm-route-0.1.tar.gz
tarxzfnginx-1.0.5.tar.gz
cdnginx-1.0.5
patch-p0<../nginx_upstream_jvm_route/jvm_route.patch
/configure--prefix=/usr/local/nginx--with-http_stub_status_module--with-pcre=/root/pcre-8.12--add-module=../nginx_upstream_jvm_route/
#--with-pcre=指向的是pcre的源码包
make;make install2.配置nginx
vim /usr/local/nginx/conf/nginx.conf
#user nobody; worker_processes 1;
#error_log logs/error.log;
#error_loglogs/error.lognotice;
#error_log logs/error.log info;pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200;
events {
worker_connections5000;
}http {
upstreambackend{
server192.168.0.20:8080srun_id=tomcat1;//在tomcat的配置里会配置jvmRoute为tomcat1
server192.168.0.30:8080srun_id=tomcat2;
jvm_route$cookie_JSESSIONID|sessionidreverse;
}include mime.types;
default_typeapplication/octet-stream;
#charsetgb2312;
charsetUTF-8;
server_names_hash_bucket_size128;
client_header_buffer_size32k;
large_client_header_buffers432k;
client_max_body_size20m;
limit_rate1024k;
sendfileon;
tcp_nopushon;
keepalive_timeout60;
tcp_nodelayon;
fastcgi_connect_timeout300;
fastcgi_send_timeout300;
fastcgi_read_timeout300;
fastcgi_buffer_size64k;
fastcgi_buffers464k;
fastcgi_busy_buffers_size128k;
fastcgi_temp_file_write_size128k;
gzipon;
#gzip_min_length1k;
gzip_buffers416k;
gzip_http_version1.0;
gzip_comp_level2;
gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;
gzip_vary on;server
{
listen80;
server_namewww.baiying.com;
indexindex.jspindex.htmindex.html;
root /usr/www;location / {
proxy_passhttp://backend;
proxy_redirectoff;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerHost$http_host;
}
location~.*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires30d;
}
location~.*\.(js|css)?$
{
expires1h;
}
location/Nginxstatus{
stub_statuson;
access_logoff;
}
log_formataccess'$remote_addr-$remote_user[$time_local]"$request"'
'$status$body_bytes_sent"$http_referer"'
'"$http_user_agent" $http_x_forwarded_for';}
}
[root@slaveconf]#/usr/local/bin/nginx.shstop
[root@slaveconf]#/usr/local/bin/nginx.shstart
3.安装tomcat
1)安装tomcat_1
tarzxfapache-tomcat-6.0.33.tar.gz
mvapache-tomcat-6.0.33/usr/local/tomcat
cptomcat-replication.jar/usr/local/tomcat/lib
2)安装tomcat_2,步骤同1)4.分别在tomcat服务器安装jdk
chmod 755 jdk-6u26-linux-i586-rpm.bin ./jdk-6u26-linux-i586-rpm.bin
cat >>/etc/profile <<EOF
exportJAVA_HOME=/usr/java/jdk1.6.0_26
exportCLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/binEOF;
source /etc/profile //使环境变量立即生效
5.tomcat集群配置
tomcat1配置:
修改conf/server.xml配置文件<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
<Clusterclassname="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Managerclassname="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channelclassname="org.apache.catalina.tribes.group.GroupChannel">
<Membershipclassname="org.apache.catalina.tribes.membership.McastService"
address="224.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiverclassname="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.0.20"
port="4001"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Senderclassname="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transportclassname="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Valveclassname="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valveclassname="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployerclassname="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListenerclassname="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListenerclassname="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>tomcat2配置: 修改conf/server.xml配置文件
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
<Clusterclassname="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Managerclassname="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channelclassname="org.apache.catalina.tribes.group.GroupChannel">
<Membershipclassname="org.apache.catalina.tribes.membership.McastService"
address="224.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiverclassname="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.0.30"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Senderclassname="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transportclassname="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Valveclassname="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"/>
<ClusterListenerclassname="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListenerclassname="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>6.session配置
修改web应用里面WEB-INF目录下的web.xml文件,加入标签
<distributable/>
直接加在</web-app>之前
开启网卡组播功能:
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth07.创建jsp测试页面
在tomcat1安装路径下建立目录test
mkdir/usr/local/tomcat/webapps/ROOT/test/
viindex.jsp
<html>
<title>
tomcat1jsp
</title>
<%
StringshowMessage="OhMyGod192.168.0.20华仔!";
out.print(showMessage);
%>
</html>
----------------------------
在tomcat2安装路径下建立目录test
mkdir/usr/local/tomcat/webapps/ROOT/test/
viindex.jsp
<html>
<title>
tomcat2jsp
</title>
<%
StringshowMessage="OhMyGod192.168.0.30华仔!";
out.print(showMessage);
%>
</html>