实战负载均衡配置的步骤大全

网络 网络优化 网络运维
对于一个软件环境的负载均衡配置做了非常具体的记录。首先,是安装环境的说明,之后是安装过程的介绍,然后才是具体的配置过程。

负载均衡的概念来源于集群的定义,集群中的一项技术就是负载均衡。那么作为服务器的一项规划和分配的方式,我们如何在系统中达成这个目的呢?下面就为大家介绍一下如何搞定负载均衡配置。首先要了解我们的使用环境。那么本文的例子是4台服务器的硬件环境。

(一)负载均衡配置环境说明
(1)服务器有4台,一台安装apache,三台安装tomcat
(2)apache2.0.55、tomcat5.5.15、jk2.0.4、jdk1.5.6或jdk1.4.2
(3)ip配置,一台安装apache的ip为192.168.0.88,三台安装tomcat的服务器ip分别为192.168.0.1/2/4

(二)负载均衡配置的软件安装过程
(1)在三台要安装tomcat的服务器上先安装jdk
(2)配置jdk的安装路径,在环境变量path中加入jdk的bin路径,新建环境变量JAVA_HOME指向jdk的安装路径
(3)在三台要安装tomcat的服务器上分别安装tomcat,调试三个tomcat到能够正常启动
(4)tomcat的默认WEB服务端口是8080,默认的模式是单独服务,我的三个tomcat的WEB服务端口修改为7080/8888/9999
修改位置为tomcat的安装目录下的conf/server.xml
修改前的配置为    

  1. <Connector port="8080" maxHttpHeaderSize="8192" 
  2.                 maxThreads="150" minSpareThreads="25" maxSpareThreads="75" 
  3.                 enableLookups="false" redirectPort="8443" acceptCount="100" 
  4.                 connectionTimeout="20000" disableUploadTimeout="true" /> 

修改后的配置为    

  1. <Connector port="7080" maxHttpHeaderSize="8192" 
  2.                maxThreads="150" minSpareThreads="25" maxSpareThreads="75" 
  3.                enableLookups="false" redirectPort="8443" acceptCount="100" 
  4.                connectionTimeout="20000" disableUploadTimeout="true" /> 

依次修改每个tomcat的监听端口(7080/8888/9999)
(5)分别测试每个tomcat的启动是否正常
http://192.168.0.1:7080
http://192.168.0.2:8888
http://192.168.0.4:9999/#p#

