tomcat负载均衡

node2#rpm -ivh jdk-7u67-linux-x64.rpm 

         #vim /etc/profile.d/java.sh

            export JAVA_HOME=/usr/java/latest

            export PATH=$JAVA_HOME/bin:$PATH

         #tar xf apache-tomcat-7.-.55-C /usr/local

         #cd /usr/local

         #ln -sv apache-tomcat-7.0.55 tomcat

         #cd tomcat

         #useradd tomcat

         #chown -R tomcat.tomcat ./*

         #vim /etc/profile.d/tomcat.sh

           export CATALINA_HOME=/usr/local/tomcat

           export PATH=$CATALINA_HOME/bin:$PATH

         #su -tomcat -c ‘/usr/local/tomcat/bin/catalina.sh start’

         #cd /etc/profile.d/

         #scp java.sh tomcat.sh node3:/etc/profile.d/


node3: #rpm -ivh jdk-7u67-linux-x64.rpm 

         #tar xf apache-tomcat-7.0.55 -C /usr/local

         #cd /usr/local

         #ln -sv apache-tomcat-7.0.55 tomcat

         #cd tomcat

         #useradd tomcat

         #chown -R tomcat.tomcat ./*

         #su -tomcat -c ‘/usr/local/tomcat/bin/catalina.sh start’

         #echo $PATH

         #catalina.sh start

         #ps aux | grep java(tomcat 身份运行java)

         #catalina.sh stop


node2:#cd /usr/local/tomcat/conf/

     #vim server.xml

<Enginename="Catalina"defaultHost="node2.stu7.com jvmRoute="TomcatA">

  <Host name="node2.stu7.com"  appBase="/webapps"

            unpackWARs="true" autoDeploy="true">

   <Host name="node2.stu7.com"  appBase="/webapps"

            unpackWARs="true" autoDeploy="true">

           <Context path="" docBase="ROOT" reloadable="true" />

      </Host>

  

 

node2:#mkdir -pv /webapps/ROOT/{lib,classes, WEB-INF}

      # cd /webapps/ROOT/

     #vim index.jsp

<%@ page language="java" %>

<html>

  <head><title>TomcatA</title></head>

  <body>

    <h1><font color="red">TomcatA.stu7.com</font></h1>

    <table align="centre" border="1">

      <tr>

        <td>Session ID</td>

    <% session.setAttribute("stu7.com","stu7.com"); %>

        <td><%= session.getId() %></td>

      </tr>

      <tr>

        <td>Created on</td>

        <td><%= session.getCreationTime() %></td>

     </tr>

    </table>

  </body>

</html>


#su -tomcat -c ‘catalina.sh start’

打开浏览器:172.16.249.125:8080

wKiom1TZ9z_w3yEZAADDnKx_JNw007.jpg


node2:#scp conf/server.xml node3:/usr/local/tomcat/conf/


node3:#mkdir -pv /webapps/ROOT/{lib,classes, WEB-INF}

      #cd /webapps/ROOT/

     #vim index.jsp

     <%@ page language="java" %>

<html>

  <head><title>TomcatB</title></head>

  <body>

    <h1><font color="blue">TomcatB.stu7.com</font></h1>

    <table align="centre" border="1">

      <tr>

        <td>Session ID</td>

    <% session.setAttribute("stu7.com","stu7.com"); %>

        <td><%= session.getId() %></td>

      </tr>

      <tr>

        <td>Created on</td>

        <td><%= session.getCreationTime() %></td>

     </tr>

    </table>

  </body>

</html>

#su -tomcat -c ‘catalina.sh start’

打开浏览器;172.16.249.126:8080

wKiom1TZ93mzDidWAADHBaBQBhs897.jpg

nginx方式:

node1:yum install nginx -y

  #vim /etc/nginx/nginx.conf

   upstream tcsrvs {

        server 172.16.249.125:8080;

        server 172.16.249.99:8080;

     }

#vim /etc/nginx/conf.d/default.conf

 location / {

        #root   /usr/share/nginx/html;

        proxy_pass http://tcsrvs;

        index  index.html index.htm;

    }

#nginx -t

#servce nginx start

打开浏览器:172.16.249.101


wKioL1TZ-JTjrIZDAAC-X-Bc3hE196.jpg

wKiom1TZ97iiLMpOAADBeLLz5_s960.jpg


http方式:

node1:# httpd -M

      proxy_balancer_module (shared)

      proxy_http_module (shared)

      proxy_ajp_module (shared)

  #cd /etc/httpd/conf/

      #vim /httpd.conf

         #DocumentRoot "/var/www/html"

      #vim mod_http.conf (文件必须放在/etc/httpd/conf.d/)

    NameVirtualHost *:80

