环境
操作系统:windows
tomcat1:Apache Tomcat/7.0.52(8085)
tomcat2:Apache Tomcat/7.0.52(8086)
jdk:1.8.0_251
apache-http:httpd-2.2.17-win32-x86-no_ssl(8888)
说明
基于httpd实现的session共享、负载均衡和反向代理。
部署
1、直接双击httpd-2.2.17-win32-x86-no_ssl.msi文件安装httpd
2、修改httpd\conf\httpd.conf
修改监听端口
Listen 8888
增加自定义配置文件的引入
include "D:\software\httpd\conf\mod_jk.conf"
3、在httpd\conf\下新建mod_jk.conf文件
内容:
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so
JkWorkersFile conf/workers.properties
JkMount /*.jsp controller
4、复制JK插件mod_jk-1.2.31-httpd-2.2.3.so到httpd\modules\下
5、在httpd\conf\下新建workers.properties文件
#server
worker.list=controller
#========tomcat1========
worker.tomcat1.port=8025
worker.tomcat1.host=127.0.0.1
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
#========tomcat2========
worker.tomcat2.port=8026
worker.tomcat2.host=127.0.0.1
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
说明:
1、worker.xxx.port/host/type/lbfactor中的xxx为tomcat中server.xml内的jvmRoute标签的内容
2、port是AJP/1.3的端口号
1、在tomcat\conf\server.xml中:
修改ip和端口号
<Connector port="8085" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8025" protocol="AJP/1.3" redirectPort="8443" />
增加jvmRoute配置
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
...
增加集群配置
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
1、部署test.jsp到测试包内
test.jsp内容参考如下(title和h1的内容可根据不同的tomcat进行调整以方便观察):
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>tomcat1</title>
</head>
<body>
<center><h1>tomcat1</h1></center>
<center>
<h3>sessionId:</h3><%=session.getId()%>
<h3>session创建时间:</h3><%=session.getCreationTime()%>
<center>
</body>
</html>
2、在所有工程包的web.xml中添加
配置
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<distributable/>
</web-app>
启动
1、双击httpd\bin\ApacheMonitor.exe启动
2、点击界面中的start按钮启动
看到如下界面说明启动成功
双击tomcat/bin/startup.bat启动多个tomcat
测试