(三)负载均衡配置过程
(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"

修改后    

  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.      <!-- You should set jvmRoute to support load-balancing via AJP ie :-->  
  6.      <Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat1">    
  7.      <!-- Define the top level container in our container hierarchy  
  8.      <Engine name="Catalina" defaultHost="localhost">  
  9.      --> 

将其中的jvmRoute="jvm1"分别修改为jvmRoute="tomcat1"和jvmRoute="tomcat2"和 jvmRoute="tomcat3"
(7)然后重启三个tomcat,调试能够正常启动。
(8)在apache的安装目录中的conf目录下创建文件workers2.propertie,写入文件内容如下#p#

  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. # Define the communication channel  
  9. [channel.socket:192.168.0.2:8009]  
  10. info=Ajp13 forwarding over socket  
  11. tomcatId=tomcat2  
  12. debug=0  
  13. lb_factor=1  
  14. # Define the communication channel  
  15. [channel.socket:192.168.0.4:8009]  
  16. info=Ajp13 forwarding over socket  
  17. tomcatId=tomcat3  
  18. debug=0  
  19. lb_factor=1  
  20. [status:]  
  21. info=Status worker, displays runtime information.    
  22. [uri:/jkstatus.jsp]  
  23. info=Display status information and checks the config file for changes.  
  24. group=status:  
  25. [uri:/*]  
  26. info=Map the whole webapp  
  27. debug=0 

(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.    // 如果有新的 Session 属性设置  
  11.    String dataName = request.getParameter("dataName");  
  12.    if (dataName != null && dataName.length() > 0) {  
  13.       String dataValue = request.getParameter("dataValue");  
  14.       session.setAttribute(dataName, dataValue);  
  15.    }  
  16.    out.print("<b>Session 列表</b>");  
  17.    Enumeration e = session.getAttributeNames();  
  18.    while (e.hasMoreElements()) {  
  19.       String name = (String)e.nextElement();  
  20.       String value = session.getAttribute(name).toString();  
  21.       out.println( name + " = " + value+"<br>");  
  22.           System.out.println( name + " = " + value);  
  23.     }  
  24. %>  
  25.    <form action="index.jsp" method="POST">  
  26.      名称:<input type=text size=20 name="dataName">  
  27.       <br>  
  28.      值:<input type=text size=20 name="dataValue">  
  29.       <br>  
  30.      <input type=submit>  
  31.     </form>  
  32. </body>  
  33. </html> 

(10)重启apache服务器和三个tomcat服务器,到此负载 均衡已配置完成。测试负载均衡配置先测试apache,访问http://192.168.0.88/jkstatus.jsp
能否正常访问,并查询其中的内容,有三个tomcat的相关配置信息和负载说明,访问http://192.168.0.88/TomcatDemo /index.jsp看能够运行,
能运行,则已建立负载均衡。#p#

(四)tomcat集群配置
(1)负载均衡配置的条件下配置tomcat集群
(2)分别修改三个tomcat的配置文件conf/server.xml,修改内容如下
修改前        

  1. <!--  
  2.          <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster" 
  3.                   managerClassName="org.apache.catalina.cluster.session.DeltaManager" 
  4.                   expireSessionsOnShutdown="false" 
  5.                   useDirtyFlag="true" 
  6.                   notifyListenersOnReplication="true">  
  7.              <Membership  
  8.                  className="org.apache.catalina.cluster.mcast.McastService" 
  9.                  mcastAddr="228.0.0.4" 
  10.                  mcastPort="45564" 
  11.                  mcastFrequency="500" 
  12.                  mcastDropTime="3000"/>  
  13.              <Receiver  
  14.                  className="org.apache.catalina.cluster.tcp.ReplicationListener" 
  15.                  tcpListenAddress="auto" 
  16.                  tcpListenPort="4001" 
  17.                  tcpSelectorTimeout="100" 
  18.                  tcpThreadCount="6"/>  
  19.              <Sender  
  20.                  className="org.apache.catalina.cluster.tcp.ReplicationTransmitter" 
  21.                  replicationMode="pooled" 
  22.                  ackTimeout="5000"/>  
  23.              <Valve className="org.apache.catalina.cluster.tcp.ReplicationValve" 
  24.                     filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>                     
  25.              <Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer" 
  26.                        tempDir="/tmp/war-temp/" 
  27.                        deployDir="/tmp/war-deploy/" 
  28.                        watchDir="/tmp/war-listen/" 
  29.                        watchEnabled="false"/>                        
  30.              <ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/>  
  31.          </Cluster>  
  32.          -->  

修改后       

  1. <!-- modify by whh -->  
  2.          <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster" 
  3.                   managerClassName="org.apache.catalina.cluster.session.DeltaManager" 
  4.                   expireSessionsOnShutdown="false" 
  5.                   useDirtyFlag="true" 
  6.                   notifyListenersOnReplication="true">  
  7.              <Membership  
  8.                  className="org.apache.catalina.cluster.mcast.McastService" 
  9.                  mcastAddr="228.0.0.4" 
  10.                  mcastPort="45564" 
  11.                  mcastFrequency="500" 
  12.                  mcastDropTime="3000"/>  
  13.              <Receiver  
  14.                  className="org.apache.catalina.cluster.tcp.ReplicationListener" 
  15.                  tcpListenAddress="auto" 
  16.                  tcpListenPort="4001" 
  17.                  tcpSelectorTimeout="100" 
  18.                  tcpThreadCount="6"/>  
  19.              <Sender  
  20.                  className="org.apache.catalina.cluster.tcp.ReplicationTransmitter" 
  21.                  replicationMode="pooled" 
  22.                  ackTimeout="5000"/>  
  23.              <Valve className="org.apache.catalina.cluster.tcp.ReplicationValve" 
  24.                     filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>                     
  25.              <Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer" 
  26.                        tempDir="/tmp/war-temp/" 
  27.                        deployDir="/tmp/war-deploy/" 
  28.                        watchDir="/tmp/war-listen/" 
  29.                        watchEnabled="false"/>                        
  30.              <ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/>  
  31.          </Cluster>  
  32.         <!-- modify by whh --> 

将集群配置选项的注释放开即可,如上。
(3)在Conf目录下建立jk2.properties文件,写入下面一行,其中端口为相应的Tomcat对应的AJP端口。channelSocket.port=8009
(4)重启三个tomcat。到此tomcat的集群已配置完成。#p#

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

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

配置后

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

 

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

2010-04-20 22:19:29

tomcat负载均衡配

2010-04-22 16:41:56

负载均衡端口规则

2010-05-04 16:10:51

负载均衡算法

2013-04-22 11:29:14

Nginx

2010-05-05 18:58:34

STP负载均衡

2010-04-22 16:05:46

网络负载均衡配置

2019-07-09 15:10:02

Nginx反向代理负载均衡

2010-04-22 12:45:05

2010-04-21 12:28:50

Oracle负载均衡

2010-04-23 11:05:16

流量负载均衡

2012-05-07 10:17:48

2010-04-22 23:47:55

tomcat负载均衡设

2010-05-06 12:02:43

路由负载均衡

2009-01-11 10:30:00

负载均衡网络故障

2014-07-28 11:37:49

NginxTomcat

2010-04-20 17:34:21

Linux双网卡负载均衡

2010-03-30 13:59:56

Nginx负载均衡配置

2017-07-03 08:08:25

负载均衡分类

2010-04-22 16:24:42

配置网络负载均衡

2010-04-20 21:27:28

tomcat负载均衡配
点赞
收藏

51CTO技术栈公众号