基於redis叢集實現session共用
Spring-Data-Redis 從1.8.0 版本開始支援了redis叢集密碼
宇宙慣例 先上代碼
1、maven依賴配置
<properties> <spring-session.version>1.2.0.RELEASE</spring-session.version> <spring-data-redis.version>1.8.0.RELEASE</spring-data-redis.version> <jedis.version>2.9.0</jedis.version></properties><dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>${spring-data-redis.version}</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> <version>${spring-session.version}</version> </dependency></dependencies>
2、redis叢集配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="minIdle" value="${redis.minIdle}" /> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="testOnBorrow" value="true" /> </bean> <bean id="clusterRedisNodes1" class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg value="192.168.10.33" /> <constructor-arg value="7000" type="int" /> </bean> <bean id="clusterRedisNodes2" class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg value="192.168.10.61" /> <constructor-arg value="7000" type="int" /> </bean> <bean id="clusterRedisNodes3" class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg value="192.168.10.52" /> <constructor-arg value="7000" type="int" /> </bean> <bean id="clusterRedisNodes4" class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg value="192.168.10.53" /> <constructor-arg value="7000" type="int" /> </bean> <bean id="clusterRedisNodes5" class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg value="192.168.10.54" /> <constructor-arg value="7000" type="int" /> </bean> <bean id="clusterRedisNodes6" class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg value="192.168.10.57" /> <constructor-arg value="7000" type="int" /> </bean> <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <property name="clusterNodes"> <set> <ref bean="clusterRedisNodes1" /> <ref bean="clusterRedisNodes2" /> <ref bean="clusterRedisNodes3" /> <ref bean="clusterRedisNodes4" /> <ref bean="clusterRedisNodes5" /> <ref bean="clusterRedisNodes6" /> </set> </property> </bean> <!-- Redis串連 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg ref="jedisPoolConfig" /> <property name="password" value="${redis.password}"></property> <constructor-arg ref="redisClusterConfiguration" /> </bean> <!-- 緩衝序列化方式 --> <bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <bean id="valueSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" /> <!-- 緩衝 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer" ref="keySerializer" /> <property name="valueSerializer" ref="valueSerializer" /> <property name="hashKeySerializer" ref="keySerializer" /> <property name="hashValueSerializer" ref="valueSerializer" /> </bean> <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> <constructor-arg index="0" ref="redisTemplate" /> <property name="defaultExpiration" value="${redis.expiration}" /> </bean></beans>
3、spring-session.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- SPRING-SESSION --> <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <description>會話有效期間(秒)</description> <property name="maxInactiveIntervalInSeconds" value="600" /> <property name="redisNamespace" value="zero" /> </bean></beans>
4、redis.properties
redis.minIdle=5redis.maxIdle=20redis.maxTotal=100redis.expiration=1000redis.maxWait=-1redis.password=xxxxxxx
4、最後一步修改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>
完工
一行代碼不需要修改,session共用整合完畢,不得不感慨Spring的解耦性。
Spring就是世界主宰.jpg。。。