tomcat集群

实现tomcat集群:

环境说明

1、安装Apache  httpd-2.2.21-win32-x86-no_ssl.msi

2、apache-tomcat-7.0.54.zip(复制两份)

配置说明:

1、保证两个tomcat不冲突,修改其中的一个就可以了

2、修改其中tomcatA(我这样为tomcat命名)的server.xml配置文件

    <Server port="8006" shutdown="SHUTDOWN">  (port修改成了8006 不冲突就行)

    <Connector port="8180" protocol="HTTP/1.1"   connectionTimeout="20000"
               redirectPort="8543" /> (port和redirectPort  不与另外tomcat中配置文件起冲突)

    <Connector port="8010" protocol="AJP/1.3" redirectPort="8543" /> (同理)

  

    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> (Engine节点 新增jvmRoute属性 jvm1 随便定义 另外tomcat配置文件中 Engine也需要新增JvmRoute属性如:jvmRoute="jvm2")

tomcat中的配置就已经完成了。接下来就修改安装Apache中的了,

1、找到安装目录如:C:\Program Files\Apache Software Foundation\Apache2.2\conf 中 httpd.conf 文件

#LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule proxy_http_module modules/mod_proxy_http.so

#Include conf/extra/httpd-vhosts.conf

将这些的注释去掉

在最后加上

ProxyRequests Off 
<proxy balancer://cluster> 
    BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=jvm2
    BalancerMember ajp://127.0.0.1:8010 loadfactor=1 route=jvm1
</proxy>

8009 是引用 Connector节点中的端口号

route=jvm2  就是Engine节点中的jvmRoute属性了

2、在找到C:\Program Files\Apache Software Foundation\Apache2.2\conf\extra  中的httpd-vhosts.conf文件

 <VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/docs/dummy-host.think-net.local"
   ServerName dummy-host.think-net.local
    ServerAlias www.dummy-host.think-net.local
    ErrorLog "logs/dummy-host.think-net.local-error.log"
    CustomLog "logs/dummy-host.think-net.local-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/docs/dummy-host2.think-net.local"
    ServerName dummy-host2.think-net.local
    ErrorLog "logs/dummy-host2.think-net.local-error.log"
    CustomLog "logs/dummy-host2.think-net.local-access.log" common
</VirtualHost>

将这段代码注释掉或者删除

在下面加上

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    ServerName localhost 
    ServerAlias localhost 
    ProxyPass / balancer://cluster/ 
    ProxyPassReverse / balancer://cluster/ 
    ErrorLog "logs/dummy-host2.sm.com-error.log" 
    CustomLog "logs/dummy-host2.sm.com-access.log" common 
</VirtualHost>

tomcat集群的配置就已经完成了,下面就是测试了。

编写一个测试的index.jsp 把它放到两个tomcat中去 启动两个tomcat

在浏览器中 http://localhost/tomcatColony/index.jsp 刷新就可以看到效果了。

index.jsp代码

<%@ 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>

相关推荐