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
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() );
}
}
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.