Linux系统下塔建Apache和Tomcat负载均衡集群

1 软件下载

apache

tomcat

jk

apr apr-util

pcre

相关软件都准备好


systemctl start firewalld.service         #启动firewall
systemctl stop firewalld.service          #停止firewall
systemctl disable firewalld.service      #禁止firewall开机启动

1 查看系统已安装的jdk
[root@Server ~]# rpm -qa|grep jdk
java-1.8.0-openjdk-1.8.0.102-1.b14.el7_2.x86_64
java-1.7.0-openjdk-1.7.0.111-2.6.7.2.el7_2.x86_64
java-1.8.0-openjdk-headless-1.8.0.102-1.b14.el7_2.x86_64
java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.2.el7_2.x86_64
卸载jdk
rpm -e –nodeps java-1.8.0-openjdk-1.8.0.102-1.b14.el7_2.x86_64
rpm -e –nodeps java-1.7.0-openjdk-1.7.0.111-2.6.7.2.el7_2.x86_64
rpm -e –nodeps java-1.8.0-openjdk-headless-1.8.0.102-1.b14.el7_2.x86_64
rpm -e –nodeps java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.2.el7_2.x86_64
开始安装1.6版本的JDK
[root@Server share]# chmod +x jdk-6u45-linux-x64.bin
[root@Server share]# ./jdk-6u45-linux-x64.bin
再配置环境变量
[root@Server share]# vim /etc/profile
加入如下:
export JAVA_HOME=/usr/share/jdk1.6.0_45
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib 
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin 
保存退出
[root@Server share]# source /etc/profile
[root@Server share]# java -version
java version “1.6.0_45”
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
jdk安装完成

现在开始安装apache
[root@Server apr]# ./configure –prefix=/usr/local/apr
[root@Server apr]# make
[root@Server apr]# make install

安装apr-util
[root@Server apr-util]# ./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr
[root@Server apr-util]# make
[root@Server apr-util]# make install


安装pcre

[root@Server pcre]# ./configure –prefix=/usr/local/pcre
[root@Server pcre]# make
[root@Server pcre]# make install

安装apache
[root@Server httpd-2.29]# ./configure –prefix=/usr/local/apache –enable-so –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util/ –with-pcre=/usr/local/pcre
[root@Server httpd-2.29]# make
[root@Server httpd-2.29]# make install

安装JK
[root@Server tool]# tar xf tomcat-connectors-1.2.42-src.tar.gz
[root@Server tomcat-connectors-1.2.42-src]# cd native/
[root@Server native]# ./configure –with-apxs=/usr/local/apache/bin/apxs
[root@Server native]# make
[root@Server native]# make install

在此要检查一下
libstdc++-devel-4.1.2-46.el5.x86_64.rpm
gcc-c++-4.1.2-46.el5.x86_64.rpm
这二个rpm包有没有安装
完成后检查一下这个目录有没有jk这个文件
/usr/local/apache/modules/mod_jk.so
再给一个可执行的权限
vim httpd.conf,在其中添加如下内容:
ServerName 127.0.0.1
否则启动和关闭的时候会出现错误信息, 虽然不影响服务的访问
完成后检查一下这个目录有没有jk这个文件
/usr/local/apache/modules/mod_jk.so
再给一个可执行的权限
[root@Server modules]# chmod 777 mod_jk.so
开始配置apache文件
在conf目录下有个 http.conf文件配置
在里面新增如下一行话:
include “/usr/local/apache/conf/mod_jk.conf”

