欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

集群环境,一个apache+三台tomcat上实现负载均衡

程序员文章站 2022-07-13 13:59:10
...
安装软件版本,文本后面有软件以及配置文件下载地址
集群环境,一个apache负载均衡到三台tomcat上
apr-1.4.8.tar.gz
apr-util-1.5.1.tar.gz
jdk1.8.0_131.tar.gz
mod_jk-1.2.31-httpd-2.2.x.so
pcre-8.42.tar.gz
httpd-2.2.25.tar.gz
apache-tomcat-7.0.94.tar.gz


准备工作,在自己的linux(Centos7.4)环境上,新建一个目录,把上面的软件都上传到自己的目录,
然后全部解压tar.gz文件,tomcat解压备份三份,分别命名Tomcat71,Tomcat72,Tomcat73



第一步: 配置JDK的环境变量

# vi /etc/profile
#添加内容
#java environment
export JAVA_HOME=/home/software/jdk1.8.0_131/
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

#生效环境变量
# source /etc/profile

#检查是否生效  java,javac,java -version



第二步:修改tomcat的配置文件
由于本地安装在一台物理机器中,所以需要修改很多个位置,详情看修改位置备注信息server.xml。,三台tomcat都需要去修改下面的位置
修改内容如下:  具体修改内容,可以看三个tomcat的配置文件

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
-->
<!--修改位置一: Server port="8005"  在一台机器中,每个tomcat的端口要不同,如果在不同的物理机器中,那么不需要修改-->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <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>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
<!--修改位置二: Connector port="8080"  在一台机器中,每个tomcat的端口要不同,如果在不同的物理机器中,那么不需要修改也可以-->
<!--修改位置三: redirectPort="8443"  在一台机器中,每个tomcat的端口要不同,如果在不同的物理机器中,那么不需要修改也可以-->
    <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" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
<!--修改位置四: Connector port="8009"  在一台机器中,每个tomcat的端口要不同,如果在不同的物理机器中,那么不需要修改也可以-->
<!--修改位置五: redirectPort="8443"  在一台机器中,每个tomcat的端口要不同,这个值跟上面修改位置三值一样,如果在不同的物理机器中,那么不需要修改也可以-->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
<!--修改位置六: jvmRoute="Tomcat71"  jvmRoute修改为自己定义的值,后续集群配置需要-->
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="Tomcat71">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
 
      <!--修改位置七 打开这个,用于session共享-->
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
     

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

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

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>





第三步:安装apache
Apache至少需要apr、apr-util、pcre组件的支持。

1.查询是否安装了apache服务器httpd
[root@localhost src]# rpm -qa|grep httpd
httpd-2.4.6-45.el7.centos.x86_64
httpd-tools-2.4.6-45.el7.centos.x86_64
2.卸载系统自动装的apache服务器httpd
[root@localhost src]# httpd -k stop     #停止httpd服务器
[root@localhost src]# yum remove httpd    #卸载httpd服务器

3,检查系统是否安装了GCC
[root@localhost src]# gcc
bash: gcc: 未找到命令...      #出现未找到命令提示,说明没有安装GCC
4,安装GCC和gcc-c++
[root@localhost src]# yum -y install gcc
[root@localhost pcre-8.40]# yum -y install gcc-c++

5 编译安装apr-1.4.8                         
#改变目录到apr-apr-1.4.8
[root@localhost local]# cd apr-1.4.8
#指定apr的安装目录为/usr/local/apr 配置
[root@localhost apr-1.4.8]# ./configure -prefix=/usr/local/apr
#编译安装
[root@localhost apr-1.4.8]# make && make install
6,编译安装 apr-uril-1.5.2
#改变目录到apr-util-1.5.2/
[root@localhost local]# cd apr-util-1.5.2/
#指定apr-util的安装路径,指定apr-util所对应的apr
[root@localhost apr-util-1.5.2]# ./configure  -prefix=/usr/local/apr-util  -with-apr=/usr/local/apr
#编译安装
[root@localhost apr-util-1.5.2]# make && make install
7, 编译安装pcre
#卸载系统自带的pcre
[root@localhost apr-util-1.5.2]# rpm -qa pcre
pcre-8.32-15.el7_2.1.x86_64

