微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用嵌入式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不共享?

顺便说一句,我读过< distributed />必须在web.xml上指定tag才能使应用程序使用tomcat会话群集.但我不知道如何使用Spring Boot的no-xml环境来指定它.这是问题的原因吗?然后如何指定?

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

最佳答案
关键是使上下文可分配,并设置管理器.

当我按如下方式修改问题的代码时,会话群集起作用.

@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为此.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