• 集群下使用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>
    

     pom.xml正式版:

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

    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;

    //依赖包不要导入错。
    @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));
    	}
    

      

  • 相关阅读:
    变Win 2003为工作站
    搞笑短信
    ECC加密算法入门介绍
    搞笑短信
    万能Ghost系统制作教程
    “注册编辑已被管理员停用”之解决办法
    让Win7任务栏的“库”显示“我的电脑”
    全景拼图利器PTGui Pro 9.1.6 Retail汉化版
    Win7远程桌面不能连接的解决方案
    到底谁霸占了A类的IP地址
  • 原文地址:https://www.cnblogs.com/tietazhan/p/6728070.html
Copyright © 2020-2023  润新知