【转载】Tomcat8 搭建集群
转载自:https://blog.csdn.net/qq_35662795/article/details/67674573
环境信息
操作系统: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
[html] view plain copy<%@ 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自带的注释)
[html] view plain copy<?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=&quo
文章来源:
Author:芸诺
link:http://yuuuo.com/?id=51