學(xué)習(xí)Spring-Session+Redis實(shí)現(xiàn)session共享的方法
1、添加依賴
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.2.1.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency>
2、配置
spring-mvc.xml:
<bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="600"/>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="100" />
<property name="maxIdle" value="10" />
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
<property name="hostName" value="${redis_hostname}"/>
<property name="port" value="${redis_port}"/>
<property name="password" value="${redis_pwd}" />
<property name="timeout" value="3000"/>
<property name="usePool" value="true"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
web.xml添加攔截器:
<filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3、使用spring-session
只要使用標(biāo)準(zhǔn)的servlet api調(diào)用session,在底層就會通過Spring Session得到的,并且會存儲到Redis或其他你所選擇的數(shù)據(jù)源中。
這里是我寫的一個(gè)demo:
/**
* @author fengzp
* @date 17/2/23下午3:19
*/
@Controller
@RequestMapping(value = "index")
public class IndexController {
private final Gson gson = new GsonBuilder().setDateFormat("yyyyMMddHHmmss").create();
@RequestMapping(value = "login")
public String login(HttpServletRequest request, String username){
request.getSession().setAttribute("user", gson.toJson(new User(username,"123456")));
return "login";
}
@RequestMapping(value = "index")
public String index(HttpServletRequest request, Model model){
User user = gson.fromJson(request.getSession().getAttribute("user").toString(), User.class);
model.addAttribute("user", user);
return "index";
}
}
index.jsp:
第一個(gè)tomcat
<html>
<body>
<h2>Hello World!</h2>
<p>${user.username}</p>
</body>
</html>
第二個(gè)tomcat
<html>
<body>
<h2>Hello World! i am the second!</h2>
<p>${user.username}</p>
</body>
</html>
測試
這里利用上一篇nginx負(fù)載配置的兩個(gè)tomcat來測試。
首先訪問 http://192.168.99.100/feng/index/login.htm?username=nginx 來觸發(fā)生成session。
查看redis,發(fā)現(xiàn)session已經(jīng)保存到redis。

訪問 http://192.168.99.100/feng/index/index.htm 來讀取session, 并刷新多次。


發(fā)現(xiàn)在負(fù)載的情況下讀取session沒問題,并且是同一個(gè)session,成功實(shí)現(xiàn)負(fù)載+session共享!以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
由@NotNull注解引出的關(guān)于Java空指針的控制
這是一些很容易學(xué)會的簡單技術(shù),但是對于代碼質(zhì)量和健壯性來說確實(shí)很重要。以我的經(jīng)驗(yàn),僅是第一個(gè)小技巧就已經(jīng)對改進(jìn)代碼質(zhì)量具有很大的作用了2016-09-09
java如何將int數(shù)組轉(zhuǎn)化為Integer數(shù)組
這篇文章主要介紹了java如何將int數(shù)組轉(zhuǎn)化為Integer數(shù)組,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
分析那些不講武德的SDK(構(gòu)造使用規(guī)范)
這篇文章主要為大家介紹了盤點(diǎn)分析那些不講武德的SDK(構(gòu)造規(guī)范)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
基于OpenCv與JVM實(shí)現(xiàn)加載保存圖像功能(JAVA?圖像處理)
openCv有一個(gè)名imread的簡單函數(shù),用于從文件中讀取圖像,本文給大家介紹JAVA?圖像處理基于OpenCv與JVM實(shí)現(xiàn)加載保存圖像功能,感興趣的朋友一起看看吧2022-01-01
詳解@Autowired(required=false)注入注意的問題
這篇文章主要介紹了@Autowired(required=false)注入注意的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Java進(jìn)階學(xué)習(xí):jar打包詳解
Java進(jìn)階學(xué)習(xí):jar打包詳解...2006-12-12

