ulimit -SHn 65535
echo "ulimit -SHn 65535" >>/etc/rc.local
环境:
redhat6.3、pcre-8.12、nginx-upstream-jvm-route-0.1、nginx-1.0.5、apache-tomcat-6.0.33
nginx服务器地址:192.168.10.40
tomcat1:192.168.10.20
tomcat2:192.168.10.30
拓扑如下:
1.安装nginx
下载pcre包:
wget http://ftp.exim.llorien.org/pcre/pcre-8.12.tar.gz
tar zxf pcre-8.12.tar.gz
cd pcre-8.12
./configure
make;make install
下载下面的插件安装,否则nginx无法识别tomcat中jvmRoute,从而无法达到session复制的效果。
wget http://friendly.sinaapp.com//LinuxSoft/nginx-upstream-jvm-route-0.1.tar.gz
tar xzf nginx-upstream-jvm-route-0.1.tar.gz
下载nginx源码包:
wget http://nginx.org/download/nginx-1.0.5.tar.gz
tar xzf nginx-1.0.5.tar.gz
cd nginx-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的源码包;如果需要做nginx和tomcat动静分离;可继续添加模块 --
make;make install
2.配置nginx
vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
worker_connections 5000;
}
http {
upstream backend {
server 192.168.10.20:8080 srun_id=tomcat1; //在tomcat的配置里会配置jvmRoute为tomcat1
server 192.168.10.30: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 www.baiying.com;
index index.jsp index.htm index.html;
root /usr/www;
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)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
location /Nginxstatus {
stub_status on;
access_log off;
}
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@liang conf]# killall nginx
nginx启动:
[root@liang conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3.安装tomcat
下载tomcat包:
wget http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.33/bin/apache-tomcat-6.0.33.tar.gz
1)安装tomcat_1
tar zxf apache-tomcat-6.0.33.tar.gz
mv apache-tomcat-6.0.33 /usr/local/tomcat
tomcat-replication.jar 下载地址:(--用于测试广播!)
wget http://cvs.apache.org/~fhanik/tomcat-replication.jar
cp tomcat-replication.jar /usr/local/tomcat/lib
2)安装tomcat_2,步骤同1)
4.分别在tomcat服务器安装jdk
1)安装jdk包之前查看服务器上的jdk,并且卸载;
[root@liang conf]# rpm -qa|grep java
tzdata-java-2012c-1.el6.noarch
java-1.6.0-openjdk-1.6.0.0-1.45.1.11.1.el6.x86_64
现在分别卸载以上两个包:
[root@liang conf]# rpm -e --nodeps tzdata-java-2012c-1.el6.noarch
[root@liang conf]# rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.45.1.11.1.el6.x86_64
下面安装新的jdk:如下
下载jdk包:--我这机器用wget直接下载出了问题;可尝试加wget --no-cookie --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F"的下载方法:
如下:
wget --no-cookie --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F" http://download.oracle.com/otn-pub/java/jdk/7/jdk-7-linux-x64.rpm
2)安装jdk包:
chmod 755 jdk-7-linux-x64.rpm
rpm -ivh jdk-7-linux-x64.rpm
2)修改java环境变量;
vim /etc/profile
最后添加下面行数:
export JAVA_HOME=/usr/java/jdk1.7.0
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin
source /etc/profile //使环境变量立即生效
5.tomcat集群配置
tomcat1配置:
修改vim /usr/local/tomcat/conf/server.xml配置文件;找到Engine 这行修改,并且在这行添加
下面的 <Cluster .......
</Cluster>
总得为下面再贴过去:
Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
<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="192.168.10.20"
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>
注意:以上有个IP为address="192.168.10.20" 修改为自己本机的ip。
address="192.168.1.102"组播IP,一般为本机
address="224.0.0.4"#约定的广播地址,所有服务器相同
port="4001"#集群通讯端口--如果tomcat集群在不同机器上端口可修改为不同
tomcat2配置:
修改/usr/local/tomcat/conf/server.xml配置文件类似上面的tomcat1的配置:
如下:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
<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="192.168.0.30"
port="4000"
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>
注意:以上有个IP为address="192.168.10.30" 修改为自己本机的ip。
6.session配置
修改web应用里面WEB-INF目录下的web.xml文件,加入标签
<distributable/>
直接加在</web-app>之前
开启网卡组播功能: --(两台都运行同样的如下命令)
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
7.修改tomcat的web主目录--- 两台机器分别都运行。
vim /usr/local/tomcat/conf/server.xml
找到
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
在这行下面添加如下一行--即可把目录换成了/liang/test/下:
<Context path="" docBase="/liang/test" debug="0" reloadable="true" />
7.创建jsp测试页面
在tomcat1下建立目录test
mkdir /liang/test/
vi index.jsp
<html>
<title>
tomcat1 jsp
</title>
<%
String showMessage="Oh My God 192.168.0.20 华仔!";
out.print(showMessage);
%>
</html>
----------------------------
在tomcat2下建立目录test
mkdir /liang/test/
vi index.jsp
<html>
<title>
tomcat2 jsp
</title>
<%
String showMessage="Oh My God 192.168.0.30 华仔!";
out.print(showMessage);
%>
</html>
8.在两台tomcat上分别启动tomcat 进行测试:
启动命令:
cd /usr/local/tomcat/bin
./startup --启动进行访问
9.测试结果
访问192.168.10.40 返回的结果为tomcat1或者tomcat2上一台后就不会改变访问结果
注意: 中途有问题 可以在单独搭建好tomcat后分别访问tomcat的8080端口看是否能正常访问网站
----如果不能,可查看下机器上的iptables等进行排错。
补充:想要适当解决内存溢出问题jdk选jrockit-jdk1.6.0_29-R28.2.0-4.1.0-linux-x64