tomcat集羣

tomcat集羣 php

 

httpd負載均衡 html

httpd -M node

確保有proxy_balancer_modeule模塊 nginx

編輯httpd.conf web

註釋DocumentRoot apache

編輯conf.d/mod_httpd.conf vim

定義虛擬主機 後端

兩種方式都能實現負載均衡: tomcat

  • 基於mod_proxy模塊實現負載均衡
  • 基於mod_jk模塊實現負載均衡

下面咱們就來分別演示一下, 服務器

1.基於mod_proxy實現負載均衡

定義虛擬主機

編輯conf.d/mod_httpd.conf

        也就是在/etc/httpd/conf.d此目錄下 建立mod_httpd.conf

定義虛擬主機

  1. NameVirtualHost *:80
  2. <Proxy balancer://tomsrvs>
  3. BalancerMember http://172.16.21.15:80 loadfactor=1
  4. BalancerMember http://172.16.21.16:80 loadfactor=1
  5. ProxySet lbmethod=byrequests
  6. </Proxy>
  7. <VirtualHost *:80>
  8. ServerName node17.stu21.com
  9. ProxyVia On
  10. ProxyRequests Off
  11. ProxyPreserveHost On
  12. <Proxy *>
  13. Order deny,allow
  14. Allow from all
  15. </Proxy>
  16. Proxypass /status !
  17. ProxyPass / balancer://tomsrvs/
  18. ProxyPassReverse / balancer://tomsrvs/
  19. <Location />
  20. Order deny,allow
  21. Allow from all
  22. </Location>
  23. </VirtualHost>

 

 

 

 

 

 

好了,你們能夠看到基於mod_proxy模塊的tomcat負載均衡配置完成。下面咱們來配置一下基於mod_jk的模塊的負載均衡!

 

 

 

#關閉httpd

2.#mod_jk負載均衡

#安裝httpd-devel

        [root@node17 ~]# yum intsall httpd-devel

#安裝tomcat-connectors

[root@node17 native]# pwd

/root/tomcat-connectors-1.2.40-src/native

[root@node17 native]# ./configure --with-apxs=`which apxs`

[root@node17 native]make && make install

 

 

/etc/httpd/conf.d此目錄下

編輯workers.properties

[root@node17 conf.d]# vim workers.properties

  1. worker.list=tomsrvs,statA
  2. worker.TomcatA.type=ajp13
  3. worker.TomcatA.host=172.16.21.15
  4. worker.TomcatA.port=8009
  5. worker.TomcatA.lbfactor=5
  6. worker.TomcatB.type=ajp13
  7. worker.TomcatB.host=172.16.21.16
  8. worker.TomcatB.port=8009
  9. worker.TomcatB.lbfactor=5
  10. worker.tomsrvs.type=lb
  11. worker.tomsrvs.sticky_session=0
  12. worker.tomsrvs.balance_workers=TomcatA,TomcatB
  13. worker.statA.type=status
  14. ~
  15.  

/etc/httpd/conf.d此目錄下

