环境信息
操作系统:win8
Tomcat版本:Tomcat 8.0.3
Apache版本:httpd-2.2.25-win32-x86-openssl-0.9.8y
1. 安装Apache
1.1 下载Windows版Apache
Apache下载地址:http://httpd.apache.org/download.cgi
在下载页面选择”Binaries”(哪个版本下的”Binaries”均可)
然后选择”win32/”
下载”httpd-2.2.25-win32-x86-openssl-0.9.8y”(Apache有两个版本”no_ssl”和”openssl”,即不支持ssl和支持ssl。选择”openssl”版是由于以后学习ssl会用到”openssl”)
1.2 安装Apache
双击” httpd-2.2.25-win32-x86-openssl-0.9.8y.msi”
Server信息随便填
选择Custom,可以自定义Apache的安装路径
安装完成
此时,在Windows右下角可以看到Apache图标
右键此图标,选择”Open Apache Monitor”,可以看到Apache Service Monitor界面
在浏览器下输入http://localhost,看到”It works”,表明Apache安装成功
1.3 修改Apache的启动类型
Apache默认是自动启动的,可以到”服务”下修改其启动类型
右键计算机-管理-服务和应用程序-服务,找到Apache2.2。右键Apache2.2,选择属性,修改启动类型即可
1.4 卸载Apache
打开”控制面板”,选择”卸载程序”,右键”Apache HTTP Server 2.2.25”,选择”卸载”
2. 部署Tomcat集群
2.1 创建Tomcat-Cluster目录
在D盘下新建Tomcat-Cluster文件夹,在Tomcat-Cluster下新建两个文件夹Tomcat1,Tomcat2
2.2 配置Tomcat1
2.2.1将Tomcat(Tomcat 8.0.3文件夹)下的文件拷贝到Tomcat1
2.2.2 创建测试项目TestCluster
在D:\ Tomcat-Cluster\Tomcat1\webapps下,创建TestCluster文件夹,然后将ROOT文件夹下的WEB-INF文件夹拷贝到TestCluster下
打开TestCluster\WEB-INF\web.xml
在</web-app>上面添加<distributable/>
(设置<distributable/>,即表明集群下某一节点生成或改变的Session,将广播到该集群的其它节点)
在TestCluster文件夹下新建index.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ page import="java.text.SimpleDateFormat"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>TestCluster</title>
- </head>
- <body>
- Server Info:
- <%
- String dtm = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
- System.out.println("["+request.getLocalAddr()+":"+ request.getLocalPort()+"]" + dtm);
- out.println("<br>["+request.getLocalAddr()+":" +request.getLocalPort()+"]" + dtm+"<br>");
- %>
- Session Info:
- <%
- session.setAttribute("name","dennisit");
- System.out.println("[Session Info] Session ID:"+session.getId());
- out.println("<br>[Session Info] Session ID:" + session.getId()+"<br>");
- %>
- </body>
- </html>
2.2.3 修改Tomcat1的Server.xml(Tomcat1\conf\Server.xml)
a. 将Server标签下的8005端口改为7005,Connector标签下8080,8009端口改为7080,7009(为了避免端口冲突)
b.在Engine标签内添加jvmRoute属性,将其设置为” Tomcat1”
c.将Cluster标签的注释打开
修改后的server.xml(为了看起来简洁,删掉了Tomcat自带的注释)
- <?xml version='1.0' encoding='utf-8'?>
- <!--将端口号由8005改为7005 -->
- <Server port="7005" shutdown="SHUTDOWN">
- <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
- <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
- <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
- <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
- <GlobalNamingResources>
- <Resource name="UserDatabase" auth="Container"
- type="org.apache.catalina.UserDatabase"
- description="User database that can be updated and saved"
- factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
- pathname="conf/tomcat-users.xml" />
- </GlobalNamingResources>
- <Service name="Catalina">
- <!--将端口号由8080改为7080 -->
- <Connector port="7080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" />
- <!--将端口号由8009改为7009 -->
- <Connector port="7009" protocol="AJP/1.3" redirectPort="8443" />
- <!--在Engine标签添加jvmRoute属性-->
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="Tomcat1">
- <!--将Cluster标签的注释去掉-->
- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
- <Realm className="org.apache.catalina.realm.LockOutRealm">
- <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
- resourceName="UserDatabase"/>
- </Realm>
- <Host name="localhost" appBase="webapps"
- unpackWARs="true" autoDeploy="true">
- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
- prefix="localhost_access_log" suffix=".txt"
- pattern="%h %l %u %t "%r" %s %b" />
- </Host>
- </Engine>
- </Service>
- </Server>
2.3 配置Tomcat2
将Tomcat1下的文件拷贝到Tomcat2
修改Tomcat2的Server.xml
a.将端口7005,7080,7009改为9005,9080,9009
b.Engine标签内的jvmRoute改为” Tomcat2”
2.4 启动Tomcat集群
(如果之前配置过Tomcat的环境变量,要先将Tomcat的环境变量删除)
双击Tomcat1\bin\startup.bat、Tomcat2\bin\startup.bat
在浏览器下输入:http://localhost:7080/TestCluster/index.jsp,即可访问集群下Tomcat1节点的TestCluster
在浏览器下输入:http://localhost:9080/TestCluster/index.jsp,即可访问集群下Tomcat2节点的TestCluster
可以看到Tomcat1、Tomcat2两个节点的Session一样
3. 整合Apache与Tomcat集群
整合Apache与Tomcat集群可以使用mod_proxy_balancer也可以使用mod_jk
3.1 使用mod_proxy_balancer
a. 在D:\Program Files\Apache 2.2\conf下新建mod_proxy_balancer.conf
- LoadModule proxy_module modules/mod_proxy.so
- LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
- LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
- LoadModule proxy_connect_module modules/mod_proxy_connect.so
- LoadModule proxy_http_module modules/mod_proxy_http.so
- <Proxy balancer://TestCluster>
- BalancerMember ajp://127.0.0.1:7009 loadfactor=1
- BalancerMember ajp://127.0.0.1:9009 loadfactor=1
- #BalancerMember http://127.0.0.1:7080 loadfactor=1
- #BalancerMember http://127.0.0.1:9080 loadfactor=1
- </Proxy>
- ProxyPass / balancer://TestCluster/
LoadModule-加载模块
Proxy-负责定义代理,该代理下可以包含多个处理请求的节点
BalancerMember-处理请求的节点(设置ajp、http均可)
loadfactor-权重。某一节点的权重越大,分配到该节点的请求越多
ProxyPass -Apache向Proxy转发请求的条件(ProxyPass /*.jsp balancer://TestCluster/,表明Apache将http://localhost/下的所有jsp请求转发给balancer://TestCluster/处理)
b. 修改httpd.conf
打开httpd.conf(Apache 2.2\conf\httpd.conf),在尾部添加一行代码
- include conf/mod_proxy_balancer.conf
c. 测试
重启Apache,然后在浏览器下输入http://localhost/TestCluster/index.jsp,可看到如下页面
刷新之后,看到如下页面
两次请求,分别转发到了Tomcat1节点、Tomcat2节点,并且两个节点的Session一样
3.2 使用mod_jk
a. 下载Windows版mod_jk.so
Windows版mod_jk.so下载地址:http://tomcat.apache.org/download-connectors.cgi
点击页面的” Binary Releases”,然后点击”windows/”
下载”tomcat-connectors-1.2.39-windows-i386-httpd-2.2.x.zip”
解压该文件,可得到”mod_jk.so”。将”mod_jk.so”拷到Apache 2.2\modules目录下
b. 在Apache 2.2\conf下新建workers.properties
- worker.list=controller,Tomcat1,Tomcat2
- #========Tomcat1========
- worker.Tomcat1.port=7009
- worker.Tomcat1.host=localhost
- worker.Tomcat1.type=ajp13
- worker.Tomcat1.lbfactor=1
- #========Tomcat2========
- worker.Tomcat2.port=9009
- worker.Tomcat2.host=localhost
- worker.Tomcat2.type=ajp13
- worker.Tomcat2.lbfactor=1
- #========controller========
- worker.controller.type=lb
- worker.controller.balanced_workers=Tomcat1,Tomcat2
- worker.controller.sticky_session=false
(如果worker.controller.sticky_session设为true,则同一用户的同一session始终和一个Tomcat节点交互)
c. 在Apache 2.2\conf下新建mod_jk.conf
- LoadModule jk_module modules/mod_jk.so
- JkWorkersFile conf/workers.properties
- JkMount /* controller
JkWorkersFile-workers.properties的位置
JkMount-Apache将http://localhost/下的所有请求转发给controller处理
d. 修改httpd.conf
打开httpd.conf(Apache 2.2\conf\httpd.conf),将尾部的include conf/mod_proxy_balancer.conf注释掉,并添加一行代码
- include conf/mod_jk.conf
e. 测试
重启Apache,然后在浏览器下输入http://localhost/TestCluster/index.jsp,可看到如下页面
刷新之后,看到如下页面
两次请求,分别转发到了Tomcat1节点、Tomcat2节点,并且两个节点的Session一样
Apache和Tomcat集群整合后,Apache是接收请求的入口,它根据设置的转发请求条件,决定将哪些请求转发个Tomcat处理
通常的应用场景为:Apache作为Web Server,它只负责处理静态页面,而动态页面会交给Tomcat(App Server)处理
参考文章:
http://blog.csdn.net/lifetragedy/article/details/7712691
http://www.cnblogs.com/leader_89/archive/2011/08/01/2109181.html