Tomcat集群和负载均衡的实现(session同步)

2017-08-02  本文已影响0人  0xkevin

(一) 环境说明

(二) 安装过程

<Connector port="7080" maxHttpHeaderSize="8192"   
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"   
               enableLookups="false" redirectPort="8443" acceptCount="100"   
               connectionTimeout="20000" disableUploadTimeout="true" />  
<Connector port="8888" maxHttpHeaderSize="8192"   
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"   
               enableLookups="false" redirectPort="8443" acceptCount="100"   
               connectionTimeout="20000" disableUploadTimeout="true" />  
<Connector port="9999" maxHttpHeaderSize="8192"   
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"   
               enableLookups="false" redirectPort="8443" acceptCount="100"   
               connectionTimeout="20000" disableUploadTimeout="true" />  

http://192.168.0.1:7080
http://192.168.0.2:8888
http://192.168.0.4:9999

(三) 负载均衡配置过程

将其中的jvmRoute="jvm1"分别修改为jvmRoute="tomcat1"、jvmRoute="tomcat2"和jvmRoute="tomcat3"

    <!-- An Engine represents the entry point (within Catalina) that processes   
         every request.  The Engine implementation for Tomcat stand alone   
         analyzes the HTTP headers included with the request, and passes them   
         on to the appropriate Host (virtual host). -->   
  
    <!-- You should set jvmRoute to support load-balancing via AJP ie :-->   
    <Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat1">         
       
          
    <!-- Define the top level container in our container hierarchy   
    <Engine name="Catalina" defaultHost="localhost">   
    --> 
# Define the communication channel   
[channel.socket:192.168.0.1:8009]   
info=Ajp13 forwarding over socket   
#配置第一个服务器   
tomcatId=tomcat1 #要和tomcat的配置文件server.xml中的jvmRoute="tomcat1"名称一致   
debug=0   
lb_factor=1 #负载平衡因子,数字越大请求被分配的几率越高   
  
# Define the communication channel   
[channel.socket:192.168.0.2:8009]   
info=Ajp13 forwarding over socket   
tomcatId=tomcat2   
debug=0   
lb_factor=1   
  
# Define the communication channel   
[channel.socket:192.168.0.4:8009]   
info=Ajp13 forwarding over socket   
tomcatId=tomcat3   
debug=0   
lb_factor=1   
  
[status:]   
info=Status worker, displays runtime information.    
  
[uri:/jkstatus.jsp]   
info=Display status information and checks the config file for changes.   
group=status:   
  
[uri:/*]   
info=Map the whole webapp   
debug=0   
<%@ page contentType="text/html; charset=GBK" %>   
<%@ page import="java.util.*" %>   
<html><head><title>Cluster App Test</title></head>   
<body>   
Server Info:   
<%   
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>   
<%   
  out.println("<br> ID " + session.getId()+"<br>");   
  
  // 如果有新的 Session 属性设置   
  String dataName = request.getParameter("dataName");   
  if (dataName != null && dataName.length() > 0) {   
     String dataValue = request.getParameter("dataValue");   
     session.setAttribute(dataName, dataValue);   
  }   
  
  out.print("<b>Session 列表</b>");   
  
  Enumeration e = session.getAttributeNames();   
  while (e.hasMoreElements()) {   
     String name = (String)e.nextElement();   
     String value = session.getAttribute(name).toString();   
     out.println( name + " = " + value+"<br>");   
         System.out.println( name + " = " + value);   
   }   
%>   
  <form action="index.jsp" method="POST">   
    名称:<input type=text size=20 name="dataName">   
     <br>   
    值:<input type=text size=20 name="dataValue">   
     <br>   
    <input type=submit>   
   </form>   
</body>   
</html>  

(四) tomcat集群配置

        <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"   
                 managerClassName="org.apache.catalina.cluster.session.DeltaManager"  
                  expireSessionsOnShutdown="false"   
                 useDirtyFlag="true"   
                 notifyListenersOnReplication="true">   
  
            <Membership   
                className="org.apache.catalina.cluster.mcast.McastService"   
                mcastAddr="228.0.0.4"   
                mcastPort="45564"   
                mcastFrequency="500"   
                mcastDropTime="3000"/>   
  
            <Receiver   
                className="org.apache.catalina.cluster.tcp.ReplicationListener"   
                tcpListenAddress="auto"   
                tcpListenPort="4001"   
                tcpSelectorTimeout="100"   
                tcpThreadCount="6"/>   
  
            <Sender   
                className="org.apache.catalina.cluster.tcp.ReplicationTransmitter"  
                 replicationMode="pooled"   
                ackTimeout="5000"/>   
  
            <Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"   
                   filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>  
                     
            <Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer"  
                       tempDir="/tmp/war-temp/"   
                      deployDir="/tmp/war-deploy/"   
                      watchDir="/tmp/war-listen/"   
                      watchEnabled="false"/>   
                        
            <ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/>  
         </Cluster> 

将集群配置选项的注释放开即可

(五) 应用配置

对于要进行负载和集群的的tomcat目录下的webapps中的应用中的WEB-INF中的web.xml文件要添加如下一句配置 <distributable/>

<?xml version="1.0" encoding="UTF-8"?>   
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">  
   <display-name>TomcatDemo</display-name>   
   <distributable/>   
</web-app>

补充:
因为tomcat的session同步功能需要用到组播,windows默认情况下是开通组播服务的,但是linux默认情况下并没有开通,可以通过指令打开route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0,如果需要服务器启动时即开通组播需在/etc/sysconfig/static-routes文件内加入eht0 net 224.0.0.0 netmask 240.0.0.0。具体组播概念请查阅CCNP相关内容。

上一篇 下一篇

猜你喜欢

热点阅读