zoukankan      html  css  js  c++  java
  • springboot+redis实现分布式session共享

     

     

    官方文档,它是spring session项目的redis相关的一个子文档:https://docs.spring.io/spring-session/docs/2.0.0.BUILD-SNAPSHOT/reference/html5/guides/boot-redis.html

    在spring boot的文档中,告诉我们添加@EnableRedisHttpSession来开启spring session支持,配置如下:

    @Configuration  
    @EnableRedisHttpSession  
    public class RedisSessionConfig {  
    } 

    而@EnableRedisHttpSession这个注解是由spring-session-data-redis提供的,所以在pom.xml文件中添加:

    复制代码
    <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-redis</artifactId>  
    </dependency>  
    <dependency>  
            <groupId>org.springframework.session</groupId>  
            <artifactId>spring-session-data-redis</artifactId>  
    </dependency>  
    复制代码

    在配置文件application.properties里配置spring session

    spring.session.store-type=redis #指定redis实现spring session
    server.session.timeout=600 # Session 过期时间,单位s
    spring.session.redis.flush-mode= # Sessions 刷新模式
    spring.session.redis.namespace= # Namespace for keys used to store sessions.

    配置redis连接信息

    spring.redis.host=localhost
    spring.redis.password=secret
    spring.redis.port=6379

    加上端口号

    server.port=8080

    定义一个Controller

    复制代码
    @RestController  
    @RequestMapping(value = "/admin/v1")  
    public class QuickRun {  
        @RequestMapping(value = "/first", method = RequestMethod.GET)  
        public Map<String, Object> firstResp (HttpServletRequest request){  
            Map<String, Object> map = new HashMap<>();  
            request.getSession().setAttribute("request Url", request.getRequestURL());  
            map.put("request Url", request.getRequestURL());  
            return map;  
        }  
      
        @RequestMapping(value = "/sessions", method = RequestMethod.GET)  
        public Object sessions (HttpServletRequest request){  
            Map<String, Object> map = new HashMap<>();  
            map.put("sessionId", request.getSession().getId());  
            map.put("message", request.getSession().getAttribute("map"));  
            return map;  
        }  
    }  
    复制代码

    复制上面的工程,把port改为9090

    两个项目都启动好

    首先访问8080端口的设置session

    {"request Url":"http://localhost:8080/admin/v1/first"}  

    接着,我们访问8080端口的sessions,返回:

    {"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:8080/admin/v1/first"} 

    最后,再访问9090端口的sessions,返回:

    {"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:8080/admin/v1/first"} 

    可见,8080与9090两个服务器返回结果一样,实现了session的共享

    如果此时再访问9090端口的first的话,首先返回:

    {"request Url":"http://localhost:9090/admin/v1/first"}  

    而两个服务器的sessions都是返回:

    {"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:9090/admin/v1/first"} 

    这个时候打开redis客户端,可以查询到session信息已经保存在redis里。

    注意点:

    1.Redis版本要在2.8+

  • 相关阅读:
    经典算法排序——希尔排序
    查找算法总结
    Anddroid HttpService
    经典排序算法——直接选择排序
    [置顶] 获取Android手机上音乐播放器状态
    经典排序算法——归并排序
    Android锁屏未读短信,未接电话
    经典排序算法——冒泡排序
    经典排序算法——快速排序
    linux下同时存在当静态库和动态库同名
  • 原文地址:https://www.cnblogs.com/heyy520/p/9899279.html
Copyright © 2011-2022 走看看