1、基于tomcat的session共享至redis:
***
2、共享springboot内置tomcat的session涉及配置:
实现 TomcatContextCustomizer 用于启动共享的redis 连接
public class Test implements TomcatContextCustomizer {
@Override
public void customize(Context context) {
RedisSessionManager redisSessionManager = new RedisSessionManager();
redisSessionManager.setContextPath("");
context.setManager(redisSessionManager);
}
}
配置bean
@Bean
public AbstractServletWebServerFactory createServletWebServerFactory(){
TomcatServletWebServerFactory factory=new TomcatServletWebServerFactory();
factory.addContextValves(new RedisSessionHandlerValve());
factory.addContextCustomizers(new Test());
return factory;
}
3、使用springboot 自身的redis 共享
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 28800, redisNamespace = "auto_test_platform:spring:session")
public class RedisSessionConfig {
/**
* spring session 共享专用 bean
* 名称必须是 springSessionRedisConnectionFactory
* @return
*/
@Bean(name = "springSessionRedisConnectionFactory")
public RedisConnectionFactory springSessionRedisConnectionFactory() {
log.info(">>>>>>>>>>>>>>>>>>>>> 注册 springSessionRedisConnectionFactory >>>>>>>>>>>>>>>>>>>>>>>>");
return new LettuceConnectionFactory();
}
}