所以我在Heroku上部署了我的Spring应用程序,有2个节点,我注意到如果我在购物篮中添加产品并开始刷新购物车页面,有时我会看到产品,有时产品不在那里。所以我想也许每次请求都会遇到不同的节点。
在春季会话网站上,我可以看到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());
答案 0 :(得分:1)
Spring Session不会设置粘性会话。相反,它允许任何JVM通过将会话对象放在中央数据存储区(即Redis)中来查找会话对象。