一、集群技术初步分析
1、tomcat集群多应用服务器,分担压力,并保证服务高可用,使用tomcat自带cluster实现集群环境,Manager负责管理session共享
2、web服务器采用Apache,负责应用服务器间负载均衡
3、apache服务器和tomcat的连接方法其实有三种:mod_JK、http_proxy和ajp_proxy,本文采用mod_JK模式
4、盗一张图,如下:
二、tomcat集群环境安装与配置
1、下载tomcat服务器,本文使用apache-tomcat-7.0.64版本,复制两份,分别为tomcat1和tomcat2,配置server.xml,以tomcat1为例,配置代码如下:
<!-- 服务关闭端口 -->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<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>
<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">
<!-- http连接器使用nio非阻塞模式,Excutor作为执行器提供线程池支持 -->
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="10"/>
<!-- 负责建立HTTP连接,使用nio非阻塞模式,配置使用executor执行器,使用连接池 -->
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443"
executor="tomcatThreadPool"/>
<!-- Define an AJP 1.3 Connector on port 8009 -->
<!-- AJP连接器,监听8009端口,采用AJP协议,连接tomcat服务器与http服务器(如Apache服务器),实现两者通信 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<!-- 使用AJP协议实现负载均衡,需要jvmRoute参数,值自定义
Tomcat会在创建session时会根据根据jvmRoute的值在sessionID后面追加route值
用于追踪分发的请求
-->
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
<!-- Clus