共计 10310 个字符,预计需要花费 26 分钟才能阅读完成。
Tomcat 实现 session 保持案例
apache: tomcats
(1) apache:
mod_proxy
mod_proxy_http 实现代理
mod_proxy_balancer 实现负载均衡
tomcat:
http connector http 连接器
(2) apache:
mod_proxy
mod_proxy_ajp ajp 模块
mod_proxy_balancer
tomcat:
ajp connector ajp 连接器
(3) apache:
mod_jk
tomcat:
ajp connector
方案一:使用 nginx 反代用户请求到 tomcat:(实现负载均衡和 session 绑定)
配置 hosts 文件:
192.168.20.1 node1.lee.com node1
192.168.20.2 node2.lee.com node2
192.168.20.8 node4.lee.com node4
192.168.20.7 node3.lee.com node3
前端 nginx 配置实现负载均衡:
1. 在 http 上下文定义 upstream server
upstream tcsrvs {
ip_hash;实现 session 绑定
server node1.lee.com:8080;
server node2.lee.com:8080;
}
2. 在 server 段中调用:
location / {
root /usr/share/nginx/html;
}
location ~* \.(jsp|do)$ {
proxy_pass http://tcsrvs;
}
后端两个 tomcat 配置 server.xml:
示例只给了第一台的配置,第二台的只需将所有 node1 改为 node2 即可
<Engine name=”Catalina” defaultHost=”node1.lee.com”>
<Host name=”node1.lee.com” appBase=”/data/webapps/” unpackWARs=”true” autoDeploy=”true”>
<Context path=”” docBase=”/data/webapps” reloadable=”true”>
<Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”/data/logs”
prefix=”web1_access_log” suffix=”.txt”
pattern=”%h %l %u %t "%r" %s %b” />
/data/webapps/index.jsp 文件:
<%@ page language=”java” %>
<%@ page import=”java.util.*” %>
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<% out.println(“Hello, world.”); %>
</body>
</html>
方案二:使用 httpd 反代用户请求到 tomcat
前端 httpd 反代配置:
<proxy balancer://lbcluster1>
BalancerMember http://172.16.100.68:8080 loadfactor=10 route=TomcatA
BalancerMember http://172.16.100.69:8080 loadfactor=10 route=TomcatB
</proxy>
<VirtualHost *:80>
ServerName web1.lee.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order Deny,Allow
Allow from all
</Proxy>
ProxyPass /status !
ProxyPass / balancer://lbcluster1/
ProxyPassReverse / balancer://lbcluster1/
<Location />
Order Deny,Allow
Allow from all
</Location>
</VirtualHost>
后端 tomcat 主机配置:此处为 node1 主机,node2 主机设置为 jvmRoute=”TomcatB”,测试页也做相应替换
<Engine name=”Catalina” defaultHost=”node1.lee.com” jvmRoute=”TomcatA”> #jvmRoute 为了让前端 httpd 可以精确识别自己,使用 jvmRoute 作为标示
编辑测试页面:/data/webapps/index.jsp
<%@ page language=”java” %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color=”red”>TomcatA.lee.com</font></h1>
<table align=”centre” border=”1″>
<tr>
<td>Session ID</td> <% session.setAttribute(“lee.com”,”lee.com”); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
测试:
发现即便调度到同一主机 session 也会变,更不用说不调度在同一主机
解决:修改这两行,使用 session 粘性功能
Header add Set-Cookie “ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/” env=BALANCER_ROUTE_CHANGED
<proxy balancer://lbcluster1>
BalancerMember http://192.168.20.1:8080 loadfactor=10 route=TomcatA
BalancerMember http://192.168.20.2:8080 loadfactor=10 route=TomcatB
ProxySet stickysession=ROUTEID
</proxy>
测试后发现 session 绑定成功
使用 ajp 连接:只需要修改两行
#Header add Set-Cookie “ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/” env=BALANCER_ROUTE_CHANGED
注释上面一行是因为使用 ajp 协议的话只需要 ProxySet stickysession=ROUTEID 一条语句即可绑定
<proxy balancer://lbcluster1>
BalancerMember ajp://172.16.100.68:8009 loadfactor=10 route=TomcatA
BalancerMember ajp://172.16.100.69:8009 loadfactor=10 route=TomcatB
ProxySet stickysession=ROUTEID
</proxy>
方案三:使用 mod_jk 作反向代理,使用 mod_jk 后端连接只能使用 ajp 协议
tar xf tomcat-connectors-1.2.40-src.tar.gz
cd tomcat-connectors-1.2.40-src/native
准备编译环境:
yum install httpd-devel gcc glibc-devel
yum groupinstall “Development tools”
mod_jk 依赖于 apxs
[root@node3 rpm]# which apxs
/usr/sbin/apxs
在 native 目录下:
./configure –with-apxs=/usr/sbin/apxs
装载 mod_jk 模块:在 httpd.conf 文件中:
LoadModule jk_module modules/mod_jk.so
查看是否装载成功:
[root@node3 conf]# httpd -M | grep jk
Syntax OK
jk_module (shared)
配置 jk_module 属性:httpd.conf 中
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /* TomcatA #此处的 TomcatA 必须与后端 Tomcat 的 engine 中定义哪个 TomcatA 的一致
JkMount /status/ stat1
创建 /etc/httpd/conf.d/workers.properties
worker.list=TomcatA,stat1
worker.TomcatA.port=8009
worker.TomcatA.host=192.168.20.1
worker.TomcatA.type=ajp13
worker.TomcatA.lbfactor=1
worker.stat1.type = status
访问 mod_jk 自带的 status 页面:此页面也有管理功能
改为负载均衡:并且会话绑定功能
修改 httpd.conf:
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /* lbcluster1
JkMount /jkstatus/ stat1
修改 /etc/httpd/conf.d/workers.properties
worker.list = lbcluster1,stat1
worker.TomcatA.type = ajp13
worker.TomcatA.host = 192.168.20.1
worker.TomcatA.port = 8009
worker.TomcatA.lbfactor = 5
worker.TomcatB.type = ajp13
worker.TomcatB.host = 192.168.20.2
worker.TomcatB.port = 8009
worker.TomcatB.lbfactor = 5
worker.lbcluster1.type = lb
worker.lbcluster1.sticky_session = 1
worker.lbcluster1.balance_workers = TomcatA, TomcatB
worker.stat1.type = status
测试成功
介绍:proxy-balancer-manager 模块页面的使用
<proxy balancer://lbcluster1>
BalancerMember http://192.168.20.1:8080 loadfactor=10 route=TomcatA
BalancerMember http://192.168.20.2:8080 loadfactor=10 route=TomcatB
ProxySet stickysession=ROUTEID
</proxy>
<VirtualHost *:80>
ServerName web1.lee.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Location /balancer-manager>
SetHandler balancer-manager
ProxyPass !
Order Deny,Allow
Allow from all
</Location>
<Proxy *>
Order Deny,Allow
Allow from all
</Proxy>
ProxyPass /status !
ProxyPass / balancer://lbcluster1/
ProxyPassReverse / balancer://lbcluster1/
<Location />
Order Deny,Allow
Allow from all
</Location>
</VirtualHost>
测试:
delta-manager 实现会话复制集群实现
在 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.0.1.7″
port=”45564″
frequency=”500″
dropTime=”3000″/>
<Receiver className=”org.apache.catalina.tribes.transport.nio.NioReceiver”
address=”192.168.20.1″ #node2 改成 192.168.20.2
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.ClusterSessionListener”/>
</Cluster>
配置我们的特定应用程序调用上面的 cluster 功能
[root@node1 conf]# cp web.xml /data/webapps/WEB-INF/
[root@node1 conf]# vim /data/webapps/WEB-INF/web.xml
添加 <distributable/>
[root@node1 conf]# scp /data/webapps/WEB-INF/web.xml node2:/data/webapps/WEB-INF/
web.xml 100% 163KB 162.7KB/s 00:00
查看日志,发现集群中加入了主机:
tail -100 /usr/local/tomcat/logs/catalina.out
01-Nov-2015 00:09:04.215 INFO [Membership-MemberAdded.] org.apache.catalina.ha.tcp.SimpleTcpCluster.memberAdded Replication member added:org.apache.catalina.tribes.membership.MemberImpl[tcp://{192, 168, 20, 2}:4000,{192, 168, 20, 2},4000, alive=1036, securePort=-1, UDP Port=-1, id={-40 -58 -73 -47 -114 -18 76 74 -81 -66 125 -30 -36 -78 -87 -23}, payload={}, command={}, domain={},]
测试发现尽管负载均衡切换了主机,但是 session 不会改变
同理,使用 Mod_jk 和 ajp 连接后端也成功,使用 nginx 做反代也行,这里就不缀余了
使用 msm 实现 session 服务器实现:
借助于 memcached:
yum install memcached
[root@node3 ~]# cat /etc/sysconfig/memcached
PORT=”11211″
USER=”memcached”
MAXCONN=”1024″
CACHESIZE=”64″
OPTIONS=””
提供四个 java 类库:
[root@node1 msm-1.8.3]# ls
memcached-session-manager-1.8.3.jar msm-javolution-serializer-1.8.3.jar
memcached-session-manager-tc8-1.8.3.jar spymemcached-2.10.2.jar
javolution-5.5.1.jar
放置于两台 tomcat 服务器的 /usr/local/tomcat/lib 目录下:
[root@node1 ~]# scp -r msm-1.8.3/ node2:/usr/local/tomcat/lib
The authenticity of host ‘node2 (192.168.20.2)’ can’t be established.
RSA key fingerprint is d5:69:d0:fc:ce:90:14:14:6d:4c:52:82:53:a5:ed:0b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘node2,192.168.20.2’ (RSA) to the list of known hosts.
root@node2’s password:
spymemcached-2.10.2.jar 100% 429KB 428.8KB/s 00:00
memcached-session-manager-tc8-1.8.3.jar 100% 10KB 10.2KB/s 00:00
msm-javolution-serializer-1.8.3.jar 100% 69KB 69.4KB/s 00:00
memcached-session-manager-1.8.3.jar 100% 144KB 143.6KB/s 00:00
javolution-5.5.1.jar 100% 144KB 143.6KB/s 00:00
编辑 server.xml 文件 Host 上下文中定义 context
<Context path=”” docBase=”/data/webapps” reloadable=”true”>
<Manager className=”de.javakaffee.web.msm.MemcachedBackupSessionManager”
memcachedNodes=”n1:192.168.20.7:11211,n2:192.168.20.8:11211″
failoverNodes=”n1″
requestUriIgnorePattern=”.*\.(ico|png|gif|jpg|css|js)$”
transcoderFactoryClass=”de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory”
/>
<Valve className=”org.apache.catalina.valves.RemoteAddrValve”
deny=”172\.16\.100\.100″/>
<Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”/data/logs”
prefix=”web1_access_log” suffix=”.txt”
pattern=”%h %l %u %t "%r" %s %b” />
</Context>
更多 Tomcat 相关教程见以下内容:
CentOS 6.6 下安装配置 Tomcat 环境 http://www.linuxidc.com/Linux/2015-08/122234.htm
RedHat Linux 5.5 安装 JDK+Tomcat 并部署 Java 项目 http://www.linuxidc.com/Linux/2015-02/113528.htm
Tomcat 权威指南(第二版)(中英高清 PDF 版 + 带书签) http://www.linuxidc.com/Linux/2015-02/113062.htm
Tomcat 安全配置与性能优化 http://www.linuxidc.com/Linux/2015-02/113060.htm
Linux 下使用 Xshell 查看 Tomcat 实时日志中文乱码解决方案 http://www.linuxidc.com/Linux/2015-01/112395.htm
CentOS 64-bit 下安装 JDK 和 Tomcat 并设置 Tomcat 开机启动操作步骤 http://www.linuxidc.com/Linux/2015-01/111485.htm
CentOS 6.5 下安装 Tomcat http://www.linuxidc.com/Linux/2015-01/111415.htm
Tomcat 中 session 的管理机制 http://www.linuxidc.com/Linux/2016-09/135072.htm
Tomcat 的详细介绍:请点这里
Tomcat 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-12/1387550.htm