环境
A:nginx和httpd:192.168.213.253
B:tomcat:192.168.213.251
C:tomcat:192.168.213.254
1)httpd会话绑定
在B和C上设置
vim /etc/tomcat/server.xml
<Engine defaultHost="localhost" jvmroute="tomcatA"> ##在B上此行增加一个jvmroute="tomcatA,在C上此行增加jvmroute="tomcatB"
systemctl restart tomcat
在A上设置
vim /etc/httpd/conf.d/tomcat.conf
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=http://www.likecs.com/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://tomcatsrvs>
BalancerMember :8080 route=tomcatA
BalancerMember :8080 route=tomcatB
ProxySet lbmethod=byrequests
ProxySet stickysession=ROUTEID
</Proxy>
namevirtualhost *:80
<VirtualHost *:80>
documentroot /app
<directory /app>
Require all granted
</directory>
ProxyVia On
ProxyRequests Off
ProxyPass / balancer://tomcatsrvs/
ProxyPassReverse / balancer://tomcatsrvs/
</VirtualHost>
service httpd reload
2)nginx会话绑定
在B和C上操作同上
在A上配置
vim /etc/nginx/nginx.conf
upstream tomcatsrv {
server 192.168.213.251:8080 ;
server 192.168.213.254:8080 ;
hash $request_uri consistent;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
index index.jsp ;
root /usr/share/tomcat/webapps;
location / {
proxy_pass ;
}
}
service nginx restart
发现只能调度至第一次访问的后端服务器,实现会话绑定。
6.两个tomcat服务器保存有相同的会话
环境
A:nginx和httpd:192.168.213.253
B:tomcat:192.168.213.251
C:tomcat:192.168.213.254
1)在B和C上的设置
访问tomcat的官方文档
:8080/docs/cluster-howto.html ,Document---->Clustering
将官方文档中的如下内容复制到tomcat配置文件的<engine>或<host>中,此实验放到Engine中
vim /etc/tomcat/server.xml
<Engine defaultHost="localhost">
<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.74.74.74"
port="45564"
frequency="500"
##表示每0.5s发送一次心跳信息告诉其他成员自己还活着
dropTime="3000"/> ##表示3s没有发送信息就证明坏了
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="172.18.213.251" ##另外一台主机修改为172.18.213.254
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>
cp /etc/tomcat/web.xml /usr/share/tomcat/webapps/app/WEB-INF/
cd /usr/share/tomcat/webapps/app/WEB-INF/
vim web.xml
在此文件内部没有注释的地方加如下内容
<distributable/> ##注意一定要在这个文件的内部,就在<web.app下加上就可
systemctl restart tomcat
注意:
CentOS 7上的tomcat自带的文档中的配置示例有语法错误;没有加最后的/
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
2)在A的设置
vim /etc/httpd/conf.d/tomcat.conf
<proxy balancer://tomcatsrvs>
BalancerMember :8080
BalancerMember :8080
ProxySet lbmethod=byrequests
</Proxy>
namevirtualhost *:80
<VirtualHost *:80>
documentroot /app
<directory /app>
Require all granted
</directory>
ProxyVia On
ProxyRequests Off
ProxyPass / balancer://tomcatsrvs/
ProxyPassReverse / balancer://tomcatsrvs/
</VirtualHost>
service httpd reload