How to setup a Spring Boot application with embedded tomcat session clustering?

前端 未结 2 1167
[愿得一人]
[愿得一人] 2020-12-09 06:27

I want to setup a Spring Boot application with embedded tomcat session clustering.

Since embedded tomcat does not have a server.xml file, I\'ve created

相关标签:
2条回答
  • 2020-12-09 07:15

    In Spring Boot 2.0.x, you need to use a WebServerFactoryCustomizer to configure the clustering.

    @Component
    public class WebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
        @Override
        public void customize( final TomcatServletWebServerFactory factory ) {
            factory.addContextCustomizers( new TomcatClusterContextCustomizer() );
        }
    }
    
    public class TomcatClusterContextCustomizer implements TomcatContextCustomizer {
        @Override
        public void customize( final Context context ) {
            // Call method defined in the question text above, but pass Engine 
            // instead of Tomcat
            configureCluster( (Engine)context.getParent().getParent() );
        }
    }
    
    0 讨论(0)
  • 2020-12-09 07:16

    The key was to make the context distributable, and setting manager.

    When I modified the code of the question as follows, the session clustering worked.

    @Configuration
    public class TomcatConfig
    {
        @Bean
        public EmbeddedServletContainerFactory servletContainerFactory()
        {
            TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory()
            {
                ...
            };
    
            factory.addContextCustomizers(new TomcatContextCustomizer()
            {
                @Override
                public void customize(Context context)
                {
                    context.setManager(new DeltaManager());
                    context.setDistributable(true);
                }
            });
    
            return factory;
        }
    
        ...
    } 
    

    For Spring Boot 1.2.4, context.setManager() is not needed. But for Spring Boot to 1.3.0, if context.setManager() is not called, clustering fails and the following log is shown.

    2015-11-18 19:59:42.882  WARN 9764 --- [ost-startStop-1] o.a.catalina.ha.tcp.SimpleTcpCluster     : Manager [org.apache.catalina.session.StandardManager[]] does not implement ClusterManager, addition to cluster has been aborted.
    

    I am somewhat worried about this version dependency. So I opened an issue for this.

    0 讨论(0)
提交回复
热议问题