怎么在Redis中實現(xiàn)一個分布式Session管理機制?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。


1.redis的session管理是利用spring提供的session管理解決方案,將一個應(yīng)用session交給Redis存儲,整個應(yīng)用中所有session的請求都會去redis中獲取對應(yīng)的session數(shù)據(jù)。

1. 引入依賴pop.xml
<!--springboot-redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--spring-data-redis session 管理--> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> <!--排除內(nèi)嵌tomcat--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
2. 開發(fā)Session管理配置類(使用注解)

@Configuration
@EnableRedisHttpSession //將整個應(yīng)用中使用session的數(shù)據(jù)全部交給redis處理
public class RedisSessionManager {
}3. Controller層設(shè)計
package com.xizi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("test")
public class TestController {
//使用redis 的session管理 注意:當(dāng)session中數(shù)據(jù)發(fā)生變化時必須將session中變化的數(shù)據(jù)同步到redis中
@RequestMapping("test")
public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {
List<String> list = (List<String>) request.getSession().getAttribute("list");
if(list==null){
list = new ArrayList<>();
}
list.add("xxxx");
request.getSession().setAttribute("list",list);//每次session變化都要同步session
response.getWriter().println("size: "+list.size());
response.getWriter().println("sessionid: "+request.getSession().getId());
}
@RequestMapping("logout")
public void logout(HttpServletRequest request){
//退出登錄
request.getSession().invalidate();//失效
}
}4.打包測試

1.Nginx相關(guān)配置



2.Tomcat集群

//這是tom4 后面的兩個端口號依次+1 //關(guān)閉端口 <Server port="8003" shutdown="SHUTDOWN"> //連接端口 <Connector port="8989" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="10010" protocol="AJP/1.3" redirectPort="8443" />

改變初始頁面index.jsp

3.Redis集群
已經(jīng)開啟了,不會的去看我前面的Redis集群搭建博客


4. 測試
上傳war包到三個Tomcat的Webapps目錄下

直接訪問Nginx頁面,反向代理了Tomcat集群



關(guān)于怎么在Redis中實現(xiàn)一個分布式Session管理機制問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
本文名稱:怎么在Redis中實現(xiàn)一個分布式Session管理機制-創(chuàng)新互聯(lián)
標(biāo)題URL:http://chinadenli.net/article18/hcdgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、動態(tài)網(wǎng)站、定制網(wǎng)站、品牌網(wǎng)站設(shè)計、網(wǎng)站策劃、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容