SpringMVC集成Spring-Session進行分布式管理
項目使用的是SpringMVC+hibernate,聽從老大的意見使用Spring-Session進行處理session,用redis托管Session。下面正式記錄下處理方法。
1.如果項目之前沒有整合過spring-data-redis的話,這一步需要先做,在maven中添加這幾個依賴:
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.orange.redis-embedded</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.6</version>
</dependency>
<!-- 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>
<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>
#------------ redis ------------ redis.host=192.168.1.174 redis.port=6379 redis.pass= redis.timeout=1800
測試環節:
1. 開啟兩個tomcat,eg:localhost:8888和localhot:8080 然后運行起來。
2.開開一個瀏覽器(一定注意使用同一個瀏覽器),打開8080的一個tab,然后登錄,進入個人中心(需要登錄才能進入的界面)。登錄之后,將連接拷貝到另外一個瀏覽器tab,端口改為8888,發現不用登錄就已經登錄進去了。
3.查看redis,Mac下可以使用rdm工具

本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!