tomcat 之 httpd session stiky

# 注释中心主机

[root@nginx ~]# vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"


#:配置虚拟主机

[root@nginx ~]# vim /etc/httpd/conf.d/tomcat.conf
<VirtualHost *:80>
ServerName      node1.magedu.com
ProxyRequests   Off
ProxyVia        On
ProxyPreserveHost On
ProxyPass       / balancer://lbtomcats/
ProxyPassReverse / balancer://lbtomcats/
</VirtualHost>

<Proxy balancer://lbtomcats>
BalancerMember http://node1.magedu.com:8080 loadfactor=1
BalancerMember http://node2.magedu.com:8080 loadfactor=2
</Proxy>

#:分别配置tomcat
[root@node1 ~]# vim /usr/local/tomcat/conf/server.xml
<Engine name="Catalina" defaultHost="node1.magedu.com" jvmRoute="Tomcat1">
[root@node2 ~]# vim /usr/local/tomcat/conf/server.xml
<Engine name="Catalina" defaultHost="node2.magedu.com" jvmRoute="Tomcat2">



#观察结果是轮询的
#:使用session粘性

[root@nginx ~]# vim /etc/httpd/conf.d/tomcat.conf
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<VirtualHost *:80>
ServerName      node1.magedu.com
ProxyRequests   Off
ProxyVia        On
ProxyPreserveHost On
ProxyPass       / balancer://lbtomcats/
ProxyPassReverse / balancer://lbtomcats/
</VirtualHost>

<Proxy balancer://lbtomcats>
BalancerMember http://node1.magedu.com:8080 loadfactor=1 route=Tomcat1
BalancerMember http://node2.magedu.com:8080 loadfactor=2 route=Tomcat2
ProxySet stickysession=ROUTEID
</Proxy>
#
发现Session不变了,一直找的同一个Tomcat服务器

 

#:api

[root@nginx ~]# vim /etc/httpd/conf.d/tomcat.conf 
<VirtualHost *:80>
ServerName      node1.magedu.com
ProxyRequests   Off
ProxyVia        On
ProxyPreserveHost On
ProxyPass       / balancer://lbtomcats/
ProxyPassReverse / balancer://lbtomcats/
</VirtualHost>

<Proxy balancer://lbtomcats>
BalancerMember ajp://node1.magedu.com:8009 loadfactor=1 route=Tomcat1
BalancerMember ajp://node2.magedu.com:8009 loadfactor=2 route=Tomcat2
ProxySet stickysession=ROUTEID
</Proxy>

虽然,上面的做法实现客户端在一段时间内找同一台Tomcat,从而避免切换后导致的Session丢失。但是如果

Tomcat节点挂掉,那么Session依旧丢失。

假设有AB两个节点,都将Session持久化。如果Tomcat A服务下线期间用户切换到了Tomcat B上,就获得了Tomcat BSession,就算持久化SessionTomcat A上线了,也没用了。

posted @ 2019-09-02 16:59  Linus小跟班  阅读(297)  评论(0编辑  收藏  举报