• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Tomcat集群与Session共享 + Nginx负载

php 搞代码 4年前 (2022-01-23) 26次浏览 已收录 0个评论

虽然曾经做过,却经常性忘记,特将其记录,以备后续查看。
相关资料:
Linux下Tomcat运行环境搭建详见另一片博文:http://blog.gaodaima.com/xiadi934/article/details/50911175
Tomcat7.0官方集群文档,具体细节参数请到该处查阅:http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html
Linux下搭建Nginx + Tomcat还有问题的请绕过。

思路

  1. 使用两台机器,本机(window,IP:192.168.1.108) + 虚拟机(Linux,IP:192.168.25.132)
  2. Linux上部署 nginx + tomcat7 + web,Window上部署 : tomcat7 + web
  3. 参考Tomcat官方集群例子, 采用Tomcat本身自带集群与Session共享

实现

  • Window上Tomcat下server.xml文件配置,其余配置均不变,只修改以下配置
<code><span><<span>Engine</span><span>name</span>=<span>"Catalina"</span><span>defaultHost</span>=<span>"localhost"</span><span>jvmRoute</span>=<span>"tomcat127"</span>></span><span><<span>Cluster</span><span>className</span>=<span>"org.apache.catalina.ha.tcp.SimpleTcpCluster"</span><span>channelSendOptions</span>=<span>"8"</span>></span><span><<span>Manager</span><span>className</span>=<span>"org.apache.catalina.ha.session.DeltaManager"</span><span>expireSessionsOnShutdown</span>=<span>"false"</span><span>notifyListenersOnReplication</span>=<span>"true"</span> /></span><span><<span>Channel</span><span>className</span>=<span>"org.apache.catalina.tribes.group.GroupChannel"</span>></span><span><<span>Membership</span><span>className</span>=<span>"org.apache.catalina.tribes.membership.McastService"</span><span>address</span>=<span>"228.0.0.4"</span><span>port</span>=<span>"45564"</span><span>frequency</span>=<span>"500"</span><span>dropTime</span>=<span>"3000"</span> /></span><span><!---ecms -ecms 广播地址与端口采用默认--></span><span><<span>Receiver</span><span>className</span>=<span>"org.apache.catalina.tribes.transport.nio.NioReceiver"</span><span>address</span>=<span>"192.168.1.108"</span><span>port</span>=<span>"4000"</span><span>autoBind</span>=<span>"100"</span><span>selectorTimeout</span>=<span>"5000"</span><span>maxThreads</span>=<span>"6"</span> /></span><span><!---ecms -ecms 本机局域网IP,端口默认--></span><span><<span>Sender</span><span>className</span>=<span>"org.apache.catalina.tribes.transport.ReplicationTransmitter"</span>></span><span><<span>Transport</span><span>className</span>=<span>"org.apache.catalina.tribes.transport.nio.PooledParallelSender"</span> /></span><span></<span>Sender</span>></span><span><<span>Interceptor</span><span>className</span>=<span>"org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"</span> /></span><span><<span>Interceptor</span><span>className</span>=<span>"org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"</span> /></span><span></<span>Channel</span>></span><span><<span>Valve</span><span>className</span>=<span>"org.apache.catalina.ha.tcp.ReplicationValve"</span><span>filter</span>=<span>""</span> /></span><span><<span>Valve</span><span>className</span>=<span>"org.apache.catalina.ha.session.JvmRouteBinderValve"</span> /></span><span><<span>Deployer</span><span>className</span>=<span>"org.apache.catalina.ha.deploy.FarmWarDeployer"</span><span>tempDir</span>=<span>"/tmp/war-temp/"</span><span>deployDir</span>=<span>"/tmp/war-deploy/"</span><span>watchDir</span>=<span>"/tmp/war-listen/"</span><span>watc<strong>(本文来源gaodai#ma#com搞@@代~&码网</strong><pre>搞代gaodaima码

hEnabled=“false” /><ClusterListenerclassName=“org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener”/><ClusterListenerclassName=“org.apache.catalina.ha.session.ClusterSessionListener”/></Cluster></Engine>

  • Linux上Tomcat下server.xml文件配置,其余配置均不变,加上同上配置
<code><Engine name=<span>"Catalina"</span> defaultHost=<span>"localhost"</span> jvmRoute=<span>"tomcat132"</span>>    <Cluster className=<span>"org.apache.catalina.ha.tcp.SimpleTcpCluster"</span> channelSendOpti>"8"</span>>        <Manager className=<span>"org.apache.catalina.ha.session.DeltaManager"</span>          expireSessi>"false"</span> notifyListeners>"true"</span> />        <Channel className=<span>"org.apache.catalina.tribes.group.GroupChannel"</span>>          <Membership className=<span>"org.apache.catalina.tribes.membership.McastService"</span>            address=<span>"228.0.0.4"</span> port=<span>"45564"</span> frequency=<span>"500"</span> dropTime=<span>"3000"</span> />          <Receiver className=<span>"org.apache.catalina.tribes.transport.nio.NioReceiver"</span>            address=<span>"192.168.25.132"</span> port=<span>"4000"</span> autoBind=<span>"100"</span> selectorTimeout=<span>"5000"</span>            maxThreads=<span>"6"</span> /><!---ecms -ecms  虚拟机的局域网IP -->            <span>...</span>       <span>...</span> </code>
  • 修改Nginx配置,加入负载均衡
<code>upstream backendServer{       ip_hash; <span>#可选,根据来源IP方式选择web服务器,省略的话按默认的轮循方式选择web服务器</span><span>server</span><span>127.0</span><span>.0</span><span>.1</span>:<span>8080</span>; <span>#虚拟机本机Tomcat</span><span>server</span><span>192.168</span><span>.1</span><span>.108</span>:<span>8080</span>; <span>#Window上Tomcat</span>}<span>server</span> {    <span>#设置监听端口</span>    listen <span>80</span>;    charset utf-<span>8</span>;    <span>#设置服务器域名(IP访问和多域名访问可不设置)</span><span>#server_name _*;</span><span>#server_name  www.test.com;</span><span>#开启shtml支持</span>    ssi on;    ssi_silent_errors on;    ssi_types text/shtml;    <span>#设置主访问日志</span><span>#access_log logs/access.log main;</span>    access_log  /dev/<span>null</span>;    error_page  <span>404</span>                  /<span>404.</span>html;    error_page  <span>500</span><span>502</span><span>503</span><span>504</span>      /<span>50</span>x.html;    location /    {        proxy_pass http:<span>//backendServer;</span>        include proxy.conf;    }}</code>
  • 修改两个Tomcat下conf/web.xml,尾部加入以下代码
<code><span><<span>distributable</span>/></span><span><!---ecms -ecms  允许Session分配 --></span></code>
  • 加入测试页面,在两个Tomcat/webapps下创建文件夹test,并创建index.jsp,代码如下
<code><span><%@ page language=<span>"java"</span> import=<span>"java.util.*"</span> pageEncoding=<span>"utf-8"</span>%></span><span><%<span>String</span> path = <span>request</span>.getContextPath();<span>String</span> basePath = <span>request</span>.getScheme()+<span>"://"</span>+<span>request</span>.getServerName()+<span>":"</span>+<span>request</span>.getServerPort()+path+<span>"/"</span>;%></span><span></span><span><<span>html</span>></span><span><<span>head</span>></span><span><<span>base</span><span>href</span>=<span>"<%=basePath%>"</span>></span><span><<span>title</span>></span>首页<span></<span>title</span>></span><span><<span>meta</span><span>http-equiv</span>=<span>"pragma"</span><span>content</span>=<span>"no-cache"</span>></span><span><<span>meta</span><span>http-equiv</span>=<span>"cache-control"</span><span>content</span>=<span>"no-cache"</span>></span><span><<span>meta</span><span>http-equiv</span>=<span>"expires"</span><span>content</span>=<span>"0"</span>></span><span><<span>meta</span><span>http-equiv</span>=<span>"keywords"</span><span>content</span>=<span>"keyword1,keyword2,keyword3"</span>></span><span><<span>meta</span><span>http-equiv</span>=<span>"description"</span><span>content</span>=<span>"This is my page"</span>></span><span><!---ecms -ecms     <link rel="stylesheet" type="text/css" href="styles.css">    --></span><span></<span>head</span>></span><span><<span>body</span>></span><span><<span>p</span>></span>sessionID:<span><%=session.getId() %></span><span></<span>p</span>></span><span><<span>p</span>></span>session创建时间:<span><%=session.getCreationTime() %></span><span></<span>p</span>></span><span><<span>p</span>></span>服务器IP:<span><%=<span>request</span>.getRemoteHost() %></span><span></<span>p</span>></span><span></<span>body</span>></span><span></<span>html</span>></span></code>

测试

启动两个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); }); }); </script>

    以上就介绍了Tomcat集群与Session共享 + Nginx负载,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


    搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
    转载请注明原文链接:Tomcat集群与Session共享 + Nginx负载

    喜欢 (0)
    [搞代码]
    分享 (0)
  • 发表我的评论
    取消评论

    表情 贴图 加粗 删除线 居中 斜体 签到

    Hi,您需要填写昵称和邮箱!

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址