詳解SpringBoot2 使用Spring Session集群
有幾種辦法:
1.擴展指定server
利用Servlet容器提供的插件功能,自定義HttpSession的創(chuàng)建和管理策略,并通過配置的方式替換掉默認的策略。缺點:耦合Tomcat/Jetty等Servlet容器,不能隨意更換容器。
2.利用Filter
利用HttpServletRequestWrapper,實現(xiàn)自己的 getSession()方法,接管創(chuàng)建和管理Session數(shù)據(jù)的工作。spring-session就是通過這樣的思路實現(xiàn)的。
3 利用spring session
Spring Boot中spring session支持方式:
JDBC、MongoDB、Redis、Hazelcast、HashMap
一、引入maven依賴
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
二、配置application.properties
server.port=8080 spring.redis.host=localhost spring.redis.port=6379 # spring session使用存儲類型 spring.session.store-type=redis
•spirngboot默認就是使用redis方式,如果不想用可以填none。、
三、在啟動類中加入@EnableRedisHttpSession 注解
package com.shyroke; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @EnableCaching @EnableRedisHttpSession @SpringBootApplication public class SpringbootSessionApplication { public static void main(String[] args) { SpringApplication.run(SpringbootSessionApplication.class, args); } }
四、編寫控制器
package com.shyroke.controller; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.http.HttpRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value = "/") public class IndexController { @ResponseBody @RequestMapping(value = "/session") public Map<String, Object> getSession(HttpServletRequest request) { request.getSession().setAttribute("username", "admin"); Map<String, Object> map = new HashMap<String, Object>(); map.put("sessionId", request.getSession().getId()); return map; } @ResponseBody @RequestMapping(value = "/get") public String get(HttpServletRequest request) { String userName = (String) request.getSession().getAttribute("username"); return userName; } }
五、測試
•先輸入http://localhost:8080/session,在session中設置一個值
•http://localhost:8080/get,獲取session中的值
•復制這個工程,application.properties中的server.port=8081,然后訪問“http://localhost:8081/get”,如下獲取到了另一個工程中設置的session的值。
總結
以上所述是小編給大家介紹的SpringBoot2 使用Spring Session集群,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Java之SpringBoot集成ActiveMQ消息中間件案例講解
這篇文章主要介紹了Java之SpringBoot集成ActiveMQ消息中間件案例講解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-07-07SpringBoot實現(xiàn)jsonp跨域通信的方法示例
這篇文章主要介紹了SpringBoot實現(xiàn)jsonp跨域通信的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09SpringBoot?Test的webEnvironment源碼解讀
這篇文章主要為大家介紹了SpringBoot?Test的webEnvironment源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09Java Base64算法實際應用之郵件發(fā)送實例分析
這篇文章主要介紹了Java Base64算法實際應用之郵件發(fā)送,結合實例形式分析了java字符編碼與郵件發(fā)送相關操作技巧,需要的朋友可以參考下2019-09-09linux中nohup?java?-jar啟動java項目的步驟
nohup是一個Unix和Linux命令,用于運行關閉時不會被終止的進程,這篇文章主要給大家介紹了關于linux中nohup?java?-jar啟動java項目的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-08-08