SpringBoot 如何实现Session共享
HttpSession,是通过Servlet容器创建并进行管理的,创建成功以后将会保存在内存中,这里将会使用Redis解决session共享的问题。
创建项目
添加pom
添加相关的maven
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.1.RELEASE com.example demo 0.0.1-SNAPSHOT demo DemoprojectforSpringBoot 1.8 org.springframework.boot spring-boot-starter-data-redis 2.3.1.RELEASE io.lettuce lettuce-core 6.0.0.M1 redis.clients jedis 3.3.0 org.springframework.session spring-session-data-redis 2.3.0.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-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用来执行测试操作
packagecom.example.demo;
importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.PostMapping;
importorg.springframework.web.bind.annotation.RestController;
importjavax.servlet.http.HttpSession;
@RestController
publicclassHelloController{
@PostMapping("/save")
publicStringsaveName(Stringname,HttpSessionsession){
session.setAttribute("name",name);
return"8080";
}
@GetMapping("/get")
publicStringgetName(HttpSessionhttpSession){
returnhttpSession.getAttribute("name").toString();
}
}
Nginx负载均衡
mingming@xiaoming-pc:~$sudoapt-getinstallnginx
修改配置文件
upstreamsang.com{
server192.168.0.1:8080weight=1;
server192.168.0.2:8080weight=1;
}
server{
listen80;
server_namelocalhost;
location/{
proxy_passhttp://sang.com;
proxy_redirectdefault;
}
}
请求分发
保存数据
获取数据
以上就是SpringBoot如何实现Session共享的详细内容,更多关于SpringBoot实现Session共享的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。