现在的位置: 首页 > 操作系统 > 正文

ubuntu中JK + Apache 2.x + Tomcat 7实现群集完整过程

2019年07月23日 操作系统 ⁄ 共 4362字 ⁄ 字号 评论关闭

一:系统环境和软件版本

操作系统:Ubuntu 13.10 x64

Tomcat :apache-tomcat-7.0.52

JDK : 1.7.0_51

Apache :Apache/2.4.9

JK : tomcat-connectors-1.2.40(下载地址:http://tomcat.apache.org/download-connectors.cgi)

 

二:部署环境

Apache代理服务器

192.168.1.200 安装路径:/usr/local/apache

Tomcat1

192.168.1.200 安装路径:/opt/apache-tomcat-7.0.52

Tomcat2

192.168.1.201 安装路径:/opt/apache-tomcat-7.0.52

Tomcat2

192.168.1.202 安装路径:/opt/apache-tomcat-7.0.52

 

192.168.1.200

192.168.1.201

192.168.1.202

三个linux上,均需要在/etc/hosts文件中,增加如下内容:

192.168.1.200       tomcat

192.168.1.201       cluster1     

192.168.1.202       cluster2

 

三:软件安装

1:Apache采用的是源码安装,安装之前需要先安装zlib、apr、apr-util,请先自行安装这些

   我这里安装到/usr/local/apache

2:JK的安装,先解压tomcat-connectors-1.2.40-src.tar.gz,进入到tomcat-connectors-1.2.40-src\native目录,执行./configure --with-apxs=/usr/local/apache/bin/apxs。编译完之后,执行make,make完之后,不必makeinstall。make之后,会在当前目录下产生apache-2.0目录,进入该目录,把mod_jk.so文件拷贝到/usr/local/apache/modules/目录

 

四:配置

1:在/usr/local/apache/conf目录新建 workers.properties文件,内容如下:

worker.list=DLOG4J, status

# localhost server 1
# ------------------------  
worker.tomcat1.port=8009  
worker.tomcat1.host=tomcat  
worker.tomcat1.type=ajp13  

# localhost server 2
# ------------------------  
worker.tomcat2.port=8009  
worker.tomcat2.host=cluster1  
worker.tomcat2.type=ajp13  

# localhost server 2
# ------------------------  
worker.tomcat3.port=8009  
worker.tomcat3.host=cluster2  
worker.tomcat3.type=ajp13   

worker.DLOG4J.type=lb
worker.retries=3
worker.DLOG4J.balanced_workers=tomcat1, tomcat2, tomcat3
worker.DLOG4J.sticky_session=1
worker.status.type=status

2:在/usr/local/apache/conf目录下新建uriworkermap.properties文件,内容如下:

/cloudos/*=DLOG4J
/jkstatus=status

!/cloudos/*.gif=DLOG4J
!/cloudos/*.jpg=DLOG4J
!/cloudos/*.png=DLOG4J
!/cloudos/*.css=DLOG4J
!/cloudos/*.js=DLOG4J
!/cloudos/*.htm=DLOG4J
!/cloudos/*.html=DLOG4J

在配置文件中,以“!”开头的条件表示“不要”,“=”表示交给。

因此条件“/cloudos*=DLOG4J”表示将任何请求交给负载均衡服务器。

条件“!/cloudos/*.jpg=DLOG4J”表示不要将.jpg结尾的请求交给负载均衡服务器

 

3:配置/usr/local/apache/conf/httpd.conf文件

(1)    把原来的DocumentRoot 目录地址改为:DocumentRoot "/src"

(2)    增加如下内容:

<Directory "/src">

Options Indexes FollowSymLinks

AllowOverride None

Require all granted

</Directory>

(3)  在有很多LoadModule语句的地方,末尾追加一行

LoadModule jk_module modules/mod_jk.so

(4)  然后在写有<IfModuleXXXX>的区域追加一行如下配置

<IfModulejk_module>
  JkWorkersFile conf/workers.properties
  JkMountFile conf/uriworkermap.properties
  JkLogFile logs/mod_jk.log
  JkLogLevel warn
</IfModule>

 

4:配置tomcat

(1)    创建目录/src

(2)    把项目放入/src目录,我这里的项目是:cloudos

(3)    把tomcat安装文件解压到/opt目录,配置/opt/apache-tomcat-7.0.52/conf/ server.xml文件,

在<Engine name="Catalina"defaultHost="localhost">标签里面加入如下内容:

<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>

PS:<Cluster...></Cluster>节点之间部分不必自己动手敲进去,在tomcat服务器目录的/webapps/docs/cluster-howto.html文件中有这一段文字,拷贝出来贴到server.xml文件中即可。

(4) 在<Hostname="localhost">标签下,配置一个应用

<Context path="/cloudos"docBase="/src/cloudos" />

(5) 把/src、以及配置好的Tomcat整个目录拷贝到其它2个服务器上

五:启动服务

依次启动3个tomcat,最后再启动apache

 

六:注意事项

1:httpd.conf文件中的DocumentRoot配置的目录,和

Tomcat配置<Contextpath="/cloudos" docBase="/src/cloudos" />两个路径必须保持一致,否则将会出现静态资源无法访问的问题

2:web.xml文件中,必须要有 <distributable/>这一句

3:放入session的所有对象,必须实现java.io.Serializable接口

抱歉!评论已关闭.