您的位置:首页 > 运维架构 > Tomcat

单节点apache+tomcat负载均衡+集群整合

2014-04-14 17:00 190 查看
部署环境:
windows server 2003
windows server 2008

前言:

1.apache
Apache是普通服务器,本身只支持html即普通网页。不过可以通过插件支持php,还可以与Tomcat连通。Apache和Tomcat整合使用:如果客户端请求的是静态页面,则只需要Apache服务器响应请求;如果客户端请求动态页面,则是Tomcat服务器响应请求;因为jsp是服务器端解释代码的,这样整合就可以减少Tomcat的服务开销 。

区别:Apache是web服务器,Tomcat是应用(java)服务器,它只是一个servlet容器,是Apache的扩展。 Apache和Tomcat都可以做为独立的web服务器来运行,但是Apache不能解释java程序(jsp,serverlet)。2.mod_jk
tomcat为一个jsp的容器,apache为一个web server,他们之间的通信可以通过worker进行(由Tomcat使用Server.xml文件中Connector的标签来定义其端口和协议),即通过通过mod_jk的模块进行通信,即:apache的httpd.conf文件先调用mod_jk.conf,mod_jk.conf调用workers.properties,最后配置虚拟主机。

java用的是1.6.0_30的版本,具体配置方法不再介绍

1.安装apche,tomcat我的安装路径:C:\ProgramFiles\Apache Software Foundation\Apache2.2C:\tomcat\tomcat_1C:\tomcat\tomcat_2
2.安装mod_jk.so把mod_jk-1.2.31-httpd-2.2.3.so复制到C:\Program Files\Apache Software\Foundation\Apache2.2\modules目录下。
3.修改Apache配置文件C:\ProgramFiles\Apache Software\Foundation\Apache2.2\conf\httpd.conf在文件最后加上下面一句话就可以了include"C:\Program Files\Apache SoftwareFoundation\Apache2.2\conf\mod_jk.conf"4.httpd.conf 同目录下新建mod_jk.conf文件,内容如下:#加载mod_jk ModuleLoadModule jk_modulemodules/mod_jk-1.2.31-httpd-2.2.3.so#指定 workers.properties文件路径JkWorkersFileconf/workers.properties#加载workers的请求处理分配文件JkMountFileconf/uriworkermap.properties
#指定jk的日志输出文件JkLogFilelogs/mod_jk.logJkMount /*.jspcontroller#指定日志级别JkLogLevel warn5.http.conf同目录下新建 workers.properties文件,内容如下 #server 列表worker.list=tomcat1,tomcat2 #========tomcat1========worker.tomcat1.port=8009worker.tomcat1.host=localhost (可改成ip)worker.tomcat1.type=ajp13worker.tomcat1.lbfactor=1#========tomcat2========worker.tomcat2.port=9009worker.tomcat2.host=localhost (可改成ip)worker.tomcat2.type=ajp13worker.tomcat2.lbfactor=1#========controller,负载均衡控制器========worker.controller.type=lbworker.retries=3worker.controller.balanced_workers=tomcat1,tomcat2#worker.controller.sticky_session=falseworker.controller.sticky_session=1worker.list=controller,jkstatus#worker.controller.sticky_session_force=1worker.jkstatus.type=status
6.http.conf同目录下新建 uriworkermap.properties文件,内容如下#所有请求都由controller这个server处理/*=controller#所有包含jkstatus请求的都由status这个server处理/jkstatus=jkstatus#所有以.gif结尾的请求都不由controller这个server处理,以下几个都是一样的意思/*.*=controller 或 /*=controller#!/*.gif=controller#!/*.jpg=controller#!/*.png=controller#!/*.bmp=controller#!/*.css=controller#!/*.js=controller#!/*.htm=controller#!/*.html=controller#!/*.swf=controller7.修改tomcat配置文件server.xml如果你在不同电脑上安装tomcat,tomcat的安装数量为一个,可以不必修改tomcat配置文件我这里是在同一台电脑上安装两个tomcat,所以需要更改tomcat2的设置需要修改的端口配置:(1)<Server port="8005" shutdown="SHUTDOWN">改为<Server port="9005" shutdown="SHUTDOWN">(2)<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>改为 <Connector port="9080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>(3)<Connector port="8009" protocol="AJP/1.3"redirectPort="8443" />改为<Connectorport="9009" protocol="AJP/1.3"redirectPort="8443" />(4)“<Receiver”下的port改为50028.编写测试jsp(1)在tomcat1的webapps目录下建立一个目录test,里面新建一个test.jsp,内容为<% System.out.println("==========================="); System.out.println(session.getAttribute("test")); session.setAttribute("test","Session");%><html> <body bgcolor="red"> <center> <h1>Tomcat 1</h1> </body></html>(2)在tomcat2的webapps建立一个目录test.里面新建一个test.jsp,内容为<% System.out.println("==========================="); System.out.println(session.getAttribute("test")); session.setAttribute("test","Session");%><html> <body bgcolor="blue"> <center> <h1>Tomcat 2</h1> </body></html>
9.启动apache,tomcat1,tomcat2,进行测试通过 http://localhost/test/test.jsp 访问,查看tomcat1的窗口,可以看到打印了一行"=========="再刷新一次,tomcat2也打印了一条,再刷新,可以看到请求会被tomcat1,tomcat2轮流处理,实现了负载均衡至此,负载均衡设定完成。只配置负载均衡还不行,还要session复制,也就是说其中任何一个tomcat的添加的session,是要同步复制到其它tomcat,集群内的tomcat都有相同的session。

