Nginx反向代理 + Tomcat集群(session复制)

1 篇文章 0 订阅
1 篇文章 0 订阅

                         Nginx反向代理 + Tomcat集群(session复制)

1.nginx安装先到官网下载需要的稳定版本的nginx版本,然后在编译安装。

   1.1 具体操作可参考:https://www.cnblogs.com/wyd168/p/6636529.html

   1.2 nginx反向代理配置

   在nginx/conf中修改nginx.conf。在server中添加代理配置:proxy_pass http://web; 告知nginx在客户端访问服务器时,如果请求路径为"/"根目录,则代理访问upstream web模块。而upstream web又是一个集群。配置了6台集群的tomcat服务。(ip_hash,这种方案是客户端每次访问服务端,nginx都根据客户端的ip而代理到一台固定的服务器上。)

2.Java的运行环境。

具体可参考:https://www.cnblogs.com/zeze/p/5902124.html

3.Tomcat下载安装。此处已最新版本9.0为介绍。

    3.1 下载Tomcat

     请访问https://tomcat.apache.org/download-90.cgi,下载对应操作系统的版本。我这里下载的是*.tar.gz格式的。

linux上下载可使用wget命令。参考下图。

3.2 解压Tomcat

    在服务器上执行 tar zxvf xxx.tar.gz解压tomcat。

3.3 重命名[个人习惯]

    将解压的文件apache-tomcat-9.0.19重命名为tomcat-9.0.19-8080。其中8080为http请求端口号。

mv apache-tomcat-9.0.19 tomcat-9.0.19-8080

3.4 复制多个tomcat

    使用cp命令复制出多个tomcat用于部署集群。我这里复制了6个,模拟的话复制2-3个就行。

cp -r tomcat-9.0.19-8080 tomcat-9.0.19-8081

3.5 修改端口号

修复复制的tomcat的端口号,避免tomcat启动时是因端口冲突导致启动失败。tomcat修改端口在conf/server.xml中,修改的端口号有:监听关闭tomcat服务的端口、监听AJP请求的端口、监听HTTP请求的端口。具体如下图:

注意:配置文件中的注释信息因排版不下是删除了的,以更容易看清结构

<?xml version="1.0" encoding="UTF-8"?>
<Server port="8004" shutdown="SHUTDOWN">

  <Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
  <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
  <Listener className="org.apache.catalina.core.JasperListener"/>
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>

  <GlobalNamingResources>
    <Resource auth="Container" description="User database that can be updated and saved" 
factory="org.apache.catalina.users.MemoryUserDatabaseFactory" 
name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
  </GlobalNamingResources>

  <Service name="Catalina">
    <Connector connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8443"/>
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8443"/>
    <Engine defaultHost="localhost" name="Catalina">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>
      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 
pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log." suffix=".txt"/>
      </Host>
    </Engine>
  </Service>
  
</Server>

3.6 tomcat配置session集群

在tomcat下面的conf/server.xml配置文件中添加<Cluster>节点。配置信息可参考见tomcat的webapp下面的/docs/cluster-howto.html文件。下面一段直接Copy其中内容。

<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"/>
            <!-- 启动出现异常,则可以尝试把address中的"auto"改为"localhost"。如果部署的tomcat在相同的主机上面请将port修改为不冲突情况。官方推荐使用4000-4100 之一-->
            <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>

因部署的是多个tomcat,其中需要修改的是<Membership>和<Receiver>标签中的内容。同时在启动tomcat容器前,确保需要发布的web服务中web.xml有<distributable/>属性配置。

下面官方介绍:

Cluster Basics

To run session replication in your Tomcat 9 container, the following steps should be completed:

  • All your session attributes must implement java.io.Serializable //请保证session中的属性对象都是实现了序列化接口的
  • Uncomment the Cluster element in server.xml
  • If you have defined custom cluster valves, make sure you have the ReplicationValve defined as well under the Cluster element in server.xml
  • If your Tomcat instances are running on the same machine, make sure the Receiver.port attribute is unique for each instance, in most cases Tomcat is smart enough to resolve this on it's own by autodetecting available ports in the range 4000-4100 // 部署在同一台主机上,保证Receiver.port属性是惟一的,推荐在4000-4100上面选一个
  • Make sure your web.xml has the <distributable/> element  // 部署的web应用,确保在web.xml上有 <distributable/>标签元素,告诉tomcat容器,这个应用是分布式的
  • If you are using mod_jk, make sure that jvmRoute attribute is set at your Engine <Engine name="Catalina" jvmRoute="node01" > and that the jvmRoute attribute value matches your worker name in workers.properties // 如果使用了mod_jk模块,请确保tomcat的engine上面的jvmRoute属性是是一样的。并且确保在mod_jk模块的workers.properties文件中的worker那么和配置的jvmRoute属性值一样。一般使用Apache做反向代理的时候才会结合mod_jk模块
  • Make sure that all nodes have the same time and sync with NTP service!
  • Make sure that your loadbalancer is configured for sticky session mode.

4 部署展示

在6台tomcat服务器上都部署了一个叫web的应用。因是演示,只是用servlet,三个页面。

第一次访问:

第一次可以看到访问的是8081的服务器,sessionId是 1B88521CE6A0F4077DD76B27726BDE14

第二步登陆后(因仅仅是集群演示,没有做相关安全处理,登陆后标识会话未更新):

第二次登陆访问的是8080的服务器,sessionId是 1B88521CE6A0F4077DD76B27726BDE14。和第一次访问的session是一样的。

 

第三次访问:

访问到了8081这台服务器(第三次不一定访问到8081,有可能是其他或者还是前一次访问到的8080服务器)。查看sessionId还是1B88521CE6A0F4077DD76B27726BDE14。至此,nginx+tomcat(session复制方式)集群部署演示完毕。

 

代码地址:https://gitee.com/qsding/web.git

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值