【问题标题】:Tomcat clustering mechanism and session replication is not working on tomcat 8.5.78Tomcat 集群机制和会话复制在 Tomcat 8.5.78 上不起作用
【发布时间】:2022-06-20 19:08:04
【问题描述】:

我正在做一个项目,我需要在两个不同的 tomcat 上复制会话。我将 Nginx 配置为负载均衡器。配置两个tomcat做两个不同的节点。

我的要求:假设有两个节点在运行,节点一的会话复制到节点二。最终用户正在从节点一获得响应。如果节点 1 关闭,则用户将无法退出会话并继续使用相同会话的节点 2。

现在发生了什么:两个节点正在运行,会话被复制到两个节点。用户从节点一获得响应。节点一关闭,用户收到会话退出消息。

我从这个site 配置了tomcat 集群机制,就像tomcat 文档一样 我配置了tomcat 1的server.xml文件,比如

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="6">

          <Manager className="org.apache.catalina.ha.session.BackupManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"
                   mapSendOptions="6"/>
          <!--
          <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="5000"
                      selectorTimeout="100"
                      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"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
          </Channel>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/>

          <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 2的server.xml文件一样

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="6">

          <Manager className="org.apache.catalina.ha.session.BackupManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"
                   mapSendOptions="6"/>
          <!--
          <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="5001"
                      selectorTimeout="100"
                      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"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
          </Channel>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/>

          <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>

我的spring项目的web.xml文件是

<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_3_0.xsd"
         version="3.0">

  <display-name>Enterprise Web App</display-name>



    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name >springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
    <!--start real ip filter for ngnix-->
    <filter>
        <filter-name>RealIPFilter</filter-name>
        <filter-class>com.brainstation.corporate.ib.webapp.provider.ip.RealIPFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>RealIPFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--end real ip filter for ngnix-->

    <servlet>
        <servlet-name>web-app</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>web-app</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>dev</param-value>
    </context-param>

    <session-config>
        <session-timeout>0</session-timeout>
        <cookie-config>
            <name>EB_JSESSIONID</name>
            <http-only>true</http-only>
        </cookie-config>
        <tracking-mode>COOKIE</tracking-mode>
    </session-config>

    <distributable/>

</web-app>

还有我在 /etc/nginx/sites-available/default 文件上的 nginx 配置

    upstream backend {
            ip_hash;
            server localhost:7070 max_fails=3 fail_timeout=10s;
            server localhost:7080 max_fails=3 fail_timeout=10s;
            #server localhost:7090 max_fails=3 fail_timeout=10s;
    }
    
    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
    
    
        #server_name hellobs23.com
        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;
    
    
    
                root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;


        location / {
                proxy_pass http://backend;
                error_page 500 502 503 504 /50x.html;
      }


        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

还有一个有趣的事情是相同的配置在另一个项目上工作,但在我的项目上没有工作。我一直在拼命寻找解决方案 4 天。找不到任何可以让我解脱的东西。

【问题讨论】:

  • 还有什么不起作用?你得到错误,没有复制或什么?
  • @M.Deinum 我已经编辑了我的问题。你能检查一下吗?
  • 你是否正确配置了nginx,即它随机进入服务器1或2并且没有配置会话亲和性或类似的东西。
  • @M.Deinum 我已经添加了 Nginx 配置。使用相同的配置,另一个项目运行良好。但我的一个不工作。
  • 我不太擅长 Nginx,所以我无法对此发表评论。但配置适用于 X 的事实并不意味着它适用于 Y。如果存在版本差异、网络差异等,事情可能(并且将会)表现不同。

标签: java tomcat session-replication


【解决方案1】:

在继续之前需要检查几件事:

  1. 您是否在 Engine 中设置了 jvmRoute?
  2. 如果您将备份管理器更改为增量管理器会怎样?
  3. 如果将多播更改为静态成员资格会怎样?
  4. 您使用的是 POST 请求吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 2018-03-18
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多