Apache负载均衡之mod_proxy模块实现

 

实验环境:一台虚拟机,IP:192.168.1.117

实验目的:学习mod_proxy的负载功能,实现Apache反代并且负载到后端两个tomcat。

实验步骤:

/etc/httpd/conf.d目录下增加配置文件,实现Apache的负载均衡。

# cd /etc/httpd/conf.d

# vim mod_proxy.conf

      BalancerMember http://192.168.1.117:8080 loadfactor=1 route=TomcatA

      BalancerMember http://192.168.1.117:9080 loadfactor=1 route=TomcatB

      ProxySet lbmethod=byrequests

 

ProxyPass / balancer://lbcluser1/

ProxyPa***everse / balancer://lbcluser/

保存退出

在上面配置文件ProxyPass / balancer://lbcluser1/ 的中间中添加如下,这样可以访问其status页面。

     SetHander balancer-manager

     ProxyPass !

     Order allow,deny

     Allow from all

即是下面配置, # vim mod_proxy.conf

      BalancerMember http://192.168.1.117:8080 loadfactor=1 route=TomcatA

      BalancerMember http://192.168.1.117:9080 loadfactor=1 route=TomcatB

      ProxySet lbmethod=byrequests

 

     SetHander balancer-manager

     ProxyPass !

     Order allow,deny

     Allow from all

 

ProxyPass / balancer://lbcluster1/

ProxyPa***everse / balancer://lbcluster/

保存退出之后

# httpd -t

# service httpd restart

 

下面在tomcat下创建test测试页

现在两个tomcat上做测试页,即在/usr/local/tomcatA/webapps/目录下

 创建# mkdir -pv ./test/WEB-INF/{classes,lib},并且在test目录下穿件文件

# vim index.jsp

<%@ page language="java" %>

  TomcatA

 

   

TomcatA. .just a test page

   

     

       

    <% session.setAttribute("changyu.com","changyu.com"); %>

       

     

     

       

       

    

    

Session ID <%= session.getId() %>
Created on <%= session.getCreationTime() %>

 

同理在tomcatB同位置处

演示效果,在TomcatB上某context中(如/test),提供如下页面

<%@ page language="java" %>

  TomcatB

 

   

TomcatB.just a test page

   

     

       

    <% session.setAttribute("changyu.com","changyu.com"); %>

       

     

     

       

       

    

   

Session ID <%= session.getId() %>
Created on <%= session.getCreationTime() %>

 

    还有一步就是在/usr/local/tomcat{1,2}/webpapps/test/WEB-INF/的目录下加一个web.xml文件并且在其任意位置添加

对于tomcatA来说

[root@org conf]# cp ./web.xml /usr/local/tomcatA/webapps/test/WEB-INF/

[root@org conf]# cd /usr/local/tomcatA/webapps/test/WEB-INF/

[root@org WEB-INF]# ls

classes  lib  web.xml

[root@org WEB-INF]#

[root@org WEB-INF]# vim web.xml //任意位置加入下信息

    对于tomcatB来说

[root@org conf]# cp ./web.xml /usr/local/tomcatA/webapps/test/WEB-INF/

[root@org conf]# cd /usr/local/tomcatB/webapps/test/WEB-INF/

[root@org WEB-INF]# ls

classes  lib  web.xml

[root@org WEB-INF]# vim web.xml //任意位置加入下信息。

     分别重新启动。

[root@org bin]# ./catalina.sh stop

Using CATALINA_BASE:   /usr/local/tomcatB

Using CATALINA_HOME:   /usr/local/tomcatB

Using CATALINA_TMPDIR: /usr/local/tomcatB/temp

Using JRE_HOME:        /opt/jdk1.7.0_71/

Using CLASSPATH:       /usr/local/tomcatB/bin/bootstrap.jar:/usr/local/tomcatB/bin/tomcat-juli.jar

[root@org bin]# ./catalina.sh start

Using CATALINA_BASE:   /usr/local/tomcatB

Using CATALINA_HOME:   /usr/local/tomcatB

Using CATALINA_TMPDIR: /usr/local/tomcatB/temp

Using JRE_HOME:        /opt/jdk1.7.0_71/

Using CLASSPATH:       /usr/local/tomcatB/bin/bootstrap.jar:/usr/local/tomcatB/bin/tomcat-juli.jar

Tomcat started.

[root@org bin]#

    重新启动,并且访问http://192.168.1.117/test

刷新页面

           

    将上面test应用定义成集群会话管理器,将下面文件拷贝到两个文件中

                 channelSendOptions="8">

 

         

                   expireSessionsOnShutdown="false"

                   notifyListenersOnReplication="true"/>

 

         

           

                        address="228.0.0.4"

                        port="45564"

                        frequency="500"

                        dropTime="3000"/>

           

                      address="auto"

                      port="4000"

                      autoBind="100"

                      selectorTimeout="5000"

                      maxThreads="6"/>

           

             

           

           

           

         

         

                 filter=""/>

         

         

                    tempDir="/tmp/war-temp/"

                    deployDir="/tmp/war-deploy/"

                    watchDir="/tmp/war-listen/"

                    watchEnabled="false"/>

         

         

       

之后再次访问,会话保持

刷新一下,不难发现SessionID 没有变化。