日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring session实现集群中session共享

發(fā)布時(shí)間:2024/4/13 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring session实现集群中session共享 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文轉(zhuǎn)自:http://dorole.com/1422/

使用框架的會(huì)話管理工具,也就是本文要說的spring-session,可以理解是替換了Servlet那一套會(huì)話管理,既不依賴容器,又不需要改動(dòng)代碼,并且是用了spring-data-redis那一套連接池,可以說是最完美的解決方案。當(dāng)然,前提是項(xiàng)目要使用Spring Framework才行。

  這里簡(jiǎn)單記錄下整合的過程:

  如果項(xiàng)目之前沒有整合過spring-data-redis的話,這一步需要先做,在maven中添加這兩個(gè)依賴:

? ?

<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.5.2.RELEASE</version> </dependency> <dependency><groupId>org.springframework.session</groupId><artifactId>spring-session</artifactId><version>1.0.2.RELEASE</version> </dependency>

?

  再在applicationContext.xml中添加以下bean,用于定義redis的連接池和初始化redis模版操作類,自行替換其中的相關(guān)變量。

? ? ? <!-- redis --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> </bean><bean id="jedisConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><property name="hostName" value="${redis.host}" /><property name="port" value="${redis.port}" /><property name="password" value="${redis.pass}" /><property name="timeout" value="${redis.timeout}" /><property name="poolConfig" ref="jedisPoolConfig" /><property name="usePool" value="true" /> </bean><bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"><property name="connectionFactory" ref="jedisConnectionFactory" /> </bean><!-- 將session放入redis --> <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"><property name="maxInactiveIntervalInSeconds" value="1800" /> </bean>

?

  這里前面幾個(gè)bean都是操作redis時(shí)候使用的,最后一個(gè)bean才是spring-session需要用到的,其中的id可以不寫或者保持不變,這也是一個(gè)約定優(yōu)先配置的體現(xiàn)。這個(gè)bean中又會(huì)自動(dòng)產(chǎn)生多個(gè)bean,用于相關(guān)操作,極大的簡(jiǎn)化了我們的配置項(xiàng)。其中有個(gè)比較重要的是springSessionRepositoryFilter,它將在下面的代理filter中被調(diào)用到。maxInactiveIntervalInSeconds表示超時(shí)時(shí)間,默認(rèn)是1800秒。寫上述配置的時(shí)候我個(gè)人習(xí)慣采用xml來定義,官方文檔中有采用注解來聲明一個(gè)配置類。

  然后是在web.xml中添加一個(gè)session代理filter,通過這個(gè)filter來包裝Servlet的getSession()。需要注意的是這個(gè)filter需要放在所有filter鏈最前面。

<!-- delegatingFilterProxy --> <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>

  這樣便配置完畢了,需要注意的是,spring-session要求Redis Server版本不低于2.8。

  驗(yàn)證:使用redis-cli就可以查看到session key了,且瀏覽器Cookie中的jsessionid已經(jīng)替換為session。

?

總結(jié)

以上是生活随笔為你收集整理的spring session实现集群中session共享的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。