redis整合,采用了spring-data-redis很大程度上方便了我们操作redis的操作.
pom依赖
<spring-data-redis.version>1.8.1.RELEASE</spring-data-redis.version>
<jedis.version>2.9.0</jedis.version>
----------------------------------------------------------------------
<!-- redis Spring 基于注解配置 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring-data-redis.version}</version>
</dependency>
<!-- Redis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
redis.properties文件
#------------------------------------------------------------------------------ # 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
applicationContext-redis.xml
<?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: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" /> <!-- JedisPoolConfig 配置--> <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}" /> <!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)--> <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" /> <!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3--> <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" /> <!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1--> <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" /> <!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个--> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> <!--在空闲时检查有效性, 默认false --> <property name="testWhileIdle" value="${redis.testWhileIdle}" /> </bean > <!-- redis集群配置 哨兵模式 --> <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> <!-- 配置master节点 --> <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> <!--获取redis实例工厂类--> <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> <!--redis操作模版,使用该对象可以操作redis --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" > <property name="connectionFactory" ref="jedisConnectionFactory" /> <!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String! --> <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 > <!--自定义redis工具类,在需要缓存的地方注入此类 --> <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<String,Object> 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}