<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

  1. 云栖社区>
  2. 博客>
  3. 正文

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

王坤23 浏览668
展开阅读全文
  1. 我测试是在windows7上测测试的,
  2. 需要准备的软件列表如:
a. Apache2.2
b. apache-tomcat-6.0.32-1我要集群的第一台tomcat服务器
c. apache-tomcat-6.0.32-2我要集群的第二台tomcat服务器
d. mod_jk.so 用户连接apache服务器和tomcat之间的组件

  1. 根据我个人的习惯,我喜欢先配置apache的http.conf之后,然后在去配置tomcat.
找到你的apache的安装目录如我的是在D:\Apache2.2,然后点击conf目录,你将会找到一个叫httpd.conf的配置文件,为了能让初学者能够明白,我只http.conf中的最后一行代码,代码最后几行如下:大概在490行左右添加
# Various default settings
#Include conf/extra/httpd-default.conf

#引入conf下mod_jk.conf

include conf/mod_jk.conf

#红色部分就是你在原来http.conf中需要加入的内容
# Secure (SSL/TLS) connections
#Include conf/extra/httpd-ssl.conf
#
# Note: The following must must be present to support
#       starting without SSL on platforms with no /dev/random equivalent
#       but a statically compiled-in mod_ssl.
#
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

第二步就是在你安装的apache中的conf目录下加入两个文本文件mod_jk.conf和workers.properties.结构如下:

在文件mod_jk.conf的内容如下:该文本内容来源于网络.不知出处,若有原作发现 忘见谅

#D:\Apache2.2\conf\mod_jk.conf文件
#加载mod_jk Module
LoadModule jk_module modules/mod_jk.so

#指定workers.properties文件路径 
JkWorkersFile conf/workers.properties

