首页 > 代码库 > 集群下使用redis统一session处理
集群下使用redis统一session处理
pom依赖(快照版):
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> </dependency> <dependency> <groupId>biz.paluch.redis</groupId> <artifactId>lettuce</artifactId> <version>5.0.0.Beta1</version> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-core</artifactId> <version>3.0.6.RELEASE</version> </dependency>
RedisSession.java
@EnableRedisHttpSession @EnableConfigurationProperties(RedisProperties.class) public class RedisConfiguration { @Resource private RedisProperties redisProperties; @Bean public LettuceConnectionFactory connectionFactory() { LettuceConnectionFactory factory = new LettuceConnectionFactory(); factory.setHostName("localhost"); factory.setPassword("redispassword"); factory.setDatabase("0"); factory.setTimeout(5000); factory.setPort(6379); return factory; } }
然后在把值放入到HttpSession里面就会被自动放入到redis里面了。在集群下自动达到共享session的功能。
如:
public ResponseEntity<Object> browseCunAction(@RequestParam("key")String key,@RequestParam("value")String value,HttpSession session) throws Exception { session.setAttribute(key, value); return ResponseEntity.ok(Collections.singletonMap(key, value)); }
集群下使用redis统一session处理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。