tomcat集群

 

httpd负载均衡

httpd -M

确保有proxy_balancer_modeule模块

编辑httpd.conf

注释DocumentRoot

编辑conf.d/mod_httpd.conf

定义虚拟主机

两种方式都能实现负载均衡:

  • 基于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

分别编辑node15、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的负载均衡与会话共享配置完成。

 

posted on 2015-02-03 20:43  明远子  阅读(217)  评论(0编辑  收藏  举报