0

所以我用 2 个节点在 Heroku 上部署了我的 spring 应用程序,我注意到如果我在购物篮中添加一个产品并开始刷新购物车页面,有时我会看到该产品,有时该产品不存在。所以我在想,也许请求每次都会命中不同的节点。

在 spring-session 网站上,我可以看到This can make clustering much easier. This is nice because the clustering setup is done in a vendor neutral way. Furthermore, in some environments (i.e. PaaS solutions) developers cannot modify the cluster settings easily.但我找不到如何使用spring-session. 我的设置只是其中的一个:

@Bean(name = { "defaultRedisSessionRepository", "sessionRepository" })
@ConditionalOnProperty(name = { "nemesis.platform.redis.url" })
public SessionRepository defaultRedisSessionRepository(RedisTemplate<String,ExpiringSession> redisTemplate) throws Exception {
    return new RedisOperationsSessionRepository(redisTemplate);
}

然后在我的 MVC 配置中:

@Bean(name = {"defaultSessionFilter", "sessionFilter"})
public Filter sessionFilter() {
    CompositeFilter compositeFilter = new CompositeFilter();
    compositeFilter.setFilters(Arrays.asList(new SessionRepositoryFilter(applicationContext.getBean("sessionRepository", SessionRepository.class)), applicationContext.getBean(UrlEncoderFilter.NAME, UrlEncoderFilter.class)));

    return compositeFilter;
}

然后在我的WebApplicationInitializer

    final FilterRegistration sessionFilterChainReg = servletContext.addFilter("sessionFilter", DelegatingFilterProxy.class);
    sessionFilterChainReg.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR, DispatcherType.ASYNC, DispatcherType.FORWARD, DispatcherType.INCLUDE), false, dispatcherServletReg.getName());
4

1 回答 1

1

Spring Session 不会设置粘性会话。相反,它允许任何 JVM 通过将会话对象放置在中央数据存储(即 Redis)中来查找会话对象。

于 2014-12-03T17:11:44.933 回答