微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringBoot 如何实现Session共享

这篇文章主要介绍了SpringBoot 如何实现Session共享,帮助大家更好的理解和学习spring boot框架,感兴趣的朋友可以了解下

HttpSession,是通过Servlet容器创建并进行管理的,创建成功以后将会保存在内存中,这里将会使用Redis解决session共享的问题。

创建项目

添加pom

添加相关的maven

4.0.0org.springframework.bootspring-boot-starter-parent2.3.1.RELEASEcom.exampledemo0.0.1-SNAPSHOTdemoDemo project for Spring Boot1.8org.springframework.bootspring-boot-starter-data-redis2.3.1.RELEASEio.lettucelettuce-core6.0.0.M1redis.clientsjedis3.3.0org.springframework.sessionspring-session-data-redis2.3.0.RELEASEOrg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.junit.vintagejunit-vintage-engineorg.springframework.bootspring-boot-maven-plugin

配置redis连接

配置redis连接spring: redis: database: 0 host: 106.53.115.12 port: 6379 password: 12345678 jedis: pool: max-active: 8 max-idle: 8 max-wait: -1ms min-idle: 0创建Controller用来执行测试操作package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpSession; @RestController public class HelloController { @PostMapping("/save") public String saveName(String name, HttpSession session){ session.setAttribute("name", name); return "8080"; } @GetMapping("/get") public String getName(HttpSession httpSession){ return httpSession.getAttribute("name").toString(); } }Nginx 负载均衡mingming@xiaoming-pc:~$ sudo apt-get install Nginx修改配置文件upstream sang.com { server 192.168.0.1:8080 weight = 1; server 192.168.0.2:8080 weight = 1; } server { listen 80; server_name localhost; location / { proxy_pass http://sang.com; proxy_redirect default; } }请求分发保存数据

获取数据

以上就是SpringBoot 如何实现Session共享的详细内容,更多关于SpringBoot 实现Session共享的资料请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