集群主要有两个问题:1,负载均衡,2,session共享
准备两个tomcat 如果tomcat在同一台机器上面改掉三个端口
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
以前三个端口不能相同。不然无法启动服务
nginx upstream模块 简单负载均衡
upstream nnds{
server localhost:8080;
server localhost:8081;
#server localhost:8082;
#server localhost:8083;
}
server {
listen 80;
server_name xxx.xxx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#proxy_redirect off ;#undisplay port
proxy_pass http://nnds;
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_set_header REMOTE-HOST $remote_addr;
#root html;
index index.html index.htm;
}
}
如果没有做session共享 会出现session找不到的情况 解决这种情况可以将upstream 下面加上ip_hash属性 这样每个session都是访问同一台服务器 因此这样负载均衡就不是那么明显
那么接下来就session共享.
我用的版本是tomcat 6 参考 文档:http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html
去server.xml 找到
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
换成文档上面的 两个tomcat下面红色的端口不能相同
<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="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
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"/>
</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>
最后别忘记了在 工程的 web.xml中加入<distributable/>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<display-name>TomcatDemo</display-name>
<distributable/>
</web-app>
完成
可以参考:http://hi.baidu.com/luodaijun/blog/item/5bbe4cfb5ffef864034f56a1.html
http://zyycaesar.iteye.com/blog/296606
集群二。上面的搞法是保证每个tomcat都有相同的session 如果有N个tomcat 就会导致tomcat复制灾难。
所以可以采取sna架构模式,将session放到一个公共的地方 比如memcache 或者有一个 session cache server
参考:http://blog.csdn.net/myloon/article/details/5866456