示例1:
会话管理集群示例:
node1 nginx代理 192.168.170.9
node2 tomcatA 192.168.10.11
node3 tomcatB 192.168.10.12
node1
# vi /etc/nginx/nginx.conf
http {
upstream tcsrvs {
server 192.168.10.11:8080;
server 192.168.10.12:8080;
}
}
# vi /etc/nginx/conf.d/ilinux.conf
server {
listen 80;
server_name www.ilinux.io;
location / {
proxy_pass http://tcsrvs;
}
}
# nginx -t
# nginx -s reload
# ss -tunlp
www.ilinux.io www.ilinux.io/test/ 正常
识别成不同的会话
示例2:
node2
# vi /etc/hosts
192.168.10.11 node2.magedu.com
[root@node2 ~]# cp /etc/tomcat/server.xml /etc/tomcat/server.xml.bak
[root@node2 ~]# vi /etc/tomcat/server.xml 放在host内部
<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.10.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>
[root@node2 ~]# tail /var/log/tomcat/catalina.2019- 查看配置文件错误信息并处理
[root@node2 ~]# systemctl restart tomcat
[root@node2 ~]# ls /var/lib/tomcat/webapps/test/WEB-INF
[root@node2 ~]# cp /etc/tomcat/web.xml /var/lib/tomcat/webapps/test/WEB-INF/
[root@node2 ~]# vi /var/lib/tomcat/webapps/test/WEB-INF/web.xml 放在web-app内部
<distributable/>
node3
# vi /etc/hosts
192.168.10.12 node3.magedu.com
[root@node1 ~]# cp /etc/tomcat/server.xml /etc/tomcat/server.xml.bak
[root@node1 ~]# vi /etc/tomcat/server.xml 放在host内部
<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.10.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>
[root@node1 ~]# tail /var/log/tomcat/catalina.2019- 查看配置文件错误信息并处理
[root@node1 ~]# systemctl restart tomcat
[root@node1 ~]# ls /var/lib/tomcat/webapps/test/WEB-INF
[root@node1 ~]# cp /etc/tomcat/web.xml /var/lib/tomcat/webapps/test/WEB-INF/
[root@node1 ~]# vi /var/lib/tomcat/webapps/test/WEB-INF/web.xml
<distributable/>
JAVA_OPT="-Xmx512m -Xms512m"
httpd代理示例:
node1 httpd代理 192.168.170.8
[root@node1 ~]# systemctl stop nginx
[root@node1 ~]# vi /etc/httpd/conf.d/ilinux-http-tomcat.conf
<Proxy balancer://tcsrvs>
BalancerMember http://192.168.10.11:8080
BalancerMember http://192.168.10.12:8080
ProxySet lbmethod=byrequests
</Proxy>
<VirtualHost *:80>
ServerName www.mylinux.com
ProxyRequests Off
#DocumentRoot "/data/ilinux/htdocs"
ProxyVia On
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://tcsrvs/
ProxyPassReverse / balancer://tcsrvs/
<Location />
Require all granted
</Location>
</VirtualHost>
[root@node1 ~]# httpd -t
[root@node1 ~]# systemctl restart httpd
[root@node1 ~]# ss -utnlp
浏览器测试:www.mylinux.com/test/ 将会识别成相同的会话D并没有变化
示例3:
node2
[root@node2 ~]# yum -y install memcached
[root@node2 ~]# systemctl start memcached
[root@node2 ~]# scp /etc/tomcat/server.xml.bak /etc/tomcat/server.xml
[root@node2 ~]# vi /etc/tomcat/server.xml
<Context> path="/test" docBase="test" reloadable="true">
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="m1:192.168.170.11:11211,n2:192.168.170.12:11211"
failoverNodes="m2"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.Javolution.JavolutionTranscoderFactory"
/>
</Context>
[root@node2 ~]# mv memcached-session-manager-tc7-1.8.2.jar spymemcached-2.10.2.jar msm-javolution-serializer-1.8.2.jar javolution-5.5.1.jar memcached-session-manager-1.8.2.jar /usr/share/java/tomcat/
[root@node2 ~]# systemctl restart tomcat
[root@node2 ~]# tail /var/log/tomcat/catalina.2019-
node3
[root@node3 ~]# yum -y install memcached
[root@node3 ~]# systemctl start memcached
[root@node3 ~]# scp /etc/tomcat/server.xml.bak /etc/tomcat/server.xml
[root@node3 ~]# vi /etc/tomcat/server.xml
<Context> path="/test" docBase="test" reloadable="true">
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="m1:192.168.10.11:11211,m2:192.168.10.12:11211"
failoverNodes="m2"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.Javolution.JavolutionTranscoderFactory"
/>
</Context>
[root@node3 ~]# mv memcached-session-manager-tc7-1.8.2.jar spymemcached-2.10.2.jar msm-javolution-serializer-1.8.2.jar javolution-5.5.1.jar memcached-session-manager-1.8.2.jar /usr/share/java/tomcat/
[root@node3 ~]# systemctl restart tomcat
[root@node3 ~]# tail /var/log/tomcat/catalina.2019-
www.mylinux.com/test 可以看到会话绑定m1上
node2
#systemctl stop tomcat
www.mylinux.com/test 可以看到会话绑定m2上的
浏览器测试:会话ID并没有绑定到集群上,并没有找到原因,和期望不相符合。正在排除