zoukankan      html  css  js  c++  java
  • springboot集成springsession利用redis来实现session共享

    转:https://www.cnblogs.com/mengmeng89012/p/5519698.html

    这次带来的是spring boot + redis 实现session共享的教程。

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

    Java代码  收藏代码
    1. @Configuration  
    2. @EnableRedisHttpSession  
    3. public class RedisSessionConfig {  
    4. }  

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

    Java代码  收藏代码
    1. <dependency>  
    2.         <groupId>org.springframework.boot</groupId>  
    3.         <artifactId>spring-boot-starter-redis</artifactId>  
    4. </dependency>  
    5. <dependency>  
    6.         <groupId>org.springframework.session</groupId>  
    7.         <artifactId>spring-session-data-redis</artifactId>  
    8. </dependency>  

    接下来,则需要在application.properties中配置redis服务器的位置了,在这里,我们就用本机:

    Java代码  收藏代码
    1. spring.redis.host=localhost  
    2. spring.redis.port=6379  

    这样以来,最简单的spring boot + redis实现session共享就完成了,下面进行下测试。

    首先我们开启两个tomcat服务,端口分别为8080和9090,在application.properties中进行设置【下载地址】   :

    Java代码  收藏代码
    1. server.port=8080  

    接下来定义一个Controller: 

    Java代码  收藏代码
    1. @RestController  
    2. @RequestMapping(value = "/admin/v1")  
    3. public class QuickRun {  
    4.     @RequestMapping(value = "/first", method = RequestMethod.GET)  
    5.     public Map<String, Object> firstResp (HttpServletRequest request){  
    6.         Map<String, Object> map = new HashMap<>();  
    7.         request.getSession().setAttribute("request Url", request.getRequestURL());  
    8.         map.put("request Url", request.getRequestURL());  
    9.         return map;  
    10.     }  
    11.   
    12.     @RequestMapping(value = "/sessions", method = RequestMethod.GET)  
    13.     public Object sessions (HttpServletRequest request){  
    14.         Map<String, Object> map = new HashMap<>();  
    15.         map.put("sessionId", request.getSession().getId());  
    16.         map.put("message", request.getSession().getAttribute("map"));  
    17.         return map;  
    18.     }  
    19. }  

    启动之后进行访问测试,首先访问8080端口的tomcat,返回 获取【下载地址】   :

    Java代码  收藏代码
    1. {"request Url":"http://localhost:8080/admin/v1/first"}  

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

    Java代码  收藏代码
    1. {"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:8080/admin/v1/first"}  

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

    Java代码  收藏代码
    1. {"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:8080/admin/v1/first"}  

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

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

    Java代码  收藏代码
    1. {"request Url":"http://localhost:9090/admin/v1/first"}  

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

    Java代码  收藏代码
    1. {"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:9090/admin/v1/first"}  

    通过spring boot + redis来实现session的共享非常简单,而且用处也极大,配合nginx进行负载均衡,便能实现分布式的应用了。

  • 相关阅读:
    C# Bitmap类型与Byte[]类型相互转化
    博客园添加个人Github链接
    C# Exception has been thrown by the target of an invocation(调用的目标已抛出异常) 解决办法
    C# 使用Renci.SshNet连接SSH远程服务器
    Oracle 查询当前数据库版本信息
    Oracle ORA-12569: TNS:包校验和失败
    Oracle Rollup()函数
    Oracle 字符串补零
    DataGridView 表格排序后颜色丢失
    DataGridView 实现最后一列的宽度自适应
  • 原文地址:https://www.cnblogs.com/silentdoer/p/8999569.html
Copyright © 2011-2022 走看看