一般来说,在多个tomcat集群业务中,session会话共享是必须的需求,不然前端nginx转发过来的请求不知道之前请求在哪台tomcat节点上,从而就找不到session以至于最终导致请求失败。要实现tomcat session共享有多种方案,今天介绍下使用tomcat自带的cluster方式,在多个tomcat节点间自动实时复制session信息,配置起来很简单。tomcat自带的这种session共享方案,对于规模较小的tomcat集群来说够用了,但这个方案的效率比较低,在大并发下表现并不好。所以大规模的tomcat集群还是建议使用memcache或者redis来实现session共享。
1)基础环境
ip | 主机名 | 应用 | 端口 |
192.168.10.200 | Nginx-node | nginx1.12.2 | 80 |
192.168.10.201 | Tomcat-node1 | java8.131、tomcat8.0.53 | 8080 |
192.168.10.202 | Tomcat-node2 | java8.131、tomcat8.0.53 | 8080 |
安装省略。。。
[root@Tomcat-node1 conf]
# vim server.xml
.......
<Engine name=
"Catalina"
defaultHost=
"localhost"
>
找到上面这一行,在此行的下面直接复制以下内容,不用修改,直接复制就可以
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="6">
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<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="192.168.10.201"
port="5000"
selectorTimeout="100"
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=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<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.ClusterSessionListener"/>
</Cluster>
记得多个tomcat都需要配置,同一个集群里面配置信息保持一直就可以了。如果是其他集群,修改一下address就好了。
另一个tomcat节点直接拷贝这个server.xml文件,然后将address修改为192.168.10.202就可以了
然后还有一个重要配置不要忘记了!否则session将不能实现共享!!
在tomcat项目的WEB-INF
/web
.xml文件中添加<distributable/>.
直接在底部</web-app>的前面添加既可以
<distributable/>
修改:/usr/local/tomcat8/webapps/ROOT/index.jsp
<html>
<head>
<title> session test</title>
</head>
<body>
SessionID is <%=session.getId()%>
<BR>
SessionIP is <%=request.getServerName()%>
<BR>
SessionPort is <%=request.getServerPort()%>
<%
out.println("Response from tomcat-node1");
%>
%</body>
%</html>
若nginx配置如下:
upstream tomcat-lb {
server 192.168.10.201:8080;
server 192.168.10.202:8080;
}
server {
listen 80;
server_name www.kevin.com;
location / {
proxy_pass http://tomcat-lb;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .*\.(gif|jpg|png|htm|html|css|ico|flv|swf)(.*) {
proxy_pass http://tomcat-lb;
proxy_redirect off;
proxy_set_header Host $host;
proxy_cache cache_one;
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 1d;
proxy_cache_valid any 10m;
expires 30d;
proxy_cache_key $host$uri$is_args$args;
}
}
访问http://www.kevin.com,不断刷新页面,发现页面信息会改变,但是SessionID一直保持不变!!
关闭192.168.10.201或192.168.10.202两个中的任意一个tomcat服务,访问页面,不断刷新,SessionID都会一直保持不变!!