#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名
JkMount /* controller

然后再workers.properties的内容如下:

#这是跟tomcat配置的重要文件D:\Apache2.2\conf\workers.properties

#server
worker.list = controller
#========tomcat1========
#ajp13端口号,在tomcat下server.xml配置,默认8009
worker.tomcat1.port=8009
#tomcat的主机地址,如不为本机,请填写ip地址 
worker.tomcat1.host=localhost
#协议类型
worker.tomcat1.type=ajp13
#server的加权比重,值越高,分得的请求越多。lbfactor是负载平衡因数(Load Balance Factor)
worker.tomcat1.lbfactor=1

#========tomcat2========
worker.tomcat2.port=8089
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1

#========controller,负载均衡控制器========
#负载均衡控制器类型,lbfactor是负载平衡因数(Load Balance Factor)
worker.controller.type=lb

#指定分担请求的tomcat列表
worker.controller.balanced_workers=tomcat1,tomcat2

#粘性session(默认是打开的) 当该属性值=true(或1)时,代表session是粘性的,即同一session在集群中的同一个节点上处理,session不跨越节点。在集群环境中,一般将该值设置为false
worker.controller.sticky_session=false

#设置用于负载均衡的server的session可否共享
worker.controller.sticky_session_force=1

然后重新启动apache服务器 能启动表示没问题,一般都不会有问题,到此apache中配置全部完毕。

记下来就是配置tomcat的,这里另外说一下,apache和tomcat集群其实实现很简单,原来就是通过ajp来实现的,只不过session复制的实现我还得在研究一下。不过我们用mod_jk.so来实现,session复制是已经实现了的、

tomcat配置这里要说明的是因为我是在同一台电脑,所以端口号一样是不能同时启动两个tomcat服务器的。所以首先要做的事就是修改tomcat1的server.xml配置文件后,复制到tomcat2下去 ,然后在修改端口号,这是你第一次配置会好点,不过不管你怎么弄 只要配置文件没错就行。我两台tomcat是这样子的:

然后修改后的C:\apache-tomcat-6.0.32-1\conf\server.xml文件内容如下:tomcat1啊 看仔细了

<?xml version='1.0' encoding='utf-8'?>
<!--修改01 这是关闭tomcat的端口号在 第一台服务器的端口号都是默认的 第二台需要修改-->
<Server port="8005" shutdown="SHUTDOWN">
  <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.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <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">
  <!--修改02 这是tomcat的http监听端口号在 第一台服务器的端口号都是默认的 第二台也需要修改-->
    <Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" 
			   URIEncoding="utf-8"/>
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
	 <!--修改03 jvmRoute="tomcat1"  这里的tomcat1一定要与的在D:\Apache2.2\conf\workers.properties目录下的名称要一直-->
    <Engine name="Catalina" defaultHost="localhost"  jvmRoute="tomcat1">
	<!--修改04 这是集群需要的 Cluster复制过去就行了 这里就是这样 不用特别修改-->
	 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
					 channelSendOptions="8">
			  <Manager className="org.apache.catalina.ha.session.DeltaManager"
					   expireSessionsOnShutdown="false"
					   notifyListenersOnReplication="true"/>

			  <Channel className="org.apache.catalina.tribes.group.GroupChannel">
			<Membership className="org.apache.catalina.tribes.membership.McastService"
							address="228.0.0.4"
							port="45564"
							frequency="500"
							dropTime="3000"/>
				<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
						  address="auto"
						  port="4000"
						  autoBind="100"
						  selectorTimeout="5000"
						  maxThreads="6"/>

				<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
				  <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
				</Sender>
				<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
				<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
			  </Channel>

			  <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
					 filter=""/>
			  <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

			  <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
						tempDir="/tmp/war-temp/"
						deployDir="/tmp/war-deploy/"
						watchDir="/tmp/war-listen/"
						watchEnabled="false"/>

			  <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
			  <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
			</Cluster>    
			<!--Cluster //END -->
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
      </Host>
    </Engine>
  </Service>
</Server>

然后tomcat2的我就是把配置文件粘贴过来了,就在tomcat1配置文件的基础上修改三处配置:
修改01
<Server port="8015" shutdown="SHUTDOWN">
修改02 http端口号
 <Connector port="8090" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" 
URIEncoding="utf-8"/>
修改03  这个端口要与你  这里的ajp端口号一定要与的在D:\Apache2.2\conf\workers.properties目录下的port要一直
  <Connector port="8089" protocol="AJP/1.3" redirectPort="8443" />

C:\apache-tomcat-6.0.32-2\conf\server.xml配置信息如下:

<?xml version='1.0' encoding='utf-8'?>
<Server port="8015" shutdown="SHUTDOWN">
  <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.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <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">
    <Connector port="8090" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" 
			   URIEncoding="utf-8"/>

    <Connector port="8089" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost"  jvmRoute="tomcat2">
	 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
					 channelSendOptions="8">

			  <Manager className="org.apache.catalina.ha.session.DeltaManager"
					   expireSessionsOnShutdown="false"
					   notifyListenersOnReplication="true"/>

			  <Channel className="org.apache.catalina.tribes.group.GroupChannel">
				<Membership className="org.apache.catalina.tribes.membership.McastService"
							address="228.0.0.4"
							port="45564"
							frequency="500"
							dropTime="3000"/>
				<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
						  address="auto"
						  port="4000"
						  autoBind="100"
						  selectorTimeout="5000"
						  maxThreads="6"/>

				<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
				  <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
				</Sender>
				<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
				<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
			  </Channel>

			  <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
					 filter=""/>
			  <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

			  <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
						tempDir="/tmp/war-temp/"
						deployDir="/tmp/war-deploy/"
						watchDir="/tmp/war-listen/"
						watchEnabled="false"/>

			  <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
			  <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
			</Cluster>    
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
      </Host>
    </Engine>
  </Service>
</Server>

然后分别启动tomcat1和tomcat2看能不能正常启动,

我的能够正常启动了,然后接下来就是测试tomcat直接的session共享问题了.
 
我这儿有一个test.war包是提供session共享的,测试结果如下


tomcat1中


最后结果证明,成功集群,但是大型系统中如果才用session共享复制,可不是一个好办法,更好是通过内存共享,或者ip绑定技术,就是你第一次访问的服务器一直与你保持会话。

今天百度网盘又上不去了,过几天把黑马程序员-深度揭秘服务器端内幕公开课资料视频连接也写到这里 还有里面的配置文件都弄到这里好了
http://pan.baidu.com/s/1pJiCgFT


网友评论

登录后评论
0/500
评论
王坤23
+ 关注