#改变目录到pcre-8.42/
[root@localhost local]# cd pcre-8.42/
#指定prce的安装路径,指定apr-util所对应的apr
[root@localhost pcre-8.42]# ./configure  -prefix=/usr/local/pcre
#编译安装(make是编译 make install是安装)
[root@localhost pcre-8.42]# make && make install
8, 编译安装apache
编译和安装apache分为动态、静态两种方式。动态编译是指在以后的使用中随时调整配置文件就可以加载模块;静态则相反,在编译时就决定了相应的模块。
#改变目录到httpd-2.2.25/ 
[root@localhost local]# cd httpd-2.2.25/
#配置
[root@localhost httpd-2.2.25]# ./configure -prefix=/usr/local/apache -sysconfdir=/etc/httpd -enable-so -enable-rewrite -with-apr=/usr/local/apr -with-apr-util=/usr/local/apr-util -with-pcre=/usr/local/pcre
#编译安装
[root@localhost httpd-2.2.25]#make && make install

第四步:配置apache
到apache的目录,本文安装的目录为 /usr/local/apache/
修改httpd.conf,修改内容为:ServerName localhost:80


第五步:配置集群,
1,新建文件,mod_jk.conf,uriworkermap.properties,workers.properties
内容分别为为:


mod_jk.conf的内容:

#mod_jk.so文件的位置
LoadModule jk_module modules/mod_jk.so
#配置文件的位置
JkWorkersFile conf/workers.properties
#配置文件的位置
JkMountFile conf/uriworkermap.properties
#集群日志文件的位置
JkLogFile /usr/local/apache/logs/mod_jk.log
#日志打印级别
JkLogLevel warn 
#日志后期后缀格式
JkLogStampFormat  "[%a %b %d %H:%M:%S %Y]"



