tomcat



爲三臺虛擬主機分別提供互相解析的hosts文件,並同步時間html

[root@c7node2 ~]# vim /etc/hosts
192.168.0.22    c7node1.wlw.com
192.168.0.11    c7node2.wlw.com     
192.168.0.33    C6node2.wlw.com 
[root@c7node2 ~]# ansible tomcat -m copy -a 'src=/etc/hosts dest=/etc/'
[root@c7node2  ~]#  ansible all  -a 'ntpdate cn.pool.ntp.org

  

LNMT:java

client -->http --> nginx --> reverse_proxy --> http --> tomcat (http connector)node

c7node1.wlw.com    192.168.0.22    tomcat服務器nginx

C6node2.wlw.com    192.168.0.33    nginx反向代理web


爲c7node1.wlw.com建立webapp特有的目錄結構,並提供首頁文件
apache

[root@c7node1 ~]# mkdir -pv /data/webapps/ROOT/{lib,classes,WEB-INF,META-INF}
[root@c7node1 ~]# vim /data/webapps/ROOT/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="red">TomcatA </font></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>

建立一個虛擬主機並設置爲默認主機vim

[root@c7node1 ~]# vim /usr/local/tomcat/conf/server.xml
<Engine name="Catalina" defaultHost="c7node1.wlw.com">
........
     <Host name="c7node1.wlw.com" appBase="/data/webapps">
        <Context path="" docBase="ROOT" />
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"
               prefix="c7node1_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
........


爲C6node2.wlw.com提供nginx反向代理配置,除了jsp,do結尾的文件反向代理給tomcat處理,其餘的靜態文件由nginx處理tomcat

[root@C6node2 ~]# vim /etc/nginx/conf.d/default.conf 
    location / {

    }
    location ~* \.(jsp|do)$ {
        proxy_pass http://c7node1.wlw.com:8080/;
    }


wKioL1YvrZigRVFvAAEQfPN0D_g914.jpg


LAMT:bash

  client --> http --> httpd --> reverse_proxy --> {http|ajp} --> tomcat {http connector|ajp connector}服務器

c7node1.wlw.com    192.168.0.22    tomcat服務器

C6node2.wlw.com   192.168.0.33    httpd反向代理


爲C6node2.wlw.com提供http(主:proxy_module,子:proxy_module_http)反向代理配置

[root@C6node2 ~]# vim /etc/httpd/conf/httpd.conf 
<VirtualHost *:80>
    ServerName c7node1.wlw.com
    ProxyVia on
    ProxyPreserveHost on
    ProxyRequests off
    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>
    ProxyPass / http://192.168.0.22:8080/
    ProxyPa***everse / http://192.168.0.22:8080/
    <Location />
        Order allow,deny
        Allow from all
    </Location>
</VirtualHost>


爲C6node2.wlw.com提供http(主:proxy_module,子:proxy_module_ajp)反向代理配置

[root@C6node2 ~]# vim /etc/httpd/conf/httpd.conf 
<Location /balancer-manager>
    SetHandler balancer-manager
    Proxypass !
    Order Deny,Allow
    Allow from all
</Location>
#狀態頁面
<VirtualHost *:80>
    ServerName c7node1.wlw.com
    ProxyVia on
    ProxyPreserveHost on
    ProxyRequests off
    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>
    ProxyPass /status !
    ProxyPass / ajp://192.168.0.22:8080/
    ProxyPa***everse / ajp://192.168.0.22:8080/
    <Location />
        Order allow,deny
        Allow from all
    </Location>
    <Location /status>
        SetHandler server-status
        Order deny,allow
        Deny from all
        Allow from 192.168
    </Location>
</VirtualHost>

wKioL1YvrZigRVFvAAEQfPN0D_g914.jpg

wKioL1YvtdTxc3lNAARl2DSMxdw108.jpg

wKioL1YwpzPDp6NnAAJtPesKE6I837.jpg





利用nginx實現tomcat的負載均衡

