环境准备
2台虚拟机,centos6.6
apache + tomcat1 10.0.0.141
tomcat2 10.0.0.140
apache的安装参照之前的文章
安装jdk和tomcat 两台虚拟机都要做
#下载最新的jdkcd /usr/local/srcwget tar zxvf jdk-7u79-linux-x64.tar.gzmv mv jdk1.7.0_79/ /usr/local/java#输出环境变量vim /etc/profile.d/java.shJAVA_HOME=/usr/local/javaPATH=$JAVA_HOME/bin:$PATHexport JAVA_HOME PATH#使环境变量生效source /etc/profile.d/java.sh#下载最新的tomcatcd /usr/local/srcwget tar zxvf apache-tomcat-7.0.63.tar.gzmv apache-tomcat-7.0.63 /usr/local/tomcat7#输出环境变量vim /etc/profile.d/tomcat.shCATALINA_HOME=/usr/local/tomcat7PATH=$CATALINA_HOME/bin:$PATHexport CATALINA_HOME PATH#使环境变量生效source /etc/profile.d/tomcat.sh#测试java和tomcat[root@localhost ~]# java -versionjava version "1.7.0_79"Java(TM) SE Runtime Environment (build 1.7.0_79-b15)Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)[root@localhost ~]# catalina.sh versionUsing CATALINA_BASE: /usr/local/tomcat7Using CATALINA_HOME: /usr/local/tomcat7Using CATALINA_TMPDIR: /usr/local/tomcat7/tempUsing JRE_HOME: /usr/local/javaUsing CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jarServer version: Apache Tomcat/7.0.63Server built: Jun 30 2015 08:08:33 UTCServer number: 7.0.63.0OS Name: LinuxOS Version: 2.6.32-358.el6.x86_64Architecture: amd64JVM Version: 1.7.0_79-b15JVM Vendor: Oracle Corporation
修改tomcat1的server.xml
vim /usr/local/tomcat7/conf/server.xml#在下面这一行添加jvmRoute="tomcat1" 标示后端的tomcat实例#在默认主机上添加一个test应用 #在tomcat7的应用目录下添加test目录和WEB-INFcd /usr/local/tomcat7/webappsmkdir testcp -r ROOT/WEB-INF test#新建测试的jsp文件cd testvim index.jsp<%@ page language="java" %> TomcatA TomcatA
Session ID <% session.setAttribute("abc","abc"); %> <%= session.getId() %> Created on <%= session.getCreationTime() %> #tomcat2的配置基本同tomcat1,只需改动jvmRoute="tomcat2"#启动2个tomcatstartup.sh
安装jk
cd /usr/local/srcwget http://apache.fayea.com/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.40-src.tar.gztar -zxvf tomcat-connectors-1.2.40-src.tar.gzcd tomcat-connectors-1.2.40-src/native/./configure --with-apxs=/usr/local/apache/bin/apxs --with-java-home=/usr/local/javamakemake install
配置mod_jk模块
vim conf/httpd.conf 添加一下内容LoadModule jk_module modules/mod_jk.soJkWorkersFile conf/workers.properties JkMount /* controller JkMount /jkstatus jkstatus JkLogFile logs/mod_jk.log JkLogLevel info Options MultiViews AuthType Basic AuthName "Auther Center" AuthUserFile conf/.htpasswd require valid-user granted
在conf目录下vim workers.properties加入如下内容
1 worker.list=controller,jkstatus#========tomcat1======== worker.tomcat1.port=8009 #指定tomcat1服务器AJP的端口,默认为8009worker.tomcat1.host=10.0.0.141 #指定tomcat1服务器IP或域名 worker.tomcat1.type=ajp13 #指定tomcat1与apache AJP通讯协议 worker.tomcat1.lbfactor=1 #指定负载平衡因数,只有启用了负载平衡才有用. #========tomcat2======== worker.tomcat2.port=8009 worker.tomcat2.host=10.0.0.140 worker.tomcat2.type=ajp13 worker.tomcat2.lbfactor=1 #========controller负载平衡控制器======== worker.controller.type=lb #指定controller类型 worker.controller.balance_workers=tomcat1,tomcat2 #指定负载平衡的tomcat worker.controller.sticky_session=0#worker.controller.sticky_session_force=0#指定是否粘性session
最后重新启动httpd,刷新即可负载,持久会话的话,只需将上述
worker.controller.sticky_session=1
添加jkstatus访问用户
bin/htpasswd -c ../conf/.htpasswd admin
访问jkstatus