uriworkermap.properties的内容:
/admin/*=wlb
/manager/*=wlb
/jsp-examples/*=wlb
/servlets-examples/*=wlb
/examples/*=wlb
/*.jsp=wlb
/*=wlb
!/servlets-examples/*.jpeg=wlb
/jkmanager=jkstatus
!/*.gif=wlb
!/*.jpg=wlb
!/*.png=wlb
!/*.css=wlb
!/*.js=wlb
!/*.htm=wlb
!/*.html=wlb


workers.properties的内容:
#集群列表
worker.list=wlb,jkstatus
# Define preferred failover node for tomcat71
#修改为自己的tomcat的server.xml的端口名字以及 jvmRoute="Tomcat71" 的名字
worker.Tomcat71.port=8009
#一台机器,IP为localhost,不同物理机器,修改为对应的ip
worker.Tomcat71.host=localhost
worker.Tomcat71.type=ajp13
#负载因子
worker.Tomcat71.lbfactor=1


#worker.tomcat1.redirect=tomcat72
#修改为自己的tomcat的server.xml的端口名字以及 jvmRoute="Tomcat71" 的名字
worker.Tomcat72.port=8010
worker.Tomcat72.host=localhost
worker.Tomcat72.type=ajp13
worker.Tomcat72.lbfactor=1

#worker.tomcat1.redirect=tomcat73
#修改为自己的tomcat的server.xml的端口名字以及 jvmRoute="Tomcat71" 的名字
worker.Tomcat73.port=8011
#一台机器,IP为localhost,不同物理机器,修改为对应的ip
worker.Tomcat73.host=localhost
worker.Tomcat73.type=ajp13
worker.Tomcat73.lbfactor=1
#负载因子
worker.wlb.type=lb

worker.wlb.balance_workers=Tomcat71,Tomcat72,Tomcat73
worker.jkstatus.type=status


2,把分享的mod_jk.so文件放在/usr/local/apache/modules/  目录下
3,修改httpd.conf文件,添加内容:
Include conf/mod_jk.conf   把集群的配置文件放在apache中

第六步:测试
把新建的test项目放在三台tomcat中,然后启动tomcat71,tomcat72,tomcat73
查看是否启动成功:

[root@localhost bin]# ps -ef|grep tomcat
root       5003      1  5 11:23 pts/1    00:00:31 /home/leifengchuan/software/jdk1.8.0_131//bin/java -Djava.util.logging.config.file=/home/leifengchuan/software/Tomcat71/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Dignore.endorsed.dirs= -classpath /home/leifengchuan/software/Tomcat71/bin/bootstrap.jar:/home/leifengchuan/software/Tomcat71/bin/tomcat-juli.jar -Dcatalina.base=/home/leifengchuan/software/Tomcat71 -Dcatalina.home=/home/leifengchuan/software/Tomcat71 -Djava.io.tmpdir=/home/leifengchuan/software/Tomcat71/temp org.apache.catalina.startup.Bootstrap start
root       5022      1  4 11:24 pts/1    00:00:26 /home/leifengchuan/software/jdk1.8.0_131//bin/java -Djava.util.logging.config.file=/home/leifengchuan/software/Tomcat72/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Dignore.endorsed.dirs= -classpath /home/leifengchuan/software/Tomcat72/bin/bootstrap.jar:/home/leifengchuan/software/Tomcat72/bin/tomcat-juli.jar -Dcatalina.base=/home/leifengchuan/software/Tomcat72 -Dcatalina.home=/home/leifengchuan/software/Tomcat72 -Djava.io.tmpdir=/home/leifengchuan/software/Tomcat72/temp org.apache.catalina.startup.Bootstrap start
root       5041      1  4 11:24 pts/1    00:00:22 /home/leifengchuan/software/jdk1.8.0_131//bin/java -Djava.util.logging.config.file=/home/leifengchuan/software/Tomcat73/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Dignore.endorsed.dirs= -classpath /home/leifengchuan/software/Tomcat73/bin/bootstrap.jar:/home/leifengchuan/software/Tomcat73/bin/tomcat-juli.jar -Dcatalina.base=/home/leifengchuan/software/Tomcat73 -Dcatalina.home=/home/leifengchuan/software/Tomcat73 -Djava.io.tmpdir=/home/leifengchuan/software/Tomcat73/temp org.apache.catalina.startup.Bootstrap start
root       5415   4306  0 11:33 pts/1    00:00:00 grep --color=auto tomcat

说明三台tomcat启动




启动apache,  ./usr/local/apache/bin/apachectl start

出现:
[root@localhost bin]# ps -ef|grep httpd
root       5276      1  0 11:28 ?        00:00:00 /usr/local/apache/bin/httpd -k start
daemon     5277   5276  0 11:28 ?        00:00:00 /usr/local/apache/bin/httpd -k start
daemon     5278   5276  0 11:28 ?        00:00:00 /usr/local/apache/bin/httpd -k start
daemon     5279   5276  0 11:28 ?        00:00:00 /usr/local/apache/bin/httpd -k start
daemon     5280   5276  0 11:28 ?        00:00:00 /usr/local/apache/bin/httpd -k start
daemon     5281   5276  0 11:28 ?        00:00:00 /usr/local/apache/bin/httpd -k start
root       5321   4306  0 11:29 pts/1    00:00:00 grep --color=auto httpd



说明apache起来了,分别用三个浏览器去访问 
http://ip/test/test.jsp   可以看到每个浏览器访问的是不同的tomcat,实现负载均衡,每个浏览器,每次请求,session一致


链接:https://pan.baidu.com/s/11zMz5To_eN2mVGkYBDXHtw
提取码:xgza
相关标签: tomcat apache