集羣下使用redis統一session處理

pom依賴(快照版):html

          <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>

 pom.xml正式版:java

	
		<dependency>
		    <groupId>biz.paluch.redis</groupId>
		    <artifactId>lettuce</artifactId>
		    <version>4.2.2.Final</version>
		</dependency>
		<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>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-web</artifactId>
		</dependency>

  

 RedisSession.javareact

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.context.annotation.Bean;web

//依賴包不要導入錯。
@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的功能。redis

如:spring

  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));
	}
相關文章
相關標籤/搜索