tomcat负载均衡配置代码大全

网络 网络优化 网络运维
本文主要针对负载均衡配置的代码进行了一个完整的归纳和总结。其中,将配置的每一部都有详细的说明和注释,另外还包括了集群配置与应用配置的相关代码介绍。

前面我们介绍了tomcat负载均衡配置的安装过程,接下来就进入我们的重点了——tomcat负载均衡配置代码内容。当然,我们在正确安装之后才能进行到这一步。之后还包括tomacat集群配置和应用配置。相关的代码介绍的比较详细,希望对大家有所帮助。

tomcat负载均衡配置过程

(1)在那台要安装apache的服务器上安装apache2.0.55,我的安装路径为默认C:\Program Files\Apache Group\Apache2

(2)安装后测试apache能否正常启动,调试到能够正常启动http://192.168.0.88

(3)下载jk2.0.4后解压缩文件

(4)将解压缩后的目录中的modules目录中的mod_jk2.so文件复制到apache的安装目录下的modules目录中,我的为C:\Program Files\Apache Group\Apache2\modules

(5)修改apache的安装目录中的conf目录的配置文件httpd.conf,在文件中加LoadModule模块配置信息的最后加上一句LoadModule jk2_module modules/mod_jk2.so

(6)分别修改三个tomcat的配置文件conf/server.xml,修改内容如下

修改前

  1. <!-- An Engine represents the entry point (within Catalina) that processes  
  2.          every request.  The Engine implementation for Tomcat stand alone  
  3.          analyzes the HTTP headers included with the request, and passes them  
  4.          on to the appropriate Host (virtual host). -->  
  5.  
  6.     <!-- You should set jvmRoute to support load-balancing via AJP ie :  
  7.     <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">           
  8.     -->   
  9.            
  10.     <!-- Define the top level container in our container hierarchy -->  
  11.     <Engine name="Catalina" defaultHost="localhost"

#p#修改后

  1. <!-- An Engine represents the entry point (within Catalina) that processes  
  2.      every request.  The Engine implementation for Tomcat stand alone  
  3.      analyzes the HTTP headers included with the request, and passes them  
  4.      on to the appropriate Host (virtual host). -->  
  5.  
  6. <!-- You should set jvmRoute to support load-balancing via AJP ie :-->  
  7. <Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat1">           
  8.    
  9.        
  10. <!-- Define the top level container in our container hierarchy   
  11. <Engine name="Catalina" defaultHost="localhost">  
  12. --> 

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

(7)然后重启三个tomcat,调试能够正常启动。

(8)在apache的安装目录中的conf目录下创建文件workers2.propertie,写入文件内容如下

  1. # fine the communication channel   
  2. [channel.socket:192.168.0.1:8009]   
  3. info=Ajp13 forwarding over socket  
  4. #配置第一个服务器   
  5. tomcatId=tomcat1 #要和tomcat的配置文件server.xml中的jvmRoute="tomcat1"名称一致  
  6. debug=0   
  7. lb_factor=1 #负载平衡因子,数字越大请求被分配的几率越高  
  8.  
  9. # Define the communication channel   
  10. [channel.socket:192.168.0.2:8009]   
  11. info=Ajp13 forwarding over socket  
  12. tomcatId=tomcat2   
  13. debug=0   
  14. lb_factor=1   
  15.  
  16. # Define the communication channel   
  17. [channel.socket:192.168.0.4:8009]   
  18. info=Ajp13 forwarding over socket  
  19. tomcatId=tomcat3   
  20. debug=0   
  21. lb_factor=1   
  22.  
  23. [status:]   
  24. info=Status worker, displays runtime information.    
  25.  
  26. [uri:/jkstatus.jsp]   
  27. info=Display status information and checks the config file for changes.   
  28. group=status:   
  29.  
  30. [uri:/*]   
  31. info=Map the whole webapp   
  32. debug=0 

#p#(9)在三个tomcat的安装目录中的webapps建立相同的应用,我和应用目录名为TomcatDemo,在三个应用目录中建立相同 WEB-INF目录和页面index.jsp,index.jsp的页面内容如下

  1. <%@ page contentType="text/html; charset=GBK" %>  
  2. <%@ page import="java.util.*" %>  
  3. <html><head><title>Cluster App Test</title></head>  
  4. <body>  
  5. Server Info:  
  6. <%  
  7. out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>  
  8. <%  
  9.   out.println("<br> ID " + session.getId()+"<br>");  
  10.  
  11.   // 如果有新的 Session 属性设置  
  12.   String dataName = request.getParameter("dataName");  
  13.   if (dataName != null && dataName.length() > 0) {  
  14.      String dataValue = request.getParameter("dataValue");  
  15.      session.setAttribute(dataName, dataValue);  
  16.   }  
  17.  
  18.   out.print("<b>Session 列表</b>");  
  19.  
  20.   Enumeration e = session.getAttributeNames();  
  21.   while (e.hasMoreElements()) {  
  22.      String name = (String)e.nextElement();  
  23.      String value = session.getAttribute(name).toString();  
  24.      out.println( name + " = " + value+"<br>");  
  25.          System.out.println( name + " = " + value);  
  26.    }  
  27. %>  
  28.   <form action="index.jsp" method="POST">  
  29.     名称:<input type=text size=20 name="dataName">  
  30.      <br>  
  31.     值:<input type=text size=20 name="dataValue">  
  32.      <br>  
  33.     <input type=submit>  
  34.    </form>  
  35. </body>  
  36. </html> 

(10)重启apache服务器和三个tomcat服务器,到此tomcat负载均衡配置完成。测试负载均衡先测试apache,访问http://192.168.0.88/jkstatus.jsp

能否正常访问,并查询其中的内容,有三个tomcat的相关配置信息和负载说明,访问http://192.168.0.88/TomcatDemo/index.jsp看能够运行,能运行,则已建立负载均衡。

责任编辑:佟健 来源: 互联网
相关推荐

2014-07-28 11:37:49

NginxTomcat

2010-04-20 22:36:52

负载均衡配置

2010-04-22 16:41:56

负载均衡端口规则

2010-04-20 21:27:28

tomcat负载均衡配

2010-05-05 23:27:32

负载均衡配置

2010-04-21 12:28:50

Oracle负载均衡

2010-05-04 16:10:51

负载均衡算法

2018-09-13 08:47:09

Tomcat负载均衡服务器

2020-03-11 19:38:39

Tomcat负载均衡配置

2010-04-21 12:57:33

RAC负载均衡配置

2010-04-20 17:34:21

Linux双网卡负载均衡

2010-05-04 13:23:55

Tomcat负载均衡

2010-05-06 15:24:35

Tomcat负载均衡

2010-05-06 15:04:51

Tomcat负载均衡

2010-04-22 23:47:55

tomcat负载均衡设

2010-04-22 10:09:28

负载均衡器

2010-04-22 23:07:47

服务器负载均衡

2010-04-22 12:45:05

2010-04-23 10:03:42

tomcat负载均衡技术

2011-04-18 10:04:24

apachetomcat
点赞
收藏

51CTO技术栈公众号