作者:司徒琪瑶_186 | 来源:互联网 | 2017-05-13 02:18
虽然曾经做过,却经常性忘记,特将其记录,以备后续查看。
相关资料:
Linux下Tomcat运行环境搭建详见另一片博文:
http://blog.csdn.net/xiadi934/article/details/50911175
Tomcat7.0官方集群文档,具体细节参数请到该处查阅:
http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html
Linux下搭建Nginx + Tomcat还有问题的请绕过。
思路
- 使用两台机器,本机(window,IP:192.168.1.108) + 虚拟机(Linux,IP:192.168.25.132)
- Linux上部署 nginx + tomcat7 + web,Window上部署 : tomcat7 + web
- 参考Tomcat官方集群例子, 采用Tomcat本身自带集群与Session共享
实现
- Window上Tomcat下server.xml文件配置,其余配置均不变,只修改以下配置
<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>
- Linux上Tomcat下server.xml文件配置,其余配置均不变,加上同上配置
"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;
}
}
- 修改两个Tomcat下conf/web.xml,尾部加入以下代码
<distributable/>
- 加入测试页面,在两个Tomcat/webapps下创建文件夹test,并创建index.jsp,代码如下
<%@ 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的创建时间,是一致的说明成功了。
').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('
').text(i));
};
$numbering.fadeIn(1700);
});
});
以上就介绍了Tomcat集群与Session共享 + Nginx负载,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。