當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
分布式Session解决方案_Spring Session + Redis
生活随笔
收集整理的這篇文章主要介紹了
分布式Session解决方案_Spring Session + Redis
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
接上一篇:分布式6大核心專題_分布式Session
https://gblfy.blog.csdn.net/article/details/113802195
文章目錄
- 1. 安裝redis服務并啟動
- 2. 引入Spring Session組件
- 3. 分別啟動8081端口和8082端口
- 4. 調用8081登錄接口
- 5. 調用8081獲取用戶信息接口
- 6. 調用8082獲取用戶信息接口
實現流程:
1.安裝redis服務并啟動
2.引入Spring Session組件
3.演示同一程序啟動8081端口和8082端口模擬2個服務器分布式
4.調用8081登錄接口
5.調用8081獲取用戶信息接口
6.調用8082獲取用戶信息接口
1. 安裝redis服務并啟動
docker run -d -p 6379:6379 redis:52. 引入Spring Session組件
存放查信息的容器有redis、jdbc、mongodb等,演示redis容器
pom
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId></dependency>application.yml
spring:redis:host: 192.168.0.113 #ip地址port: 6379 #端口password: #密碼session:store-type: redis #存放的容器timeout: 3600 #過期時間 server:port: 8081 #應用web端口案例代碼
package com.gblfy.distributedsession.controller;import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpSession;@RequestMapping("/user") @RestController public class UserController {@GetMapping("/login")public String login(@RequestParam String username,@RequestParam String password,HttpSession session){//賬號密碼正確session.setAttribute("login_user", username);return "登錄成功";}@GetMapping("/info")public String info(HttpSession session) {return "當前登錄的是:" + session.getAttribute("login_user");} }3. 分別啟動8081端口和8082端口
同上一篇
4. 調用8081登錄接口
#登錄接口 http://localhost:8081/user/login?username=admin&password=admin5. 調用8081獲取用戶信息接口
#獲取用戶信息接口 http://localhost:8081/user/info6. 調用8082獲取用戶信息接口
#獲取用戶信息接口 http://localhost:8082/user/info測試結論:可以正常獲取用戶信息,關閉8081服務器,也是一樣的,因為用戶信息存放在redis服務端,不在應用本地,除非把redis關閉。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的分布式Session解决方案_Spring Session + Redis的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sonar扫描脚本
- 下一篇: 安装和使用Ant Design Vue