基于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=5
redis.maxIdle=20
redis.maxTotal=100
redis.expiration=1000
redis.maxWait=-1
redis.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!!!