Apache + Tomcat 负载均衡单机部署实例
一、准备工作
Tomcat6 : http://tomcat.apache.org/download-60.cgi
下载:apache-tomcat-6.0.36.exe
apache httpd server 2.2: http://www.fayea.com/apache-mirror//httpd/binaries/win32/
下载:httpd-2.2.22-win32-x86-no_ssl.msi
apache tomcat connector: http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.31/
下载:mod_jk-1.2.31-httpd-2.2.3.so
二、安装配置
安装路径:
E:\Apache2.2
E:\apache-tomcat-6.0.36-1
E:\apache-tomcat-6.0.36-2
项目路径:
E:\work\demo
1、Apache 配置
修改 E:\Apache2.2\conf\httpd.conf 文件。
1)、加载外部配置文件:
文件最后一行加上
include conf/mod_jk.conf
2)配置项目路径:
<IfModule alias_module> Alias /demo "E:/work/demo" ScriptAlias /cgi-bin/ "E:/Apache2.2/cgi-bin/" </IfModule>
3)配置目录权限:
<Directory "E:/work/demo"> Order Deny,Allow Allow from all </Directory>
4)配置默认首页:
增加 index.jsp
<IfModule dir_module> DirectoryIndex index.jsp index.html </IfModule>
5)增加 E:\Apache2.2\conf\mod_jk.conf 文件内容:
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so JkWorkersFile conf/workers.properties #指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名 JkMount /*.jsp controller
同时将 mod_jk-1.2.31-httpd-2.2.3.so 文件放入 E:\Apache2.2\modules 文件夹下。
6)增加 E:\Apache2.2\conf\workers.properties 文件内容:
#server worker.list = controller #========tomcat1======== worker.tomcat1.port=10009 worker.tomcat1.host=localhost worker.tomcat1.type=ajp13 worker.tomcat1.lbfactor = 1 #========tomcat2======== worker.tomcat2.port=11009 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_force=1 #worker.controller.sticky_session=1
2、Tomcat 配置
1)Tomcat-1 配置
E:\apache-tomcat-6.0.36-1\conf\server.xml
需要修改端口的地方:
<Server port="10005" shutdown="SHUTDOWN">
<Connector port="10080" URIEncoding="GBK" protocol="HTTP/1.1" connectionTimeout="20000" keepAliveTimeout="15000" maxKeepAliveRequests="1" redirectPort="8443" />
<Connector port="10009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"> <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
1)Tomcat-2 配置
E:\apache-tomcat-6.0.36-2\conf\server.xml
需要修改端口的地方:
<Server port="11005" shutdown="SHUTDOWN">
<Connector port="11080" URIEncoding="GBK" protocol="HTTP/1.1" connectionTimeout="20000" keepAliveTimeout="15000" maxKeepAliveRequests="1" redirectPort="8443" />
<Connector port="11009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2"> <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
三、项目测试
项目 web.xml 文件需在 <web-app>下增加<distributable/>。
test.jsp
<%@ page contentType="text/html; charset=GBK"%> <%@ page import="java.util.*"%> <html> <head> <title>Cluster App 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.println("<b>Session 列表</b><br>"); System.out.println("============================"); 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); } %> <form action="test.jsp" method="POST"> 名称: <input type=text size=20 name="dataName"> <br> 值: <input type=text size=20 name="dataValue"> <br> <input type=submit> </form> </body> </html>
先启动Apache2服务,之后依次启动两个tomcat。
分别访问:
http://127.0.0.1:10080/test.jsp
http://127.0.0.1:11080/test.jsp
http://127.0.0.1/test.jsp
接下来测试你懂得,三者 seesion 内容一致即配置成功。
四、注意事项
1、若测试结果不成功,可以查看日志看看报什么错误,是否配置疏忽了什么环节,apache的权限有没有配置等,注意版本;
2、放在session里的对象需要序列化,即类 implements Serializable。
本文地址:https://wizzer.cn/archives/2339 , 转载请保留.