redis整合,采用了spring-data-redis很大程度上方便了我们操作redis的操作.
<spring-data-redis.version>1.8.1.RELEASEspring-data-redis.version>
<jedis.version>2.9.0jedis.version>
----------------------------------------------------------------------
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-redisartifactId>
<version>${spring-data-redis.version}version>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>${jedis.version}version>
dependency>
#------------------------------------------------------------------------------
# redis缓存架构"一主两从三哨兵"模式
# 主: 172.19.105.188 7001
# 从1: 172.19.105.188 7002
# 从2: 172.19.105.188 7003
#
# 哨兵1: 172.19.105.188 27001
# 哨兵2: 172.19.105.188 27002
# 哨兵3: 172.19.105.188 27003
#
#-------------------------------------------------------------------------------
#-------------------------- Matser的ip地址配置 ----------------------------------
# 主机
redis.hostName=172.19.105.188
# 端口号
redis.port=7001
# 连接redis密码
redis.password=
# 客户端超时时间单位是毫秒 默认是2000
redis.timeout=10000
#----------------------- JedisPool的配置 ----------------------------------------
# 最大空闲数
redis.maxIdle=400
# 连接池的最大数据库连接数。设为0表示无限制,控制一个pool可分配多少个jedis实例
redis.maxTotal=1000
# 最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
redis.maxWaitMillis=1000
# 连接的最小空闲时间 默认1800000毫秒(30分钟)
redis.minEvictableIdleTimeMillis=300000
# 每次释放连接的最大数目,默认5
redis.numTestsPerEvictionRun=1024
# 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
redis.timeBetweenEvictionRunsMillis=30000
# 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
redis.testOnBorrow=true
# 在空闲时检查有效性, 默认false
redis.testWhileIdle=true
#----------------------------- 哨兵配置 ------------------------------------------
# 哨兵1
redis.sentinel.host1=172.19.105.188
redis.sentinel.port1=27001
# 哨兵2
redis.sentinel.host2=172.19.105.188
redis.sentinel.port2=27002
# 哨兵3
redis.sentinel.host3=172.19.105.188
redis.sentinel.port3=27003
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">
<context:property-placeholder location="classpath:config/redis.properties" />
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" >
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />
<property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />
<property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="testWhileIdle" value="${redis.testWhileIdle}" />
bean >
<bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<property name="name" value="redis1">property>
<constructor-arg name="host" value="${redis.hostName}"/>
<constructor-arg name="port" value="${redis.port}"/>
bean>
property>
<property name="sentinels">
<set>
<bean name="sentinel1" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.sentinel.host1}">constructor-arg>
<constructor-arg name="port" value="${redis.sentinel.port1}">constructor-arg>
bean>
<bean name="sentinel2" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.sentinel.host2}">constructor-arg>
<constructor-arg name="port" value="${redis.sentinel.port2}">constructor-arg>
bean>
<bean name="sentinel3" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.sentinel.host3}">constructor-arg>
<constructor-arg name="port" value="${redis.sentinel.port3}">constructor-arg>
bean>
set>
property>
bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="sentinelConfig" ref="sentinelConfiguration">constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig">constructor-arg>
bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer" >
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
property>
<property name="valueSerializer" >
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
property>
<property name="enableTransactionSupport" value="true">property>
bean >
<bean id="redisUtil" class="com.xflig.utils.redis.RedisUtil">
<property name="redisTemplate" ref="redisTemplate" />
bean>
beans>
public class RedisSpringTest {
public static void main(String[] args) throws InterruptedException {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");
RedisUtil redisUtil = (RedisUtil) context.getBean("redisUtil");
redisUtil.set("city", "北京");
System.out.println(redisUtil.get("city"));
// 设置key为name,value为lpl,10为时间(秒)
redisUtil.set("name", "成龙", 30);
System.out.println("姓名:"+redisUtil.get("name"));
boolean flag = true;
int i = 0;
while(flag){
Thread.sleep(3000);
System.out.println("还剩:"+redisUtil.getExpire("name") + "s.");
i++;
if(i > 11){
flag = false;
}
}
System.out.println("姓名:"+redisUtil.get("name"));
Map userMap = new HashMap<>();
userMap.put("name", "成龙");
userMap.put("age", 60);
redisUtil.hmset("user", userMap);
System.out.println(redisUtil.hmget("user"));
}
}
四月 24, 2018 5:46:30 下午 redis.clients.jedis.JedisSentinelPool initSentinels
信息: Trying to find master from available Sentinels…
四月 24, 2018 5:46:30 下午 redis.clients.jedis.JedisSentinelPool initSentinels
信息: Redis master running at 172.19.105.188:7001, starting Sentinel listeners…
四月 24, 2018 5:46:30 下午 redis.clients.jedis.JedisSentinelPool initPool
信息: Created JedisPool to master at 172.19.105.188:7001
北京
姓名:成龙
还剩:26s.
还剩:23s.
还剩:20s.
还剩:17s.
还剩:14s.
还剩:11s.
还剩:8s.
还剩:5s.
还剩:2s.
还剩:-2s.
还剩:-2s.
还剩:-2s.
姓名:null
Disconnected from the target VM, address: ‘127.0.0.1:56792’, transport: ‘socket’
{name=成龙, age=60}
Process finished with exit code 0