詳解SpringBoot2 使用Spring Session集群
有幾種辦法:
1.擴(kuò)展指定server
利用Servlet容器提供的插件功能,自定義HttpSession的創(chuàng)建和管理策略,并通過配置的方式替換掉默認(rèn)的策略。缺點(diǎn):耦合Tomcat/Jetty等Servlet容器,不能隨意更換容器。
2.利用Filter
利用HttpServletRequestWrapper,實(shí)現(xiàn)自己的 getSession()方法,接管創(chuàng)建和管理Session數(shù)據(jù)的工作。spring-session就是通過這樣的思路實(shí)現(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默認(rèn)就是使用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中設(shè)置一個值

•http://localhost:8080/get,獲取session中的值

•復(fù)制這個工程,application.properties中的server.port=8081,然后訪問“http://localhost:8081/get”,如下獲取到了另一個工程中設(shè)置的session的值。
總結(jié)
以上所述是小編給大家介紹的SpringBoot2 使用Spring Session集群,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
java的poi技術(shù)讀取和導(dǎo)入Excel實(shí)例
本篇文章主要介紹了java的poi技術(shù)讀取和導(dǎo)入Excel實(shí)例,報表輸出是Java應(yīng)用開發(fā)中經(jīng)常涉及的內(nèi)容,有需要的可以了解一下。2016-11-11
mybatis plus in使用時傳數(shù)組、集合的注意點(diǎn)說明
這篇文章主要介紹了mybatis plus in使用時傳數(shù)組、集合的注意點(diǎn)說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
Spring @Component自定義注解實(shí)現(xiàn)詳解
@Component是一個元注解,意思是可以注解其他類注解,如@Controller @Service @Repository @Aspect。官方的原話是:帶此注解的類看為組件,當(dāng)使用基于注解的配置和類路徑掃描的時候,這些類就會被實(shí)例化2022-09-09
WebUploader實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了WebUploader實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-03-03
9個小技巧讓你的Java if else看起來更優(yōu)雅
這篇文章主要給大家介紹了9個小技巧,通過這幾個小技巧可以讓你的Java if else看起來更優(yōu)雅,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Spring?MVC DispatcherServlet處理請求過程示例詳解
這篇文章主要介紹了Spring?MVC?DispatcherServlet處理請求過程示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Mybatis-plus數(shù)據(jù)權(quán)限D(zhuǎn)ataPermissionInterceptor實(shí)現(xiàn)
本文主要介紹了Mybatis-plus數(shù)據(jù)權(quán)限D(zhuǎn)ataPermissionInterceptor實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
SpringMVC中的@ControllerAdvice使用場景詳解
這篇文章主要介紹了SpringMVC中的@ControllerAdvice使用場景詳解,在Spring?MVC進(jìn)行調(diào)用的過程中,會有很多的特殊的需求,比如全局異常,分頁信息和分頁搜索條件,請求時帶來返回時還得回顯頁面,需要的朋友可以參考下2024-01-01

