参考文章:https://blog.youkuaiyun.com/brianang/article/details/70568902点击打开链接
网上有很多相关文章,此外作为本人学习的摘要和备忘。
使用tomcat7的时候采用的是mok方式集群,apache版本为2.2。
mok方式测试过apache2.4一样可以集群,但已经不推荐。
首先官网下载apache2.4和tomcat9,此处略过。
打开{apache2.4}/conf/httpd.conf,修改
Listen 127.0.0.1:81。
三.配置tomcat.
修改server.xml里面的端口
1.修改<Server port=" 8005" shutdown="SHUTDOWN">中的port值,修改为自定义。
2.修改<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />中的port值,修改为自定义。
3.修改<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />中的port值中的port值,修改为自定义。
4.<Engine name="Catalina" defaultHost="localhost" >修改 , 加上属性 jvmRoute="jvm1"。表示虚拟机的值。
新增tomcat请按上述四个步骤配置,修改port值和jvmRoute的值就可以。
此时如果启动两个tomcat发生错误,则要修改环境变量。
将tomcat/bin目录下的catalina.bat和startup.bat里的%CATALINA_HOME%替换为新配置的环境变量名即可。
四.测试项目
写一 个简单的index.jsp页面
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.*" %>
<html><head><title>Tomcat Cluster Demo</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
System.out.println("application:" + application.getAttribute(dataName));
application.setAttribute(dataName, dataValue);
}
out.print("<b>Session List</b>");
Enumeration<String> e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="index.jsp" method="POST">
Name:<input type=text size=20 name="dataName">
<br>
Value:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
<a href="test.jsp" >跳转</a>
</body>
</html>
test.jsp.
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.*" %>
<html><head><title>Tomcat Cluster Demo</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
System.out.println("application:" + application.getAttribute(dataName));
application.setAttribute(dataName, dataValue);
}
out.print("<b>Session List</b>");
Enumeration<String> e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="index.jsp" method="POST">
Name:<input type=text size=20 name="dataName">
<br>
Value:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
放入项目根目录下。
新建WEB-INF
新建web.xml并放入WEB-INF中。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns: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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<distributable/>
</web-app>
五.启动测试
配置apache服务。
进入apache安装路径/bin后httpd -k install -n "server name"如http -k install -n "apache2.4"安装服务
启动tomcat.
在浏览其中输入http://localhost:81可看到测试tomcat主页,输入http://localhost:81/项目名称 可看到项目主页,及负载均衡配置结果
end.