SpringBoot2.x 整合Spring-Session實(shí)現(xiàn)Session共享功能
1.前言
發(fā)展至今,已經(jīng)很少還存在單服務(wù)的應(yīng)用架構(gòu),不說都使用分布式架構(gòu)部署, 至少也是多點(diǎn)高可用服務(wù)。在多個服務(wù)器的情況下,Seession共享就是必須面對的問題了。
解決Session共享問題,大多數(shù)人的思路都是比較清晰的, 將需要共享的數(shù)據(jù)存在某個公共的服務(wù)中,如緩存。很多人都采用的Redis,手動將Session存在Redis,需要使用時,再從Redsi中讀取數(shù)據(jù)。毫無疑問,這種方案是可行的,只是在手動操作的工作量確實(shí)不少。
LZ在這里采用的Spring-Session來實(shí)現(xiàn)。它使用代理過濾器,將Session操作攔截,自動將數(shù)據(jù)同步到Redis中,以及自動從Redis讀取數(shù)據(jù)。從此,操作分布式的Session就像操作單服務(wù)的Session一樣,可以為所欲為了。
2.實(shí)踐
2.1 創(chuàng)建工程
使用idea創(chuàng)建SpringBoot工程, 添加組件Web、Spring Session和Redis。 我這里idea是2019版本,SpringBoot是2.1.6。

pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.2 配置Redis
spring: redis: port: 6379 password: xofcO46Fy host: 10.17.153.104 server: port: 9090
2.3 測試
代碼實(shí)現(xiàn)
package com.xiaoqiang.sessionshare.web;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
/**
* SessionShareController <br>
* 〈session共享控制器〉
*
* @author XiaoQiang
* @create 2019-7-6
* @since 1.0.0
*/
@RestController
@RequestMapping(value = "/session")
public class SessionShareController {
@Value("${server.port}")
Integer port;
@GetMapping(value = "/set")
public String set(HttpSession session){
session.setAttribute("user","wangwq8");
return String.valueOf(port);
}
@GetMapping(value = "get")
public String get(HttpSession session){
return "用戶:"+session.getAttribute("user")+",端口:"+port;
}
}
maven package打包發(fā)布到服務(wù)器服務(wù)器,過程略。
分別使用9090 9091端口啟動項(xiàng)目。
nohup java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=9090 &
nohup java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=9091 &
先訪問http://10.17.158.136:9090/session/set,在9090這個服務(wù)的session保存用戶變量;

然后再訪問http://10.17.158.136:9091/session/get,從session中獲取得到用戶信息。

從上面樣例,可以看出session已經(jīng)實(shí)現(xiàn)了共享,只是測試過程是需要手動切換服務(wù)。為了更好地模式真實(shí)項(xiàng)目環(huán)境,為此,我們配置Nginx,來進(jìn)行測試。
2.4 配置Nginx
在Nginx安裝目錄conf下,編輯nginx.conf,
upstream tomcatServer {
server 10.17.158.136:9092 weight=1;
server 10.17.158.136:9091 weight=2;
}
server {
listen 9000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatServer;
proxy_redirect default;
#root html;
#index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
在這里我們只需要配置簡單的負(fù)載均衡,端口是9000。所有l(wèi)ocalhost:9000都會按一定策略(這里是按權(quán)重分發(fā),配置weight=1一樣,隨機(jī)分發(fā)的;nginx默認(rèn)是輪詢策略)分發(fā)到上游服務(wù)upstream配置的服務(wù)上。
配置完成后,啟動Nginx;
/apps/test/software/nginx/nginx-1.6.2/sbin/nginx
首先訪問http://10.17.158.136:9000/session/set,向seesion中保存數(shù)據(jù),從下圖中可知9090端口的服務(wù)處理了該請求。

然后在訪問/get請求,是從9091端口的服務(wù)獲取得到的用戶信息,至此,測試完成。

3.總結(jié)
本文主要是Spring Session的簡單使用,從上面可以看出,除了引入了Spring Session的jar, 其他方面,不管是代碼還是配置,都與之沒有什么關(guān)聯(lián),就相當(dāng)于在操作最常用的HttpSession,在實(shí)際項(xiàng)目中用起來也是相當(dāng)方便。
樣例已上傳github,地址:https://github.com/lanxuan826/sample-library/tree/master/sessionshare,有興趣可下載測試。
以上所述是小編給大家介紹的SpringBoot2.x 整合Spring-Session實(shí)現(xiàn)Session共享,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
- SpringBoot3整合MyBatis出現(xiàn)異常:Property?'sqlSessionFactory'or?'sqlSessionTemplate'?are?required
- SpringBoot整合ES-Elasticsearch的實(shí)例
- springboot restTemplate連接池整合方式
- SpringBoot整合Mybatis,解決TypeAliases配置失敗的問題
- Springboot整合Spring Cloud Kubernetes讀取ConfigMap支持自動刷新配置的教程
- es(elasticsearch)整合SpringCloud(SpringBoot)搭建教程詳解
- SpringBoot 整合Jest實(shí)例代碼講解
- SpringBoot整合ES解析搜索返回字段問題
相關(guān)文章
springcloud gateway如何實(shí)現(xiàn)路由和負(fù)載均衡
這篇文章主要介紹了springcloud gateway如何實(shí)現(xiàn)路由和負(fù)載均衡的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java使用ES?Client?調(diào)用滾動查詢及Elasticsearch滾動查詢Scrolling機(jī)制
Elasticsearch提供了一種稱為"滾動查詢"(Scrolling)的機(jī)制,用于處理大型數(shù)據(jù)集的分頁查詢,這篇文章給大家介紹滾動查詢的一般步驟及Java使用ESClient調(diào)用滾動查詢的方法,感興趣的朋友一起看看吧2023-08-08
SpringBoot3整合Swagger3時出現(xiàn)Type javax.servlet.http.H的ttpSe
這篇文章主要介紹了SpringBoot3整合Swagger3時出現(xiàn)Type javax.servlet.http.H的ttpServletRequest not present錯誤解決方法,文中有詳細(xì)的解決方法,需要的朋友可以參考下2025-01-01
SpringBoot項(xiàng)目接收前端參數(shù)的11種方式
在前后端項(xiàng)目交互中,前端傳遞的數(shù)據(jù)可以通過HTTP請求發(fā)送到后端, 后端在Spring Boot中如何接收各種復(fù)雜的前端數(shù)據(jù)呢?這篇文章總結(jié)了11種在Spring Boot中接收前端數(shù)據(jù)的方式,需要的朋友可以參考下2024-12-12
MyBatis動態(tài)sql查詢及多參數(shù)查詢方式
這篇文章主要介紹了MyBatis動態(tài)sql查詢及多參數(shù)查詢方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