<Proxy balancer://tcsrvs>

    BalancerMember http://172.16.249.125:8080 loadfactor=1

    BalancerMember http://172.16.249.99:8080 loadfactor=1

    ProxySet lbmethod=byrequests

</Proxy>

 <VirtualHost *:80>

       ServerName node4.stu7.com

        ErrorLog logs/error_log

       CustomLog logs/access_log common

       ProxyVia On

       ProxyRequests OFF

       proxypreserveHost On

       <Proxy *>

       Order deny,allow

       Allow from all

       </Proxy>

       Proxypass /status !

       ProxyPass / balancer://tcsrvs/

       proxypassReverse / balancer://tcsrvs/

      <location />

        Order deny,allow

        Allow from all

      </location>

</VirtualHost>

#service httpd start

#ss -tnl 80端口处于监听状态

打开浏览器:172.16.249.101:

wKioL1TZ-NXifAI2AAC44OZakYE782.jpg

wKiom1TZ9_qhIZjuAAC4dS4fQFM009.jpg


ajp方式:

#cd /etc/httpd/conf/

      #vim /httpd.conf

         #DocumentRoot "/var/www/html"

      #vim mod_ajp.conf (文件必须放在/etc/httpd/conf.d/)

    NameVirtualHost *:80

<Proxy balancer://tcsrvs>

    BalancerMember http://172.16.249.125:8009 loadfactor=1

    BalancerMember http://172.16.249.99:8009 loadfactor=1

    #ProxySet lbmethod=byrequests

</Proxy>

 <VirtualHost *:80>

       ServerName node4.stu7.com

        ErrorLog logs/error_log

       CustomLog logs/access_log common

       ProxyVia On

       ProxyRequests OFF

       proxypreserveHost On

       <Proxy *>

       Order deny,allow

       Allow from all

       </Proxy>

       Proxypass /status !

       ProxyPass / balancer://tcsrvs/

       proxypassReverse / balancer://tcsrvs/

      <location />

        Order deny,allow

        Allow from all

      </location>

</VirtualHost>

#service httpd start

#ss -tnl 80端口处于监听状态

打开浏览器:172.16.249.101:

wKioL1TZ-RThHjDwAAC9S8odWmo312.jpg

wKiom1TZ-DTRvtBiAAC_-eeyZ90978.jpg

制定管理页面不代理:

      #cd /etc/httpd/conf.d/

      #vim mod_ajp.conf  

NameVirtualHost *:80

<Proxy balancer://tcsrvs>

    BalancerMember ajp://172.16.249.125:8009 loadfactor=1

    BalancerMember ajp://172.16.249.99:8009 loadfactor=1

   # ProxySet lbmethod=byrequests

</Proxy>

 <VirtualHost *:80>

       ServerName node4.stu7.com

        ErrorLog logs/error_log

       CustomLog logs/access_log common

       ProxyVia On

       ProxyRequests OFF

       proxypreserveHost On

       <Proxy *>

       Order deny,allow

       Allow from all

       </Proxy>

       <Location /balancer-manager>

       SetHandler balancer-manager

       Proxypass !

       Order deny,allow

       Allow from all

       </Location>

       Proxypass /status !

       ProxyPass / balancer://tcsrvs/

       proxypassReverse / balancer://tcsrvs/

      <location />

        Order deny,allow

        Allow from all

      </location>

</VirtualHost>

#serivce httpd restart

打开浏览器:172.16.249.101/balancer-manager

wKioL1TZ-VHBOQNPAAN6cK4tBe8105.jpg


