linux下apache tomcat mod_jk负载均衡

时间:2022-04-29 22:42:46

apache-tomcat-7.0.6.tar.gz

httpd-2.2.17.tar.gz

jdk-6u23-linux-i586.bin

tomcat-connectors-1.2.31-src.tar.gz

1个apache  2个tomcat在一台机器上,修改其中一个tomcat相关端口,对应如下

8080     8980

8005     8905

8009     8909

1.安装httpd

tar zxvf httpd-2.2.17.tar.gz
cd httpd-2.2.17
./configure --prefix=/usr/local/apache2 --enable-so
make && make install

2.安装JDK

chmod +x jdk-6u18-linux-i586.bin
./jdk-6u18-linux-i586.bin
mv jdk1.6.0_18 /usr/local/jdk
rm -rf /usr/bin/java
rm -rf /usr/bin/javac
ln -s /usr/local/jdk/bin/java /usr/bin/java
ln -s /usr/local/jdk/bin/javac /usr/bin/javac

[root@centoslamp src]# java -version
java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) Client VM (build 16.0-b13, mixed mode, sharing)

ln -s /usr/local/jdk/jre /usr/local/jre

设置环境变量

vi /etc/profile 添加如下内容

JAVA_HOME=/usr/local/jdk
export JAVA_HOME
JRE_HOME=/usr/local/jre
export JRE_HOME

运行,使环境变量生效

source /etc/profile

3.安装tomcat

tar zxvf apache-tomcat-7.0.6.tar.gz
mv apache-tomcat-7.0.6 /usr/local/tomcat

tar zxvf apache-tomcat-7.0.6.tar.gz
mv apache-tomcat-7.0.6 /usr/local/tomcat2

4.安装tomcat-connectors

tar zxvf tomcat-connectors-1.2.31-src.tar.gz

cd tomcat-connectors-1.2.31-src/native/

./configure --with-apxs=/usr/local/apache2/bin/apxs --with-java-home=/usr/local/jdk
make && make install

5. 配置httpd.conf 文件

vi /usr/local/apache2/conf/httpd.conf

添加

LoadModule jk_module modules/mod_jk.so

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

6.创建并配置workers.properties文件

vi /usr/local/apache2/conf/workers.properties

worker.list=wlb , jkstatus

worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13
worker.worker1.lbfactor=1
# Define preferred failover node for worker1
#worker.worker1.redirect=worker2

worker.worker2.port=8909
worker.worker2.host=localhost
worker.worker2.type=ajp13
worker.worker2.lbfactor=1
# Disable worker2 for all requests except failover
#worker.worker2.activation=disabled


worker.wlb.type=lb
worker.wlb.balance_workers=worker1,worker2

worker.jkstatus.type=status

以上注释掉的两行如果开启,则worker2成为worker1的备用,在worker1不可用的情况下才会向worker2请求

7.创建并配置uriworkermap.properties

vi /usr/local/apache2/conf/uriworkermap.properties

/admin/*=wlb
/manager/*=wlb
/jsp-examples/*=wlb
/servlets-examples/*=wlb
/examples/*=wlb
/*.jsp=wlb
!/servlets-examples/*.jpeg=wlb

/jkmanager=jkstatus

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

8.编辑server.xml文件

vi /usr/local/tomcat/conf/server.xml

在这个位置修改

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/usr/local/apache2/htdocs" debug="0"/>

 

vi /usr/local/tomcat2/conf/server.xml

在这个位置修改

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/usr/local/apache2/htdocs" debug="0"/>

同时修改相关对应端口,如果在不同的机器上则不需要修改

<Connector port="8080" protocol="HTTP/1.1"
             connectionTimeout="20000"
             redirectPort="8443" />
  <!-- A "Connector" using the shared thread pool-->
  <!--
  <Connector executor="tomcatThreadPool"
             port="8080" protocol="HTTP/1.1"
             connectionTimeout="20000"
             redirectPort="8443" />

<Server port="8005" shutdown="SHUTDOWN">

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

 

9.在/usr/local/apache2/htdocs下创建测试文件test.jsp

vi /usr/local/apache2/htdocs/test.jsp

<%
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now <%= date %>

10.启动tomcat 和httpd 服务

[root@www src]# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

[root@www src]# /usr/local/tomcat2/bin/startup.sh

/usr/local/apache2/bin/apachectl restart

11.测试

访问http://192.168.15.124/test.jsp 时输出

Hello! The time is now Sun Jan 30 13:55:23 CST 2011

http://192.168.15.124/jkmanager 可以查看状态,可以看到每次请求依次连接到每个tomcat上

JK Status Manager for 192.168.15.124:80