第二部分,配置集群

1.修改tomcat1, tomcat2server.xml
大概在102行,有<Engine name="Catalina"defaultHost="localhost"> 修改一下,加上jvmRoute="tomcat1",tomcat1 改完后:<Enginename="Catalina" defaultHost="localhost" jvmRoute="tomcat1">tomcat2 改完后:<Enginename="Catalina" defaultHost="localhost" jvmRoute="tomcat2">(2)大概在109行左右,<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>是被注释掉的。在这段注释后面,加上下面的内容:
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="6">

<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<!--
<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" (如果不行,设置为本地ip)
port="5000" (tomcat2的改成5002或其他)
selectorTimeout="100"
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"/>
<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;"/>
<!--
<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.ClusterSessionListener"/>
</Cluster>

然后:在每个tomcat的conf目录下的web.xml下添加如下红色的字符
最后几行:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<distributable/>
</web-app>

2.修改测试项目test(两个test.jsp都改成一样就可以)

<%@ page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ pagecontentType="text/html; charset=UTF-8"%><%@ pageimport="java.util.*"%><!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><title>ClusterApp Test</title></head><body >Server Info:<%out.println(request.getLocalAddr()+ " : " + request.getLocalPort() + "<br>");%><%out.println("<br>ID " + session.getId() + "<br>");// 如果有新的 Session 属性设置String dataName =request.getParameter("dataName");if (dataName != null&& dataName.length() > 0) {String dataValue =request.getParameter("dataValue");session.setAttribute(dataName,dataValue);}out.print("<b>Session列表</b>");Enumeration e =session.getAttributeNames();while(e.hasMoreElements()) {String name = (String)e.nextElement();String value =session.getAttribute(name).toString();out.println(name +" = " + value + "<br>");System.out.println(name+ " = " + value);}%><formaction="test.jsp" method="POST">名称:<input type=textsize=20name="dataName"> <br>值:<input type=text size=20 name="dataValue"> <br><inputtype=submit></form></body></html>

3.然后在test 新建WEB-INF目录,WEB-INF下新建web.xml,内容如下<?xmlversion="1.0" encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>test</display-name><distributable/></web-app>
4.test复制到tomcat1,tomcat2webapps下,重启apache,tomcat1,tomcat2输入网址 http://localhost/test/test.jsp 就可以测试了。测试结果的sessionid在多次刷新的情况下不变。

问题解决:

有时候访问单节点会出现 service temporarily unavailable 问题,目前不知道确定原因是啥,

删了tomcat下的work,然后根据apache logs目录下的error.log、mod_jk.log
查看相关的错误信息解决的
主要是tomcat 和apapche启动先后的顺序问题,首先要启动tomcat,其次启动apache最好

由于设置集群的时候在sessionid 这块停留的时间较长,最后从官网找的文档猜解决,因此附上从官网找的配置文件:

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="6">

<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<!--
<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="本地ip"
port="5000"
selectorTimeout="100"
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"/>
<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;"/>
<!--
<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.ClusterSessionListener"/>
</Cluster>

到此,windows下apache+tomcat负载均衡集群整合配置完成。
本文出自 “残月” 博客,请务必保留此出处http://waning411.blog.51cto.com/4909368/1395407
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: