首先要搞清楚redis、Jedis、spring data redis的区别。Redis是基于key-value的非关系型数据库,由ANSI C编写;Jedis是redis官方推出的JAVA操作redis的SDK;Spring Date Redis则是由Spring官方推出方便操作Redis的SDK,它是基于Jedis,但是使用起来更为方便。
1 pom.xml
<properties>
<spring-data-redis.version>1.7.1.RELEASE</spring-data-redis.version>
<redis.clients.version>2.9.0</redis.clients.version>
</properties>
<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>${redis.clients.version}</version>
</dependency>
2 redis.properties
redis.hostName=192.168.5.129
redis.port=6379
redis.maxIdle=300
redis.maxTotal=1000
redis.testOnBorrow=true
redis.database=1 #指定数据库为1
3 spring-redis.xml
我们知道序列化至少有两个好处:一、数据的持久化,将数据转成字节序列永久的保存在磁盘中。二、利用序列化实现远程通信,即通过网络传输序列化的字节序列。看看下面的配置StringRedisSerializer和JdkSerializationRedisSerializer两类序列化,思考一下为什么做这样的配置呢?
Redis是内存数据库,Redis也提供了RDB持久化功能,而我们的数据不会是简单的String,事实上redis提供string/set/list/map等数据类型,redis在存储数据时,是以byte[]数组的形式存储。取数据的时候通过反序列化就很容易转成对象,供我们使用。
Spring Data Redis提供了JdkSerializationRedisSerializer、StringRedisSerializer、JacksonJsonRedisSerializer、OxmSerializer四种序列化策略,目前阶段使用基础两种即可。
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-lazy-init="false">
<description>Spring redis配置</description>
<context:property-placeholder location="classpath:properties/redis.properties" />
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="maxTotal" value="${redis.maxTotal}"></property>
<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.hostName}"></property>
<property name="port" value="${redis.port}"></property>
<property name="poolConfig" ref="jedisPoolConfig"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
</bean>
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
</property>
</bean>
</beans>
这段其实也可以不同配置,如果你看过StringRedisTemplate的源码就知道为什么了.也有人认为valueSerializer用StringRedisSerializer也比较好,.不使用JdkSerializationRedisSerializer的原因参考,
推荐使用JdkSerializationRedisSerializer的原因参考
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
</property>
public StringRedisTemplate() {
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
setKeySerializer(stringSerializer);
setValueSerializer(stringSerializer);
setHashKeySerializer(stringSerializer);
setHashValueSerializer(stringSerializer);
}
4 redis sentinel
SDR实现redis哨兵,并配置key过期事件的监听。
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-lazy-init="false">
<description>Spring redis配置</description>
<!-- <context:property-placeholder location="classpath:properties/redis.properties" /> -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="maxTotal" value="${redis.maxTotal}"></property>
<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
</bean>
<bean id="sentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<constructor-arg name="master" value="mymaster" />
<constructor-arg name="sentinelHostAndPorts">
<set>
<value>192.168.5.172:26379</value>
<value>192.168.5.173:26379</value>
<value>192.168.5.174:26379</value>
</set>
</constructor-arg>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!-- <property name="hostName" value="${redis.hostName}"></property>
<property name="port" value="${redis.port}"></property> -->
<property name="database" value="${redis.database}"></property>
<constructor-arg name="sentinelConfig" ref="sentinelConfig"/>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
</bean>
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
</bean>
<!-- redis订阅消息配置 -->
<bean id="clientSessionOvertimeListener" class="com.dzmsoft.icst.base.listener.ClientSessionOvertimeListener"></bean>
<bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
<bean id="messageListenerAdapter" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
<property name="delegate" ref="clientSessionOvertimeListener"></property>
<property name="serializer" ref="stringRedisSerializer"></property>
</bean>
<bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer" destroy-method="destroy">
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
<property name="messageListeners">
<map>
<entry key-ref="messageListenerAdapter">
<bean class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="__keyevent@0__:expired" />
</bean>
</entry>
</map>
</property>
</bean>
</beans>