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

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

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

  1. @Configuration
  2. @EnableRedisHttpSession
  3. public class RedisSessionConfig {
  4. }

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

  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服务器的位置了,在这里,我们就用本机:

  1. spring.redis.host=localhost
  2. spring.redis.port=6379

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

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

  1. server.port=8080

接下来定义一个Controller:

  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. @RequestMapping(value = "/sessions", method = RequestMethod.GET)
  12. public Object sessions (HttpServletRequest request){
  13. Map<String, Object> map = new HashMap<>();
  14. map.put("sessionId", request.getSession().getId());
  15. map.put("message", request.getSession().getAttribute("map"));
  16. return map;
  17. }
  18. }

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

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

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

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

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

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

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

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

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

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

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

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

springboot集成springsession利用redis来实现session共享的更多相关文章

  1. 使用SpringSession和Redis解决分布式Session共享问题

    SpringSession优势 遵循servlet规范,同样方式获取session,对应用代码无侵入且对于developers透明化 关键点在于做到透明和兼容 接口适配:仍然使用HttpServlet ...

  2. 基于Redis缓存的Session共享(附源码)

    基于Redis缓存的Session共享(附源码) 在上一篇文章中我们研究了Redis的安装及一些基本的缓存操作,今天我们就利用Redis缓存实现一个Session共享,基于.NET平台的Seesion ...

  3. 【Spring Session】和 Redis 结合实现 Session 共享

    [Spring Session]和 Redis 结合实现 Session 共享 参考官方文档 HttpSession with Redis Guide https://docs.spring.io/s ...

  4. .Net Core Web Api实践(三).net core+Redis+docker实现Session共享

    前言:上篇文章介绍了.net core+Redis+IIS+nginx实现Session共享,本来打算直接说明后续填坑过程,但毕竟好多坑是用docker部署后出现的,原计划简单提一下.net core ...

  5. springboot 集成spring-session redis 实现分布式session

    gradle 添加依赖 compile("org.springframework.session:spring-session:1.3.0.RELEASE") compile(&q ...

  6. Spring Boot(十一)Redis集成从Docker安装到分布式Session共享

    一.简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API,Redis也是技术领域使用最为广泛的存储中间件,它是 ...

  7. SpringBoot初始教程之Redis集中式Session管理

    1.介绍 有关Session的管理方式这里就不再进行讨论,目前无非就是三种单机Session(基于单机内存,无法部署多台机器).基于Cookie(安全性差).基于全局的统一Session管理(redi ...

  8. springboot+spring session+redis+nginx实现session共享和负载均衡

    环境 centos7. jdk1.8.nginx.redis.springboot 1.5.8.RELEASE session共享 添加spring session和redis依赖 <depen ...

  9. springboot+redis实现分布式session共享

    官方文档,它是spring session项目的redis相关的一个子文档:https://docs.spring.io/spring-session/docs/2.0.0.BUILD-SNAPSHO ...

随机推荐

  1. Python的设计模式

    设计模式是什么? 设计模式是经过总结.优化的,对我们经常会碰到的一些编程问题的可重用解决方案.一个设计模式并不像一个类或一个库那样能够直接作用于我们的代码.反之,设计模式更为高级,它是一种必须在特定情 ...

  2. git && gitlab 使用

    安装略过 使用 基于公钥的认证登录,方便对用户进行权限控制 useradd -s /usr/bin/git-shell testgit #创建一个用户 或者直接useradd testgit 然后去/ ...

  3. ZooKeeper-API CURD

    ZooKeeper Java API pom.xml 依赖 <?xml version="1.0" encoding="UTF-8"?> <p ...

  4. Angular记录(1)

    文档资料 箭头函数--MDN:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_fun ...

  5. 401AM 随笔~ 322~330 的总结

    web简介:html:超文本标记语言css:层叠样式表 或者css样式JavaScript:一门脚本语言前端:html css javascript<!---->:注释段落与文字p.b加粗 ...

  6. django项目实现中文检索

    在settings.py中设置 EMAIL_USE_SSL = True EMAIL_HOST = 'smtp.qq.com'  # 如果是 163 改成 smtp.163.com EMAIL_POR ...

  7. Linux找回root密码

    Linux忘记root密码时,密码重置步骤: 1.将虚拟机重启: 2.当进入GRUB倒计时界面,点击键盘e键: 3.点击键盘上下键选择第二行(kernel /vmlinuz-2.6.32-71.29. ...

  8. Lua中的一些库(1)

    [数学库] 数学库(math)由一组标准的数学函数构成.这里主要介绍几个常用的函数,其它的大家可以自行百度解决. 三角函数(sin,cos,tan……)所有的三角函数都使用弧度单位,可以用函数deg( ...

  9. 【原创】算法基础之Anaconda(1)简介、安装、使用

    Anaconda 2 官方:https://www.anaconda.com/ 一 简介 The Most Popular Python Data Science Platform Anaconda® ...

  10. 【原创】大叔经验分享(16)Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher

    今天尝试运行一个古老的工程,配置好之后编译通过,结果运行时报错: org.springframework.beans.factory.BeanDefinitionStoreException: Une ...