在conf路径下新建mod_jk.conf文件,并添加如下内容
#加载mod_jk Module
LoadModule jk_module modules/mod_jk.so
#指定 workers.properties文件路径
JkWorkersFile conf/workers.properties
#指定哪些请求交给tomcat处理,”loadbalancer”为在workers.propertise里指定的负载分配控制器名
JkLogFile logs/mod_jk.log
#JkLogLevel debug
JkMount /*.do loadbalancer
JkMount /*.jsp loadbalancer

在conf下新建workers.properties文件,并添加如下内容

#此处与mod_jk.conf中保持一致
worker.list=loadbalancer
#tomcat2中 ajp13 端口号,对应tomcat配置文件server.xml中Connector port=”8009″,默认8009
worker.tomcat1.port=8109
#worker2的地址,本机loaclhost或者127.0.0.1,远程直接写IP
worker.tomcat1.host=127.0.0.1
#与worker2中的server.xml的protocol保持一致
worker.tomcat1.type=ajp13
#负载的权重值,越高表示负载越大
worker.tomcat1.lbfactor=1
worker.tomcat2.port=8209
worker.tomcat2.host=127.0.0.1
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=tomcat1,tomcat2
#指定负载的worker列表,用逗号分隔
worker.loadbalancer.sticky_session=false
#配置session会话是否为粘性
#这样负载均衡器lb就会尽量保持一个session,也就是使用户在一次会话中跟同一个Tomcat进行交互
worker.loadbalancer.sticky_session_force=false
worker.loadbalancer.sticky_session=1
#是否启用session共享

这是修改http.conf文件
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
ProxyRequests Off
<proxy balancer://loadbalancer>
BalancerMember ajp://127.0.0.1:8109 loadfactor=1 route=tomcat1
BalancerMember ajp://127.0.0.1:8209 loadfactor=1 route=tomcat2
</proxy>


修改
conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin xxxx@xxx.com
ServerName 127.0.0.1
ServerAlias 127.0.0.1
ProxyPass / balancer://loadbalancer/ stickysession=jsessionid nofailover=On
ProxyPassReverse / balancer://loadbalancer/
ErrorLog “logs/loadbalancer-error.log”
CustomLog “logs/loadbalancer-access.log” common
</VirtualHost>

tomcat 1 的配置情况如下

<?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=”8105″ shutdown=”SHUTDOWN”>

  <!–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” />
  <!– JMX Support for the Tomcat server. Documentation at /docs/non-existent.html –>
  <Listener className=”org.apache.catalina.mbeans.ServerLifecycleListener” />
  <Listener className=”org.apache.catalina.mbeans.GlobalResourcesLifecycleListener” />

  <!– 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=”8180″ 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 JSSE configuration, when using APR, the 
         connector should be using the OpenSSL style configuration
         described in the APR documentation –>
    <!–
    <Connector port=”8443″ protocol=”HTTP/1.1″ SSLEnabled=”true”
               maxThreads=”150″ scheme=”https” secure=”true”
               clientAuth=”false” sslProtocol=”TLS” />
    –>

    <!– Define an AJP 1.3 Connector on port 8009 –>
    <Connector port=”8109″ 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″> 
    –> 
    <Engine name=”Catalina” defaultHost=”localhost” jvmRoute=”tomcat1″>

      <!–For clustering, please take a look at documentation at:
          /docs/cluster-howto.html (simple how to)
          /docs/config/cluster.html (reference documentation) –>
      
<Cluster className=”org.apache.catalina.ha.tcp.SimpleTcpCluster”
                 channelSendOptions=”6″>

          <Manager className=”org.apache.catalina.ha.session.BackupManager”
                   expireSessionsOnShutdown=”false”
                   notifyListenersOnReplication=”true”
                   mapSendOptions=”6″/>
          <!–
          <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=”5001″
                      selectorTimeout=”100″
                      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”/>
            <Interceptor className=”org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor”/>
          </Channel>

          <Valve className=”org.apache.catalina.ha.tcp.ReplicationValve”
                 filter=”.*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;”/>

          <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.ClusterSessionListener”/>
        </Cluster>
       

      <!– The request dumper valve dumps useful debugging information about
           the request and response data received and sent by Tomcat.
           Documentation at: /docs/config/valve.html –>
      <!–
      <Valve className=”org.apache.catalina.valves.RequestDumperValve”/>
      –>

      <!– 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”/>

      <!– Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       –>
      <Host name=”localhost” appBase=”webapps”
            unpackWARs=”true” autoDeploy=”true”
            xmlValidation=”false” xmlNamespaceAware=”false”>

        <!– 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 –>
        <!–
        <Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs” 
               prefix=”localhost_access_log.” suffix=”.txt” pattern=”common” resolveHosts=”false”/>
        –>

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

tomcat 2 的配置情况如下

<?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=”8205″ shutdown=”SHUTDOWN”>

  <!–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” />
  <!– JMX Support for the Tomcat server. Documentation at /docs/non-existent.html –>
  <Listener className=”org.apache.catalina.mbeans.ServerLifecycleListener” />
  <Listener className=”org.apache.catalina.mbeans.GlobalResourcesLifecycleListener” />

  <!– 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=”8280″ 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 JSSE configuration, when using APR, the 
         connector should be using the OpenSSL style configuration
         described in the APR documentation –>
    <!–
    <Connector port=”8443″ protocol=”HTTP/1.1″ SSLEnabled=”true”
               maxThreads=”150″ scheme=”https” secure=”true”
               clientAuth=”false” sslProtocol=”TLS” />
    –>

    <!– Define an AJP 1.3 Connector on port 8009 –>
    <Connector port=”8209″ 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″> 
    –> 
    <Engine name=”Catalina” defaultHost=”localhost” jvmRoute=”tomcat2″ >

      <!–For clustering, please take a look at documentation at:
          /docs/cluster-howto.html (simple how to)
          /docs/config/cluster.html (reference documentation) –>
        
   <Cluster className=”org.apache.catalina.ha.tcp.SimpleTcpCluster”
                 channelSendOptions=”6″>

          <Manager className=”org.apache.catalina.ha.session.BackupManager”
                   expireSessionsOnShutdown=”false”
                   notifyListenersOnReplication=”true”
                   mapSendOptions=”6″/>
          <!–
          <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=”5002″
                      selectorTimeout=”100″
                      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”/>
            <Interceptor className=”org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor”/>
          </Channel>

          <Valve className=”org.apache.catalina.ha.tcp.ReplicationValve”
                 filter=”.*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;”/>

          <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.ClusterSessionListener”/>
        </Cluster>
   

      <!– The request dumper valve dumps useful debugging information about
           the request and response data received and sent by Tomcat.
           Documentation at: /docs/config/valve.html –>
      <!–
      <Valve className=”org.apache.catalina.valves.RequestDumperValve”/>
      –>

      <!– 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”/>

      <!– Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       –>
      <Host name=”localhost” appBase=”webapps”
            unpackWARs=”true” autoDeploy=”true”
            xmlValidation=”false” xmlNamespaceAware=”false”>

        <!– 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 –>
        <!–
        <Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs” 
               prefix=”localhost_access_log.” suffix=”.txt” pattern=”common” resolveHosts=”false”/>
        –>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值