如何使用嵌入式tomcat会话群集设置Spring Boot应用程序?

时间:2015-11-10 05:11:29

标签: spring tomcat spring-boot

我想设置一个带有嵌入式tomcat会话群集的Spring Boot应用程序。

由于嵌入式tomcat没有server.xml文件,因此我创建了一个TomcatEmbeddedServletContainerFactory并以编程方式设置了群集配置。代码如下:

@Configuration
public class TomcatConfig
{
    @Bean
    public EmbeddedServletContainerFactory servletContainerFactory()
    {
        return new TomcatEmbeddedServletContainerFactory()
        {
            @Override
            protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat)
            {
                configureCluster(tomcat);
                return super.getTomcatEmbeddedServletContainer(tomcat);
            }

            private void configureCluster(Tomcat tomcat)
            {
                // static membership cluster 

                SimpleTcpCluster cluster = new SimpleTcpCluster();
                cluster.setChannelStartOptions(3);
                {
                    DeltaManager manager = new DeltaManager();
                    manager.setNotifyListenersOnReplication(true);
                    cluster.setManagerTemplate(manager);
                }
                {
                    GroupChannel channel = new GroupChannel();
                    {
                        NioReceiver receiver = new NioReceiver();
                        receiver.setPort(localClusterMemberPort);
                        channel.setChannelReceiver(receiver);
                    }
                    {
                        ReplicationTransmitter sender = new ReplicationTransmitter();
                        sender.setTransport(new PooledParallelSender());
                        channel.setChannelSender(sender);
                    }
                    channel.addInterceptor(new TcpPingInterceptor());
                    channel.addInterceptor(new TcpFailureDetector());
                    channel.addInterceptor(new MessageDispatch15Interceptor());
                    {
                        StaticMembershipInterceptor membership =
                            new StaticMembershipInterceptor();
                        String[] memberSpecs = clusterMembers.split(",", -1);
                        for (String spec : memberSpecs)
                        {
                            ClusterMemberDesc memberDesc = new ClusterMemberDesc(spec);
                            StaticMember member = new StaticMember();
                            member.setHost(memberDesc.address);
                            member.setPort(memberDesc.port);
                            member.setDomain("MyWebAppDomain");
                            member.setUniqueId(memberDesc.uniqueId);
                            membership.addStaticMember(member);
                        }
                        channel.addInterceptor(membership);
                    }
                    cluster.setChannel(channel);
                }
                cluster.addValve(new ReplicationValve());
                cluster.addValve(new JvmRouteBinderValve());
                cluster.addClusterListener(new ClusterSessionListener());

                tomcat.getEngine().setCluster(cluster);
            }
        };
    }

    private static class ClusterMemberDesc
    {
        public String address;
        public int port;
        public String uniqueId;

        public ClusterMemberDesc(String spec) throws IllegalArgumentException
        {
            String[] values = spec.split(":", -1);
            if (values.length != 3)
                throw new IllegalArgumentException("clusterMembers element " +
                    "format must be address:port:uniqueIndex");
            address = values[0];
            port = Integer.parseInt(values[1]);
            int index = Integer.parseInt(values[2]);
            if ((index < 0) || (index > 255))
                throw new IllegalArgumentException("invalid unique index: must be >= 0 and < 256");
            uniqueId = "{";
            for (int i = 0; i < 16; i++, index++)
            {
                if (i != 0)
                    uniqueId += ',';
                uniqueId += index % 256;
            }
            uniqueId += '}';
        }
    };

    // This is for example. In fact these are read from application.properties
    private int localClusterMemberPort = 9991;
    private String clusterMembers = "111.222.333.444:9992:1";
}

我用以下环境测试了代码:

  • 单台Windows PC
  • 2具有不同localClusterMemberPort和clusterMembers的Spring启动应用程序实例

由于cookie不占用帐户中的端口,因此包含JSESSIONID的cookie在两个实例之间共享。

当实例启动时,tomcat集群似乎有效,因为2个实例的请求的JSESSIONID值是相同的。但是当我使用第一个实例登录后向第二个实例发出请求时,第二个实例找不到HttpSession。它记录了以下消息:

w.c.HttpSessionSecurityContextRepository : No HttpSession currently exists
w.c.HttpSessionSecurityContextRepository : No SecurityContext was available from the HttpSession: null. A new one will be created.

显然HttpSession没有被共享。但是当第二个实例创建一个新会话时,第一个实例的登录信息被清除,登录失效。

这里发生了什么?会话是共享但HttpSession不共享?

BTW,我已经读过必须在<distributed />上指定web.xml标记,以便应用程序使用tomcat会话群集。但我不知道如何使用Spring Boot的no-xml环境来指定它。这是问题的原因吗?那怎么指定呢?

我搜索过并找到了一些使用Redis显示聚类的文档。但目前我不想在我的配置中添加另一个移动部件。在我的配置中,3~4个节点是最大的。

2 个答案:

答案 0 :(得分:7)

关键是使上下文可分发,并设置管理器。

当我按如下方式修改问题代码时,会话群集工作正常。

@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;
    }

    ...
} 

对于Spring Boot 1.2.4,不需要context.setManager()。但是对于Spring Boot到1.3.0,如果没有调用context.setManager(),则集群失败并显示以下日志。

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.

我有点担心这个版本依赖。所以我opened an issue为此。

答案 1 :(得分:0)

在Spring Boot 2.0.x中,您需要使用WebServerFactoryCustomizer来配置集群。

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