mod_jk方式:

   #lftp 172.16.0.1/pub/Sources/6.x86_64/tomcat

       tomcat-connectors-1.2.40-src.tar.gz 

   #yum install httpd-devel -y

   #tar xf tomcat-connectors-1.2.40-src.tar.gz 

   # cd tomcat-connectors-1.2.40-src/native

   #./configure --with-apxs=`which apxs`

   #make && make install

  #ls /usr/lib64/httpd/moudules/

  #cd /etc/httpd

  #vim conf.d/mod_jk.conf

     LoadModule jk_module modules/mod_jk.so

     JkWorkersFile /etc/httpd/conf.d/workers.properties

     JklogFile logs/mod_jk.log

     JklogLevel info

     JkMOunt /status !

     JkMount /jk-status StatA

     JkMount /* tcsrvs

#vim workers.properties

worker.list=tcsrvs,StatA

worker.TomcatA.port=8009

worker.TomcatA.host=172.16.249.125

worker.TomcatA.type=ajp13

worker.TomcatA.lbfactor=1

worker.TomcatB.port=8009

worker.TomcatB.host=172.16.249.99

worker.TomcatB.type=ajp13

worker.TomcatB.lbfactor=1

worker.tcsrvs.type=lb

worker.tcsrvs.sticky_session=0

worker.tcsrvs.balance_workers=TomcatA,TomcatB

worker.statA.type=status

 #service httpd restart

打开浏览器:172.16.249.101:

wKiom1TZ-JOQbjbKAAC-AbMkF80963.jpg

wKioL1TZ-aGDWTyrAADAWjKsDlU032.jpg

session绑定:

#vim /usr/local/tomcat/conf/server.xml

<Enginename="Catalina"defaultHost="node2.stu7.com jvmRoute="TomcatA">

#su - tomcat -c catalina.sh start

#vim workers.properties

worker.list=tcsrvs,StatA

worker.TomcatA.port=8009

worker.TomcatA.host=172.16.249.125

worker.TomcatA.type=ajp13

worker.TomcatA.lbfactor=1

worker.TomcatB.port=8009

worker.TomcatB.host=172.16.249.99

worker.TomcatB.type=ajp13

worker.TomcatB.lbfactor=1

worker.tcsrvs.type=lb

worker.tcsrvs.sticky_session=1

worker.tcsrvs.balance_workers=TomcatA,TomcatB

worker.statA.type=status

 #service httpd restart

打开浏览器:172.16.249.101:

wKioL1TZ-crDGQ9rAAC_7xiLBgo176.jpg

Delta会话集群配置

 

node2:#nano server.xml

    在Engine或Host组件内部定义Cluster组件:

     <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"

        channelSendOptions="8">

 

     <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.101.10.41"

          port="45564"

          frequency="500"

          dropTime="3000"/>

      <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

        address="172.16.249.125"

        port="4000"

        autoBind="100"

        selectorTimeout="5000"

        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"/>

      </Channel>

 

      <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"

        filter=""/>

      <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

 

      <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.JvmRouteSessionIDBinderListener"/>

      <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>

      </Cluster>

     

      #vim server.xml

        <Enginename="Catalina"defaultHost="localhost"jvmRoute="TomcatA">

     <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"

                 channelSendOptions="8">

 

          <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.101.10.24" 修改组播地址

                        port="45564"

                        frequency="500"

                        dropTime="3000"/>

            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

                      address="172.16.249.125"  节点地址

                      port="4000"

                      autoBind="100"

                      selectorTimeout="5000"

                      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"/>

          </Channel>

 

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"

                 filter=""/>

          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

 

          <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.JvmRouteSessionIDBinderListener"/>

          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>

        </Cluster>

<Host name="localhost"  appBase="/webapps"

            unpackWARs="true" autoDeploy="true">

 

      node2#scp server.xml node3:/usr/local/tomcat/conf/server/xml

      node3:#vim server.xml

<Enginename="Catalina"defaultHost="localhost"jvmRoute=TomcatB">

     <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"

                 channelSendOptions="8">

 

          <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.101.10.24" 修改组播地址

                        port="45564"

                        frequency="500"

                        dropTime="3000"/>

            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

                      address="172.16.249.99"  节点地址

                      port="4000"

                      autoBind="100"

                      selectorTimeout="5000"

                      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"/>

          </Channel>

 

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"

                 filter=""/>

          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

 

          <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.JvmRouteSessionIDBinderListener"/>

          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>

        </Cluster>

<Host name="localhost"  appBase="/webapps"

            unpackWARs="true" autoDeploy="true">

 

node2:#cp web.xml /webapps/ROOT/WEB-INF/

      #cd /webapps/ROOT/WEB-INF/

      #vim web.xml 

        添加<distributable />

      #scp web.xml node2:/webapps/ROOT/WEB-INF/

node2:#su - tomcat -c 'catalina.sh stop'

node2:#su - tomcat -c 'catalina.sh start'

node3:#su - tomcat -c 'catalina.sh stop'

node3:#su - tomcat -c 'catalina.sh start'

node2:#cd /usr/local/tomcat/

      #tail logs/catalina.out

wKioL1TZ-gSz0el1AAZWC5Ygaag487.jpg


打开浏览器:172.16.249.101:(无论怎么刷新都是session id一致)

wKioL1TZ-iWwU7NLAACx_5r2knc544.jpg

wKiom1TZ-ULBbgqCAAC2mXsJi-k603.jpg

说明:   

确保Engine组件中存在jvmRoute属性,其值要与mod_jk配置中使用worker同名;

      <Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcatA">

 

    为打算使用集群会话的webapp的web.xml配置文件(一般在META-INF)添加

      <distributable/>


你可能感兴趣的:(tomcat,负载均衡)