Nginx负载均衡+tomcat简单集群(DeltaManager自带共享session)

部署tomcat集群,要解决的一个但问题就是session共享问题。

本实验采用tomcat自带的cluster集群方案。
此方案优点就是部署简单,只需要常用的nginx,没有额外依赖。
缺点是session在集群中是通过组播复制的方式,即每个tomcat都保存了整个集群的session状态,如果访问量大了,会给服务带来额外的开销的负担,也给交换网络带来一定压力。

首先,万事以官方文档为基准。

http://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html

准备工作

1、tomcat安装

​ 安装2个tomcat,为了简单又真实,我在两台虚拟机上分别安装。
脚本安装tomcat:https://blog.csdn.net/zld_555/article/details/103958344

2、nginx安装

最终实验环境如下:

虚拟机Centos7X4Centos7X5
IP172.16.192.104172.16.192.105
Nginx80
Tomcat80808080
3、一个webapp项目,方便查看效果。

​ 不用也可以,在浏览器里直接看。
可以直接下载我打的包,简单的war包:https://download.csdn.net/download/zld_555/12101613

​ 项目只有这一个页面,index.jsp:

<%-- Created by IntelliJ IDEA. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <base href="<%=basePath%>">

  <title>My JSP 'index.jsp' starting page</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->
</head>

<body>
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<BR>
<%
  out.println("This is Tomcat Server Centos7X4");
%>
</body>
</html>

另外,web.xml里要求必须有属性,字面意思就是申明我是个可分布式部署的项目。

官方说明:

  • Make sure your web.xml has the <distributable/> element
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                  http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
           version="4.0">
    <display-name></display-name>
    <distributable/>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

把项目打包发布到tomcat/webapps下,项目正常运行。

第一台tomcat

第二台tomcat

4、一致的时间

​ Make sure that all nodes have the same time and sync with NTP service!

​ 使用ntp来同步下时间,我这里使用centos7自带的chrony

5、关防火墙,或者放行端口

开始做正事

1、开启tomcat集群

​ 修改tomcat/conf/server.xml 文件

​ 在<Engine> 或者<Host>块里增加如下内容。

​ 我是在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.0.0.4"
                        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.MessageDispatchInterceptor"/>
          </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>

其中:

​ address=“228.0.0.4” 是共享session的组播地址,使用同一个组播地址的tomcat组成一个集群。

如果两个tomcat监听的地址不一样,就会成为两个独立的集群。

​ Receiver 的port=4000 如果要自定义的话,保持在4000-4100,这是tomcat自动侦测的范围。

修改完了之后,不要忘记重启。

2、开启nginx的负载均衡

vim /usr/local/nginx/conf/nginx.conf

在http模块下增加upstream组,组名称是tmcluster,这里的名字一定不要用"_"下划线。

    upstream tmcluster {
        server 172.16.192.104:8080;
        server 172.16.192.105:8080;
    }

在server下,增加代理地址,指向到upstream组

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://tmcluster;
        }

这里采用了最简单的轮寻方式,其他参数说明如下,留作记录:

//举例
// upstream test{ 
//      server 11.22.333.11:6666 weight=1; 
//      server 11.22.333.22:8888 down; 
//      server 11.22.333.33:8888 backup;
//      server 11.22.333.44:5555 weight=2 max_fails=2 fail_timeout=20; 
//}
//down 表示单前的server临时不參与负载.
//weight 默认1。weight越大,负载的权重就越大
//backup: 其他全部的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻
//max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
//fail_timeout,在经历了max_fails次失败后,暂停服务的时间。max_fails可以和fail_timeout一起使用。

最后,重启nginx。

[root@localhost nginx]# nginx/sbin/nginx -s reload
3、验证效果

使用80端口访问http://172.16.192.104/cluster_test/
刷新浏览器,可以看到在两个tomcat上切换,session没有发生变化。
在这里插入图片描述
在这里插入图片描述

在Centos7X4上关闭tomcat。

[root@localhost ~]# /usr/local/tomcat/bin/shutdown.sh 
[root@localhost ~]# jps
26944 Jps

继续刷新浏览器,只访问Centos7X5,没有中断服务,sessionID不变,简单集群效果达成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值