日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

分布式Session解决方案_Spring Session + Redis

發布時間:2024/9/27 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 分布式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:5
2. 引入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=admin
5. 調用8081獲取用戶信息接口
#獲取用戶信息接口 http://localhost:8081/user/info
6. 調用8082獲取用戶信息接口
#獲取用戶信息接口 http://localhost:8082/user/info

測試結論:可以正常獲取用戶信息,關閉8081服務器,也是一樣的,因為用戶信息存放在redis服務端,不在應用本地,除非把redis關閉。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的分布式Session解决方案_Spring Session + Redis的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。