[root@C6node2 conf.d]# vim /etc/nginx/nginx.conf
http {
........
    upstream tomcat {
        server c7node1.wlw.com:8080;
        server c7node2.wlw.com:8080;
    }
server {
    listen       80 default_server;
    location / {

    }
    location ~* \.(jsp|do)$ {
#       ip_hash;    若是須要基於源IP地址hash,能夠添加此選項
        proxy_pass http://tomcat;
    }
........
}

wKioL1YwQ0jisAzfAACuNCpjd94222.jpg

wKiom1YwQxTDXTRGAACwPBweHEM486.jpg


基於mod_jk的反向代理

編譯安裝mod_jk

[root@C6node2 ~]# wget http://apache.fayea.com/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.41-src.tar.gz
[root@C6node2 ~]# tar xf tomcat-connectors-1.2.41-src.tar.gz
[root@C6node2 ~]# cd tomcat-connectors-1.2.41-src/native/
[root@C6node2 native]# yum install -y httpd-devel
[root@C6node2 native]# ./configure --with-apxs=/usr/sbin/apxs
[root@C6node2 native]# make && make install

配置mod_jk

[root@C6node2 ~]# cd /etc/httpd/conf.d
[root@C6node2 conf.d]# vim httpd-jk.conf
LoadModule  jk_module  modules/mod_jk.so
JkWorkersFile   /etc/httpd/conf.d/workers.properties
JkLogFile       logs/mod_jk.log
JkLogLevel      debug
JkMount    /*   TomcatA
JkMount  /status  stat1

[root@C6node2 conf.d]# vim workers.properties
worker.list=TomcatA,stat1
worker.TomcatA.port=9090
worker.TomcatA.host=192.168.0.11
worker.TomcatA.type=ajp13
worker.TomcatA.lbfactor=1
worker.stat1.type = status

wKiom1YwgIWQdYkRAAC9tX_mUVY368.jpg

wKioL1YwgLmyAL1zAAT0W0TjJlg014.jpg




利用httpd實現tomcat的負載均衡

①、 apache: mod_proxy,mod_proxy_http,mod_proxy_balancer

   tomcat:http connector(端口爲:8080)

[root@C6node2 conf]# vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
    ServerName c6node2.wlw.com
    ProxyVia on
    ProxyPreserveHost on
    ProxyRequests off
    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>
#   Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
    <Proxy balancer://tomcatsr>
      BalancerMember http://192.168.0.22:8080 loadfactor=1 route=TomcatA
      BalancerMember http://192.168.0.11:8080 loadfactor=1 route=TomcatB
#     ProxySet stickysession=ROUTEID    這裏是基於route作綁定,能夠添加上面Header信息,測試可加可不加
#     ProxySet stickysession=JSESSIONID    這裏基於session作綁定
    </Proxy>
    ProxyPass /status !
    ProxyPass / balancer://tomcatsr/
    ProxyPa***everse / balancer://tomcatsr/
    <Location />
        Order allow,deny
        Allow from all
    </Location>
    <Location /status>
        SetHandler server-status
        Order deny,allow
        Deny from all
        Allow from 192.168
    </Location>
</VirtualHost>


②、 apache: mod_proxy,mod_proxy_ajp,mod_proxy_balancer

   tomcat:ajp connector(端口爲:9090)

[root@C6node2 conf]# vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
    ServerName c6node2.wlw.com
    ProxyVia on
    ProxyPreserveHost on
    ProxyRequests off
    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>
#   Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
    <Proxy balancer://tomcatsr>
      BalancerMember ajp://192.168.0.22:9090 loadfactor=1 route=TomcatA
      BalancerMember ajp://192.168.0.11:9090 loadfactor=1 route=TomcatB
#     ProxySet stickysession=ROUTEID    這裏是基於route作綁定,能夠添加上面Header信息,測試可加可不加
#     ProxySet stickysession=JSESSIONID    這裏基於session作綁定
    </Proxy>
    ProxyPass /status !
    ProxyPass / balancer://tomcatsr/
    ProxyPa***everse / balancer://tomcatsr/
    <Location />
        Order allow,deny
        Allow from all
    </Location>
    <Location /status>
        SetHandler server-status
        Order deny,allow
        Deny from all
        Allow from 192.168
    </Location>
</VirtualHost>



③、 apache:mod_jk

    tomcat:ajp connector(端口:9090)


配置mod_jk

[root@C6node2 ~]# cd /etc/httpd/conf.d
[root@C6node2 conf.d]# vim httpd-jk.conf
LoadModule  jk_module  modules/mod_jk.so
JkWorkersFile  /etc/httpd/conf.d/workers.properties
JkLogFile  logs/mod_jk.log
JkLogLevel  debug
JkMount  /*  lbcluster
JkMount  /status  stat

[root@C6node2 conf.d]# vim workers.properties
worker.list = lbcluster,stat
worker.TomcatA.type = ajp13
worker.TomcatA.host = 192.168.0.22 
worker.TomcatA.port = 9090         #這裏配置的是ajp connector的端口
worker.TomcatA.lbfactor = 5
worker.TomcatB.type = ajp13
worker.TomcatB.host = 192.168.0.11
worker.TomcatB.port = 9090
worker.TomcatB.lbfactor = 5
worker.lbcluster.type = lb
worker.lbcluster.sticky_session = 0         #設置爲1則開啓session綁定
worker.lbcluster.balance_workers = TomcatA, TomcatB
worker.stat.type = status



Session Cluster:

    session manager:會話管理器

        StandardManager

[root@c7node1 ROOT]# vim /usr/local/tomcat/conf/server.xml
      </Host>
      <Host name="c7node1.wlw.com" appBase="/data/webapps">
        <Context path="" docBase="ROOT" />
        <Manager className="org.apache.catalina.session.StandardManager"
                maxInactiveInterval="7200" />
      </Host>

[root@c7node1 ROOT]# pwd
/usr/local/tomcat/work/Catalina/c7node1.wlw.com/ROOT
[root@c7node1 ROOT]# ls
org  SESSIONS.ser
#能夠看到這裏生成了SESSIONS.ser會話文件


        PersistentManager:

            FileStore


            JDBC

        DeltaManager

①、各節點配置使用deltamaanager

[root@c7node2 WEB-INF]# vim /usr/local/tomcat/conf/server.xml
 <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.0.4.8"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="auto"
                      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.ClusterSessionListener"/>
        </Cluster>

②、爲須要使用session cluster的webapps開啓session。添加distribution的功能

[root@c7node2 WEB-INF]# cp /usr/local/tomcat/conf/web.xml /data/webapps/ROOT/WEB-INF/
[root@c7node2 WEB-INF]# vim /data/webapps/ROOT/WEB-INF/web.xml 
........
    <distributable/>
</web-app>

wKiom1YwzB2iY_5LAAC5pKEWxgE679.jpg

wKioL1YwzFKg2MACAAC6Bjhv180345.jpg

能夠看到負載均衡後會話不會丟失


        BackupManager

相關文章
相關標籤/搜索