1)在Apache上编译安装Apache,安装编译步骤参考上篇第二步;
2)Tomcat-1和Tomcat-2上安装Tomcat,安装步骤参考上篇第一步;
3)Tomcat-1和Tomcat-2上配置;
- # vim /usr/local/tomcat/conf/server.xml //定义tomcat-1的worker名称
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA">
- # vim /usr/local/tomcat/conf/server.xml //定义tomcat-2的worker名称
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB">
4)Apache配置
- # vim /etc/httpd/extra/httpd-jk.conf
- 添加如下内容
- LoadModule jk_module modules/mod_jk.so
- JkWorkersFile /etc/httpd/extra/workers.properties
- JkLogFile logs/mod_jk.log
- JkLogLevel debug
- JkMount /* lbcluster1
- JkMount /status stat1
- # vim /etc/httpd/extra/workers.properties
- 添加如下行
- worker.list=lbcluster1,stat1
- worker.TomcatA.port=8009
- worker.TomcatA.host=172.16.11.11
- worker.TomcatA.type=ajp13
- worker.TomcatA.lbfactor=1
- worker.TomcatB.port=8009
- worker.TomcatB.host=172.16.11.12
- worker.TomcatB.type=ajp13
- worker.TomcatB.lbfactor=1
- worker.lbcluster1.type=lb //lb即load balancing,专用于负载均衡场景中的woker;此worker并不真正负责处理用户请求,而是将用户请求调度给其它类型为ajp13的worker//
- worker.lbcluster1.sticky_session=0 //在将某请求调度至某worker后,源于此址 的所有后续请求都将直接调度至此worker,实现将用户session与某worker绑定。 默认值为1,即启用此功能,如果后端的各worker之间支持session复制,则可以 将此属性值设为0//
- worker.lbcluster1.balance_workers=TomcatA,TomcatB //用于负载均衡模式中的各worker的名称列表,需要注意的是,出现在此处的worker名称一定不能在任何worker.list属性列表中定义过,并且worker.list属性中定义的worker名字必须包含负载均衡worker//
- worker.stat1.type=status
- # service httpd restart //重启httpd服务
5)访问验证worker工作状态:
# /usr/local/tomcat/bin/catalina.sh stop //停掉TomcatB服务访问验证对worker健康状况检查
- TomcatA实例配置:
- # cd /usr/local/tomcat/webapps/
- # mkdir -pv sess/WEB-INF/{classes,lib}
- # cd sess/
- # vim index.jsp
- 提供如下内容
- <%@ page language="java" %>
- <html>
- <head><title>TomcatA</title></head>
- <body>
- <h1><font color="red">TomcatA </font></h1>
- <table align="centre" border="1">
- <tr>
- <td>Session ID</td>
- <td><%= session.getId() %></td>
- </tr>
- <tr>
- <td>Created on</td>
- <td><%= session.getCreationTime() %></td>
- </tr>
- </table>
- </body>
- </html>
- TomcatB实例上配置
- # cd /usr/local/tomcat/webapps/
- # mkdir -pv sess/WEB-INF/{classes,lib}
- # cd sess/
- # vim index.jsp
- 提供如下内容
- <%@ page language="java" %>
- <html>
- <head><title>TomcatB</title></head>
- <body>
- <h1><font color="blue">TomcatB </font></h1>
- <table align="centre" border="1">
- <tr>
- <td>Session ID</td>
- <td><%= session.getId() %></td>
- </tr>
- <tr>
- <td>Created on</td>
- <td><%= session.getCreationTime() %></td>
- </tr>
- </table>
- </body>
- </html>
通过上述方式访问TomcatA和TomcatB时发现二者会话一直在发生变化,如果用户在淘宝上买东西的话每次刷新一次原有页面就没有了,此时用户添加的商品岂不是也没有了,这一定会给用户带来很大烦恼,这就需要配置会话保持功能;
9)配置Apache基于mod_jk模块实现会话保持功能
- # vim /etc/httpd/extra/workers.properties
- 添加如下选项
- worker.lbcluster1.sticky_session=1 //在将某请求调度至某worker后,源于此址的所有后续请求都将直接调度至此worker,实现将用户session与某worker绑定。默认值为1,即启用此功能,如果后端的各worker之间支持session复制,则可以将此属性值设为0//
- # service httpd restart //重启httpd服务
- # vim /etc/httpd/httpd.conf
- Include /etc/httpd/extra/httpd-vhosts.conf //打开虚拟主机选项
- #Include /etc/httpd/extra/httpd-jk.conf //禁止apache使用httpd-jk.conf配置文件
- #DocumentRoot "/usr/local/apache/htdocs" //禁用中心主机
- # vim /etc/httpd/extra/httpd-vhosts.conf //编辑虚拟主机文件关闭虚拟主机样例并添加新的虚拟主机
- #<VirtualHost *:80>
- # ServerAdmin [email protected]
- .................................................................
- .................................................................
- # CustomLog "logs/dummy-host2.example.com-access_log" common
- #</VirtualHost>
- 在全局配置段添加如下两项内容:
- ProxyRequests Off //关闭正向代理//
- <proxy balancer://lbcluster1> //定义负载均衡器//
- BalancerMember ajp://172.16.11.11:8009 loadfactor=10 route=TomcatA
- BalancerMember ajp://172.16.11.12:8009 loadfactor=10 route=TomcatB
- </proxy>
- ServerName tomcat.wjw.com
- ProxyPass / balancer://lbcluster1/ stickysession=jsessionid // stickysession有两种取值,分别为jsessionid(代理java)和phpsessionid(代理php)//
- ProxyPassReverse / balancer://lbcluster1/ //用于让apache调整HTTP重定向响应报文中的Location、Content-Location及URI标签所对应的URL,在反向代理环境中必须使用此指令避免重定向报文绕过proxy服务器//
- # service httpd restart //重启httpd服务
开启TomcatA,再次刷新访问
- # vim /etc/httpd/httpd.conf
- #Include /etc/httpd/extra/httpd-vhosts.conf //关闭虚拟主机选项
- Include /etc/httpd/extra/httpd-proxy.conf //添加该选项
- 在/etc/httpd/extra/目录下添加httpd-proxy.conf文件,内容如下:
- ProxyRequests Off
- <proxy balancer://lbcluster1>
- BalancerMember ajp://172.16.11.11:8009 loadfactor=10 route=TomcatA
- BalancerMember ajp://172.16.11.12:8009 loadfactor=10 route=TomcatB
- ProxySet lbmethod=bytraffic // libmethod:apache实现负载均衡的调度方法,默认是byrequests,即基于权重将统计请求个数进行调度,bytraffic则执行基于权重的流量计数调度,bybusyness通过考量每个后端服务器的当前负载进行调度//
- </proxy>
- ProxyPass /lbmanager ! //定义基于mod_proxy实现节点工作状态的管理,并不做代理//
- ProxyPass /status ! //定义基于mod_proxy实现节点工作状态的查看,并不做代理//
- ProxyPass / balancer://lbcluster1/
- ProxyPassReverse / balancer://lbcluster1/
- ProxyStatus On //打开proxy的状态信息查看功能,也可以改为full,表示显示完全信息//
- <Location /lbmanager> //定义本地访问路径//
- SetHandler balancer-manager //使用balancer-manager处理器进行处理//
- Require all granted //定义访问的权限,可以自己根据需要定义//
- </Location>
- <Location /status>
- SetHandler server-status
- Require all granted
- </Location>
1)在TomcatA和TomcatB的配置文件中的Engine部分分别添加如下内容:
- # vim /usr/local/tomcat/conf/server.xml
- TomcatA的Engine中添加如下内容:
- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
- channelSendOptions="8">
- <Manager className="org.apache.catalina.ha.session.DeltaManager"
- expireSessionsOnShutdown="false"
- notifyListenersOnReplication="true"/>
- <Channel className="org.apache.catalina.tribes.group.GroupChannel">
- <Membership className="org.apache.catalina.tribes.membership.McastService"
- address="228.50.10.1" bind="172.16.11.11" port="45564"
- frequency="500" dropTime="3000"/>
- <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
- address="172.16.11.11" port="4000" autoBind="100"
- selectorTimeout="5000" maxThreads="6"/>
- <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
- <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
- </Sender>
- <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
- <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
- </Channel>
- <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
- filter=".*\.gif;.*\.js;.*\.jpg;.*\.htm;.*\.html;.*\.txt;"/>
- <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
- <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
- tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/"
- watchDir="/tmp/war-listen/" watchEnabled="false"/>
- <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
- <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
- </Cluster>
- TomcatB配置文件中的Engine中添加如下内容
- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
- channelSendOptions="8">
- <Manager className="org.apache.catalina.ha.session.DeltaManager"
- expireSessionsOnShutdown="false"
- notifyListenersOnReplication="true"/>
- <Channel className="org.apache.catalina.tribes.group.GroupChannel">
- <Membership className="org.apache.catalina.tribes.membership.McastService"
- address="228.50.10.1" bind="172.16.11.12" port="45564"
- frequency="500" dropTime="3000"/>
- <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
- address="172.16.11.12" port="4000" autoBind="100"
- selectorTimeout="5000" maxThreads="6"/>
- <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
- <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
- </Sender>
- <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
- <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
- </Channel>
- <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
- filter=".*\.gif;.*\.js;.*\.jpg;.*\.htm;.*\.html;.*\.txt;"/>
- <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
- <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
- tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/"
- watchDir="/tmp/war-listen/" watchEnabled="false"/>
- <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
- <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
- </Cluster>
2)配置TomcatA和TomcatB的所有应用程序为分布式
- # cp /usr/local/tomcat/conf/web.xml /usr/local/tomcat/webapps/sess/WEB-INF/
- # vim /usr/local/tomcat/webapps/sess/WEB-INF/web.xml
- 添加如下内容
- <distributable/>
4)查看各节点工作状态的管理界面
6)关闭TomcatB验证Apache基于mod_proxy模块对各节点的健康状况检测效果
- #vim /etc/httpd/httpd.conf
- Include /etc/httpd/extra/httpd-jk.conf //开启httpd-jk
- #Include /etc/httpd/extra/httpd-proxy.conf //禁用httpd-proxy
- # vim /etc/httpd/extra/workers.properties
- worker.lbcluster1.sticky_session=0 //设置为0