一步一步搭建apache+tomcat 负载均衡集群


环境准备

    2台虚拟机,centos6.6

    apache + tomcat1  10.0.0.141

    tomcat2                  10.0.0.140

apache的安装参照之前的文章

http://jwh5566.blog.51cto.com/7394620/1656742


安装jdk和tomcat 两台虚拟机都要做

#下载最新的jdk
cd /usr/local/src
wget 
tar zxvf  jdk-7u79-linux-x64.tar.gz
mv mv jdk1.7.0_79/ /usr/local/java

#输出环境变量
vim /etc/profile.d/java.sh
JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH

#使环境变量生效
source /etc/profile.d/java.sh

#下载最新的tomcat
cd /usr/local/src
wget 
tar zxvf  apache-tomcat-7.0.63.tar.gz
mv apache-tomcat-7.0.63 /usr/local/tomcat7

#输出环境变量
vim /etc/profile.d/tomcat.sh
CATALINA_HOME=/usr/local/tomcat7
PATH=$CATALINA_HOME/bin:$PATH
export CATALINA_HOME PATH

#使环境变量生效
source /etc/profile.d/tomcat.sh

#测试java和tomcat
[root@localhost ~]# java -version
java 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 version
Using CATALINA_BASE:   /usr/local/tomcat7
Using CATALINA_HOME:   /usr/local/tomcat7
Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.63
Server built:   Jun 30 2015 08:08:33 UTC
Server number:  7.0.63.0
OS Name:        Linux
OS Version:     2.6.32-358.el6.x86_64
Architecture:   amd64
JVM Version:    1.7.0_79-b15
JVM Vendor:     Oracle Corporation


修改tomcat1的server.xml

vim /usr/local/tomcat7/conf/server.xml
#在下面这一行添加jvmRoute="tomcat1"  标示后端的tomcat实例
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
#在默认主机上添加一个test应用
<Host name="localhost"  appBase="webapps"
    unpackWARs="true" autoDeploy="true">
<Context path="" docBase="test" debug="0"/>

#在tomcat7的应用目录下添加test目录和WEB-INF
cd /usr/local/tomcat7/webapps
mkdir test
cp -r ROOT/WEB-INF test
#新建测试的jsp文件
cd test
vim index.jsp
<%@ page language="java" %>
<html>
 <head><title>TomcatA</title></head>
 <body>
  <h1><font color="red">TomcatA </h1>
  <table align="centre" border="1">
   <tr>
    <td>Session ID</td>
  <% session.setAttribute("abc","abc"); %>
    <td><%= session.getId() %></td>
   </tr>
   <tr>
    <td>Created on</td>
    <td><%= session.getCreationTime() %></td>
   </tr>
  </table>
 </body>
</html>

#tomcat2的配置基本同tomcat1,只需改动jvmRoute="tomcat2"
#启动2个tomcat
startup.sh

安装jk

cd /usr/local/src
wget http://apache.fayea.com/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.40-src.tar.gz
tar -zxvf tomcat-connectors-1.2.40-src.tar.gz
cd tomcat-connectors-1.2.40-src/native/
./configure --with-apxs=/usr/local/apache/bin/apxs  --with-java-home=/usr/local/java
make
make install

配置mod_jk模块

vim conf/httpd.conf 添加一下内容
LoadModule jk_module  modules/mod_jk.so

<IfModule jk_module>
  JkWorkersFile conf/workers.properties
  JkMount   /* controller
  JkMount   /jkstatus jkstatus
  JkLogFile     logs/mod_jk.log
  JkLogLevel    info
</IfModule>
<Location /jkstatus>
  Options MultiViews
  AuthType Basic
  AuthName "Auther Center"
  AuthUserFile conf/.htpasswd
  require valid-user granted
</Location>

在conf目录下vim workers.properties加入如下内容

1 worker.list=controller,jkstatus
#========tomcat1========  
worker.tomcat1.port=8009        
#指定tomcat1服务器AJP的端口,默认为8009
worker.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


wKioL1XCHlWi1HgjAAE9MZFTxJI026.jpg

添加jkstatus访问用户

bin/htpasswd -c ../conf/.htpasswd admin

访问jkstatus

wKioL1XCH8fCPAzWAAJ5OIrBxSg126.jpg

你可能感兴趣的:(apache,tomcat,+)