編輯mod_jk.conf

  1. LoadModule jk_module modules/mod_jk.so
  2. JkWorkersFile /etc/httpd/conf.d/workers.properties
  3. JkLogFile logs/mod_jk.log
  4. JkLogLevel debug
  5. JkMount /* tomsrvs
  6. JkMount /jkstatus/ statA

 

綁定session

編輯worker.propries,修改以下項

  1. worker.tomsrvs.sticky_session=1

此外還得把模塊mod_proxy 的配置文件mod_httpd.conf 改下

# mv mod_httpd.conf mod_httpd.conf.bak

分別編輯node1五、node16節點server.xml,分別修改以下項,第一行是節點1,第二行是節點2

  1. <Engine name="Catalina" defaultHost="node15.stu21.com" jvmRoute="tomcatA">
  2. <Engine name="Catalina" defaultHost="node16.stu21.com" jvmRoute="tomcatB">

 

上面建立的兩個文件配置設置好之後,接下來是基於 mod_jk模塊開啓後端2個節點的tomcat

 

 

訪問節點3 ,會話已經綁定

 

 

 

好了,到這裏咱們狀態信息查看就到這裏了,下面咱們來配置會話共享集羣。

 

 

 

 

 

.DeltaManager具體實現過程

(1).修改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.0.4" port="45564" frequency="500" dropTime="3000"/> 

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

address="192.168.18.201" 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>

以上內容定義在Engine容器中,則表示對全部主機均啓動用集羣功能。若是定義在某Host中,則表示僅對此主機啓用集羣功能。(注,節點node15與node16的tomcat的配置文件都要修改!)

以下是具體的配置:

 

 

 

 

 

 

節點node15:

[root@node15 ~]# vim /usr/local/tomcat/conf/server.xml

#在Engine組件內增長下面這一段

<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.110.100.10" port="45564" frequency="500" dropTime="3000"/> 

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

address="172.16.21.15" 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>

 

 

 

 

 

 

 

 

 

 

 

 

 

節點node16:

[root@node16 ~]# 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.110.100.10" port="45564" frequency="500" dropTime="3000"/> 

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

address="172.16.21.16" 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>

 

 

 

 

 

 

 

 

 

 

 

此外,全部啓用集羣功能的web應用程序,其web.xml中都須添加<distributable/>才能實現集羣功能。若是某web應用程序沒有本身的web.xml,也能夠經過複製默認的web.xml至其WEB-INF目錄中實現。

(2).修改web.xml

node15:

[root@node15 ~]# cd /webapps/ROOT/

[root@node15 ROOT]# ls

index.jsp 

[root@node15 ROOT]# mkdir WEB-INF 

[root@node15 ROOT]# # ls 

index.jsp WEB-INF 

[root@node15 ROOT]# # cp /usr/local/tomcat/conf/web.xml WEB-INF/

[root@node15 ~]# vim /usr/local/tomcat/conf/web.xml

 

#增長一行<distributable/>

 

node16:

[root@node16 ~]# cd /webapps/ROOT/

[root@node16 ROOT]# ls

index.jsp 

[root@node16 ROOT]# mkdir WEB-INF 

[root@node16 ROOT]# # ls 

index.jsp WEB-INF 

[root@node16 ROOT]# # cp /usr/local/tomcat/conf/web.xml WEB-INF/

[root@node16 ~]# vim /usr/local/tomcat/conf/web.xml

 

#增長一行<distributable/>

 

 

 

 

(3).啓動tomcat服務器

node15:

[root@node15 ~]# catalina.sh start

node16:

[root@node16 ~]# catalina.sh start

 

(4).查看一下tomcat集羣日誌

[root@node15 ~]#tail -f /usr/local/tomcat/logs/catalina.xxxx-xx-xx.log

 

 

最後我是基於mod_jk模塊實現負載均衡的apache(步驟前面已經實現)

 

 

 

Nginx實現Tomcat負載均衡:

 

 

安裝nginx

[root@node17 ~]# yum install -y nginx

[root@node17 ~]# vim /etc/nginx/conf.d/default.conf

配置nginx負載均衡

#

# The default server

#

upstream tomcat {

  server 192.168.18.201;

  server 192.168.18.202;

}

server {

  listen    80 default_server;

  server_name _;

  #charset koi8-r;

  #access_log logs/host.access.log main;

  location / {

    #root  /usr/share/nginx/html;

    #index index.html index.htm;

    proxy_pass http://tomcat;

  }

  error_page 404       /404.html;

  location = /404.html {

    root  /usr/share/nginx/html;

  }

  # redirect server error pages to the static page /50x.html

  #

  error_page  500 502 503 504 /50x.html;

  location = /50x.html {

    root  /usr/share/nginx/html;

  }

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80

  #

  #location ~ \.php$ {

  #  proxy_pass  http://127.0.0.1;

  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

  #

  #location ~ \.php$ {

  #  root      html;

  #  fastcgi_pass  127.0.0.1:9000;

  #  fastcgi_index index.php;

  #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

  #  include    fastcgi_params;

  #}

  # deny access to .htaccess files, if Apache's document root

  # concurs with nginx's one

  #

  #location ~ /\.ht {

  #  deny all;

  #}

}

 

檢查配置文件並啓動服務器

[root@node17 ~]# nginx –t

 

[root@node17 ~]# service nginx start

 

好了,到這裏Nginx實現tomcat的負載均衡與會話共享配置完成。

相關文章
相關標籤/搜索