Apache + Tomcat 集群负载均衡配置

 

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:-1610611985 1107304683 0 0 159 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-fareast-font-family:宋体; mso-bidi-font-family:"Times New Roman"; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {mso-style-noshow:yes; mso-style-priority:99; color:purple; mso-themecolor:followedhyperlink; text-decoration:underline; text-underline:single;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; font-size:10.0pt; mso-ansi-font-size:10.0pt; mso-bidi-font-size:10.0pt; mso-ascii-font-family:Calibri; mso-fareast-font-family:宋体; mso-hansi-font-family:Calibri; mso-font-kerning:0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:187522986; mso-list-type:hybrid; mso-list-template-ids:581338428 125752456 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-text:%1、; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt;} @list l1 {mso-list-id:421144227; mso-list-type:hybrid; mso-list-template-ids:161758200 -206168318 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l1:level1 {mso-level-number-format:alpha-lower; mso-level-text:%1、; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt; font-family:"Courier New";} @list l2 {mso-list-id:912276613; mso-list-type:hybrid; mso-list-template-ids:1736984486 986057364 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l2:level1 {mso-level-number-format:alpha-lower; mso-level-text:%1、; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:39.1pt; text-indent:-18.0pt; font-family:"Courier New"; color:windowtext; mso-ansi-font-weight:bold;} @list l3 {mso-list-id:1355182404; mso-list-type:hybrid; mso-list-template-ids:524983372 -1156428066 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l3:level1 {mso-level-number-format:alpha-lower; mso-level-text:%1、; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:39.0pt; text-indent:-18.0pt; mso-ansi-font-weight:bold;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

1、 环境

Apache 2.2.15

Tomcat 6.0.20

Jdk 1.6

2、 软件

代理模块 mod_jk-1.2.28-httpd-2.2.X.so 下载地址:

http://labs.renren.com/apache-mirror/tomcat/tomcat-connectors/jk/binaries/linux/jk-1.2.28/x86_64/mod_jk-1.2.28-httpd-2.2.X.so

将下载的 mod_jk-1.2.28-httpd-2.2.X.so 文件复制到 %apache_home% 安装目录 modules 目录下

 

3、 测试 tomcat

http://IP:8080/     tomcat1 所在服务器)

http://IP:8080/     tomcat2 所在服务器)

如下图表示正常

 

4、 配置 apache 服务器

a、 %apache_home% 目录下的 conf/httpd.conf 文件末尾中添加:

include conf/mod_jk.conf

b、 conf 目录中新建 mod_jk.conf 文件,内容如下:

# 加载 mod_jk Module

LoadModule jk_module modules/mod_jk-1.2.28-httpd-2.2.X.so

 

JkWorkersFile conf/workers.properties

指定 workers.properties 文件路径

JkLogFile logs/mod_jk.log

JkLogLevel info

 

JkMount /* controller

指定那些请求交给 tomcat 处理 ,"controller" 为在 workers.propertise 里指定的负载均衡器

c、 conf 目录下新建 workers.properties 文件,内容如下:

worker.list=controller,tomcat1,tomcat2

负载均衡器列表

 

worker.controller.type=lb

负载均衡器类型

worker.controller.balance_workers=tomcat1,tomcat2

指定分担请求的 tomcat 实例名

worker.controller.sticky_session=true

是否使用粘性会话,表示从同一窗口发来的请求都将有集群中的同一个 tomcat 进行处理

 

#========tomcat1========

worker.tomcat1.host=10.10.14.89

tomcat 的主机地址,如不为本机,请填写 ip 地址

worker.tomcat1.port=10009

ajp13 端口号,在 tomcat server.xml 配置 , 默认 10009

worker.tomcat1.type=ajp13

实例节点类型

worker.tomcat1.lbfactor=1

worker 的加权比重,值越高,分得的请求越多

 

#========tomcat2========

worker.tomcat2.host=10.10.14.137

worker.tomcat2.port=20009

worker.tomcat2.type=ajp13

worker.tomcat2.lbfactor=1

 

 

5、 修改 tomcat 配置文件

% tomcat_home%/conf/server.xml 的如下内容

a、 Engine 配置

修改前:

<Engine   name = "Catalina"   defaultHost = "localhost" >

修改后:

<Engine   name = "Catalina"   defaultHost = "localhost"   jvmRoute = "tomcat1" >

<Engine   name = "Catalina"   defaultHost = "localhost"   jvmRoute = "tomcat2" >

具体 tomcat jvmRoute 的值根据在 workers.properties 的配置,注意 IP 地址、 tomcat 实例名称和 jvmRoute 值必须一一对应。

b、 Connector 配置

修改前:

<Connector   port = "8009"   protocol = "AJP/1.3"   redirectPort = "8443"   />  

修改后:

<Connector   port = "9009"   protocol = "AJP/1.3"   redirectPort = "8443"   />

这里 tomcat port 值必须与在 workers.properties 中的 port 值一致。

c、 Cluster 配置

<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.0.0.4" port = "45564"   frequency = "500"   dropTime = "3000" />

          <Receiver   className = "org.apache.catalina.tribes.transport.nio.NioReceiver"   address = "auto" port = "4001" autoBind = "100" selectorTimeout = "5000" maxThreads = "6" />

          <!-- timeout="60000"-->

          <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" />

          <Interceptor   className = "org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor" />

        </Channel>

        <Valve   className = "org.apache.catalina.ha.tcp.ReplicationValve"

               filter = ".*/.gif;.*/.js;.*/.jpg;.*/.png;.*/.htm;.*/.html;.*/.css;.*/.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>

 

6、 配置应用程序 web.xml

<web-appxmlns="http://java.sun.com/xml/ns/j2ee "xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "version="2.4">

      <display-name>TestDemo</display-name>

      <distributable />

</web-app>

此配置说明当前 Web 工程处于分布式部署环境。如果不进行这个配置,每次访问页面 tomcat 都会产生一个新的 sessionid

 

7、 可能出现的问题

a、 Java 未先安装,或未正确配置 JAVA_HOME 体统环境变量;

b、 Linux 系统的防火墙 指定的 Tomcat 端口未被设置为远程可访问;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值