<td><%=session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
[root@TomcatB ~]# ss -tnl
State Recv-Q Send-Q LocalAddress:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
[root@TomcatB ~]# systemctl start tomcat
[root@TomcatB ~]# ss -tnl
State Recv-Q Send-Q LocalAddress:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 :::8009 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
[root@TomcatB ~]#
测试两台tomcat服务器工作是否正常
使用浏览器访问结果如图所示
接下来就需要配置前端的反代服务器,以及tomcat集群session复制了
httpd反代tomcat集群配置
在这里我们为了节约资源就把httpd反代服务器配置到TomcatA服务器上
[root@TomcatA ~]# yum install -y httpd
[root@TomcatA ~]# vim/etc/httpd/conf.d/httpd_tomcat.conf
[root@TomcatA ~]# cat/etc/httpd/conf.d/httpd_tomcat.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#LoadModule proxy_modulemodules/mod_proxy.so #LoadModule proxy_ajp_modulemodules/mod_proxy_ajp.so #LoadModule proxy_balancer_modulemodules/mod_proxy_balancer.so <proxy balancer://tomcatcluster> BalancerMemberajp://192.168.0.20:8009 route=TomcatA loadfactor=1 BalancerMemberajp://192.168.0.21:8009 route=TomcatB loadfactor=1 ProxySetlbmethod=byrequests #ProxySetstickysession=JSESSIONID </Proxy> <VirtualHost *:80> ServerNameproxy.test.com ProxyViaOn ProxyRequestsOff ProxyPreserveHostOn <Proxy*> Requireall granted </Proxy> ProxyPass/ balancer://tomcatcluster/ ProxyPassReverse/ balancer://tomcatcluster/ <Location/> Requireall granted </Location> <Location/balancer-manager> SetHandlerbalancer-manager ProxyPass! Requireall granted </Location> </VirtualHost>
[root@TomcatA ~]# vim/etc/httpd/conf/httpd.conf
修改如下内容
1 2 3 4
ServerName :80 <IfModule dir_module> DirectoryIndex index.jsp </IfModule>
[root@TomcatA ~]# httpd -t
Syntax OK
[root@TomcatA ~]# cp/etc/tomcat/server.xml{,.bak}
[root@TomcatA ~]# vim/etc/tomcat/server.xml
默认配置文件需要修改以下几个地方
<EnginedefaultHost="localhost" jvmRoute="TomcatA">
Engine标签下添加集群配置内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <ManagerclassName="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/> <ChannelclassName="org.apache.catalina.tribes.group.GroupChannel"> <MembershipclassName="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4" port="45564" frequency="500" dropTime="3000"/> <ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver" address="192.168.0.20" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> </Channel> <ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <DeployerclassName="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> <ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>
修改日志格式为httpd combined格式
1 2 3 4
<Valve className="org.apache.catalina.valves.AccessLogValve"directory="logs" prefix="localhost_access_log."suffix=".txt" pattern="%h %l %u %t"%r" %s %b "%{Referer}i" "%{User-Agent}i"" />
[root@TomcatB ~]# mv/etc/tomcat/server.xml{,.bak}
[root@TomcatA ~]# scp/etc/tomcat/server.xml 192.168.0.21:/etc/tomcat/
root@192.168.0.21's password: