时间:2021-07-01 10:21:17 帮助过:20人阅读
思路
实现
<Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcat127"><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.1.108"port="4000"autoBind="100"selectorTimeout="5000"maxThreads="6" /><SenderclassName="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><ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve"filter="" /><ValveclassName="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" /><ClusterListener
className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/><ClusterListener
className="org.apache.catalina.ha.session.ClusterSessionListener"/>Cluster>
...
Engine>
"Catalina" defaultHost="localhost" jvmRoute="tomcat132">
"org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOpti>"8">
"org.apache.catalina.ha.session.DeltaManager"
expireSessi>"false" notifyListeners>"true" />
"org.apache.catalina.tribes.group.GroupChannel">
"org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4" port="45564" frequency="500" dropTime="3000" />
"org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.25.132" port="4000" autoBind="100" selectorTimeout="5000"
maxThreads="6" />
...
...
upstream backendServer
{
ip_hash; #可选,根据来源IP方式选择web服务器,省略的话按默认的轮循方式选择web服务器server127.0.0.1:8080; #虚拟机本机Tomcatserver192.168.1.108:8080; #Window上Tomcat
}
server {
#设置监听端口
listen 80;
charset utf-8;
#设置服务器域名(IP访问和多域名访问可不设置)#server_name _*;#server_name www.test.com;#开启shtml支持
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;
#设置主访问日志#access_log logs/access.log main;
access_log /dev/null;
error_page 404 /404.html;
error_page 500502503504 /50x.html;
location /
{
proxy_pass http://backendServer;
include proxy.conf;
}
}
<distributable/>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><html><head><basehref="<%=basePath%>"><title>首页title><metahttp-equiv="pragma"content="no-cache"><metahttp-equiv="cache-control"content="no-cache"><metahttp-equiv="expires"content="0"><metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"><metahttp-equiv="description"content="This is my page">head><body><p>sessionID:<%=session.getId() %>p><p>session创建时间:<%=session.getCreationTime() %>p><p>服务器IP:<%=request.getRemoteHost() %>p>body>html>
测试
启动两个Tomcat,测试结果如下,则说明配置负载+集群+Session共享成功。
Nginx默认是就近IP访问本机Linux下tomcat,效果图如下
关闭Linux下的tomcat,刷新后,则是访问Window下tomcat
请注意查看Session的创建时间,是一致的说明成功了。
以上就介绍了Tomcat集群与Session共享 + Nginx负载,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。