spring boot與redis 實(shí)現(xiàn)session共享教程
如果大家對(duì)spring boot不是很了解,大家可以參考下面兩篇文章。
這次帶來的是spring boot + redis 實(shí)現(xiàn)session共享的教程。
在spring boot的文檔中,告訴我們添加@EnableRedisHttpSession來開啟spring session支持,配置如下:
@Configuration @EnableRedisHttpSession public class RedisSessionConfig { }
而@EnableRedisHttpSession這個(gè)注解是由spring-session-data-redis提供的,所以在pom.xml文件中添加:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>
接下來,則需要在application.properties中配置redis服務(wù)器的位置了,在這里,我們就用本機(jī):
spring.redis.host=localhost spring.redis.port=6379
這樣以來,最簡單的spring boot + redis實(shí)現(xiàn)session共享就完成了,下面進(jìn)行下測試。
首先我們開啟兩個(gè)tomcat服務(wù),端口分別為8080和9090,在application.properties中進(jìn)行設(shè)置【下載地址】 :
server.port=8080
接下來定義一個(gè)Controller:
@RestController @RequestMapping(value = "/admin/v1") public class QuickRun { @RequestMapping(value = "/first", method = RequestMethod.GET) public Map<String, Object> firstResp (HttpServletRequest request){ Map<String, Object> map = new HashMap<>(); request.getSession().setAttribute("request Url", request.getRequestURL()); map.put("request Url", request.getRequestURL()); return map; } @RequestMapping(value = "/sessions", method = RequestMethod.GET) public Object sessions (HttpServletRequest request){ Map<String, Object> map = new HashMap<>(); map.put("sessionId", request.getSession().getId()); map.put("message", request.getSession().getAttribute("map")); return map; } }
啟動(dòng)之后進(jìn)行訪問測試,首先訪問8080端口的tomcat,返回 獲取【下載地址】 :
{"request Url":"http://localhost:8080/admin/v1/first"}
接著,我們?cè)L問8080端口的sessions,返回:
{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":http://localhost:8080/admin/v1/first}
最后,再訪問9090端口的sessions,返回:
{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":http://localhost:8080/admin/v1/first}
可見,8080與9090兩個(gè)服務(wù)器返回結(jié)果一樣,實(shí)現(xiàn)了session的共享
如果此時(shí)再訪問9090端口的first的話,首先返回:
{"request Url":"http://localhost:9090/admin/v1/first"}
而兩個(gè)服務(wù)器的sessions都是返回:
{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:9090/admin/v1/first"}
通過spring boot + redis來實(shí)現(xiàn)session的共享非常簡單,而且用處也極大,配合nginx進(jìn)行負(fù)載均衡,便能實(shí)現(xiàn)分布式的應(yīng)用了。
本次的redis并沒有進(jìn)行主從、讀寫分離等等配置(_(:з」∠)_其實(shí)是博主懶,還沒嘗試過.......)
而且,nginx的單點(diǎn)故障也是我們應(yīng)用的障礙......以后可能會(huì)有對(duì)此次博客的改進(jìn)版本,比如使用zookeeper進(jìn)行負(fù)載均衡,敬請(qǐng)期待。
好了,到此結(jié)束吧,以上所述是小編給大家介紹的spring boot與redis 實(shí)現(xiàn)session共享教程,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 基于SpringBoot+Redis的Session共享與單點(diǎn)登錄詳解
- SpringBoot2.x 整合Spring-Session實(shí)現(xiàn)Session共享功能
- Spring boot集成spring session實(shí)現(xiàn)session共享的方法
- Springboot實(shí)現(xiàn)多服務(wù)器session共享
- spring boot整合redis實(shí)現(xiàn)shiro的分布式session共享的方法
- 詳解springboot中redis的使用和分布式session共享問題
- Spring Boot高級(jí)教程之使用Redis實(shí)現(xiàn)session共享
- Springboot Session共享實(shí)現(xiàn)原理及代碼實(shí)例
相關(guān)文章
MyBatis-Plus與Druid結(jié)合Dynamic-datasource實(shí)現(xiàn)多數(shù)據(jù)源操作數(shù)據(jù)庫的示例
Dynamic-DataSource 可以和絕大多是連接層插件搭配使用,比如:mybatis,mybatis-plus,hibernate等,本文就來介紹一下MyBatis-Plus與Druid結(jié)合Dynamic-datasource實(shí)現(xiàn)多數(shù)據(jù)源操作數(shù)據(jù)庫的示例,感興趣的可以了解一下2023-10-10SpringBoot 圖書管理系統(tǒng)(刪除、強(qiáng)制登錄、更新圖書)詳細(xì)代碼
在企業(yè)開發(fā)中,通常不采用delete語句進(jìn)行物理刪除,而是使用邏輯刪除,邏輯刪除通過修改標(biāo)識(shí)字段來表示數(shù)據(jù)已被刪除,方便數(shù)據(jù)恢復(fù),本文給大家介紹SpringBoot 圖書管理系統(tǒng)實(shí)例代碼,感興趣的朋友跟隨小編一起看看吧2024-09-09springboot實(shí)戰(zhàn)權(quán)限管理功能圖文步驟附含源碼
這篇文章主要為大家介紹了springboot實(shí)戰(zhàn)權(quán)限管理功能圖文步驟及示例源碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06springboot application.properties 文件注入數(shù)組方式
這篇文章主要介紹了springboot application.properties 文件注入數(shù)組方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11spring中ApplicationListener的使用小結(jié)
ApplicationListener是spring提供的一個(gè)監(jiān)聽器,本文主要介紹了spring中ApplicationListener的使用小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07