Redis RedisCluster Spring整合

转载: http://blog.csdn.net/fengyong7723131/article/details/52995592

前言:

在上一篇文章中,用了jedisCluster来做redis集群的客户端,这一篇我们介绍一下spring-data-redis给我们提供的操作redis集群的redisTemplate,着急用的可以跳过1,直接看2

准备工作:

jdk版本:1.8

junit版本:4.12

jar包版本:

[html]  view plain  copy
  1.   
  2. <dependency>  
  3.   <groupId>redis.clientsgroupId>  
  4.   <artifactId>jedisartifactId>  
  5.   <version>2.9.0version>  
  6.   <type>jartype>  
  7. dependency>  
[html]  view plain  copy
  1.   
  2. <dependency>  
  3.   <groupId>org.springframeworkgroupId>  
  4.   <artifactId>spring-contextartifactId>  
  5.   <version>4.3.3.RELEASEversion>  
  6. dependency>  
[html]  view plain  copy
  1. <dependency>  
  2.   <groupId>org.springframework.datagroupId>  
  3.   <artifactId>spring-data-redisartifactId>  
  4.   <version>1.7.4.RELEASEversion>  
  5. dependency>  

1:RedisTemplate由来简介

在网上没有找到redisTemplate操作redis集群的例子,所以只能自己动手,在这里简单说一下过程.首先既然redisTemplate依赖jedis,那我们可以认为他内部操作的就是jedis,同理,我们也可以认为他内部也能操作jedisCluster.接下来就在spring-data-redis的源码里面搜一下jedisCluster这个字符串,发现JedisClusterConnection和JedisConnectionFactory中出现了jedisCluster,有没有觉得JedisConnectionFactory很眼熟呢,对,就是配置文件中redisTemplate初始化时候需要用到的连接工厂.现在就可以直接看JedisConnectionFactory
首先,我们来看JedisConnectionFactory,发现里面有一个属性就是jedisCluster,那就看看jedisCluster是如何被初始化的,看下图:

Redis RedisCluster Spring整合_第1张图片
我们可以先看这个方法,这个方法是从InitializingBean中实现的方法,是spring初始化bean的时候需要调用的.所以可以假装认为只要实例化了JedisConnectionFactory就可以实例化jedisCluster,但是不要忘了有一个条件,那就是clusterConfig不能为空,接下来我们找clusterConfig是如何被实例化的.发现JedisConnectionFactory有一个构造函数

JedisConnectionFactory(RedisClusterConfiguration clusterConfig, JedisPoolConfig poolConfig).这下就好办了JedisPoolConfig我们本来就认识,RedisClusterConfiguration是需要我们实例化的,接下来就看看RedisClusterConfiguration,一进来RedisClusterConfiguration我们就能看到个好东西,见下图:

Redis RedisCluster Spring整合_第2张图片

Redis RedisCluster Spring整合_第3张图片

我们可以看RedisClusterConfiguration的注释,虽然没有说明,但是光看格式,就大概能猜到这些东西应该是写到properties文件里面的,而构造函数的参数又正好是propertySource,很容易就能联想到ResourcePropertySource,接下来就简单了,直接开始开干了.

2:redisClusterConfig配置文件

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:p="http://www.springframework.org/schema/p"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  6.          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
  7.   
  8.   
  9.     <bean id="propertyConfigurer"  
  10.           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  11.         <property name="location" value="classpath:redis.properties" />  
  12.     bean>  
  13.   
  14.       
  15.     <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" >  
  16.           
  17.         <property name="maxIdle" value="${redis.maxIdle}" />  
  18.           
  19.         <property name="maxWaitMillis" value="${redis.maxWait}" />  
  20.           
  21.         <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
  22.     bean >  
  23.   
  24.       
  25.     <bean id="resourcePropertySource" class="org.springframework.core.io.support.ResourcePropertySource">  
  26.         <constructor-arg name="name" value="redis.properties"/>  
  27.         <constructor-arg name="resource" value="classpath:redis.properties"/>  
  28.     bean>  
  29.       
  30.     <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">  
  31.         <constructor-arg name="propertySource" ref="resourcePropertySource"/>  
  32.     bean>  
  33.       
  34.     <bean id="connectionFactory"  class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >  
  35.        <constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>  
  36.         <constructor-arg name="poolConfig" ref="poolConfig"/>  
  37.         <property name="password" value="${redis.password}" />  
  38.         <property name="timeout" value="${redis.timeout}" >property>  
  39.     bean >  
  40.     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >  
  41.         <property name="connectionFactory" ref="connectionFactory" />  
  42.           
  43.         <property name="keySerializer" >  
  44.             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
  45.         property>  
  46.         <property name="valueSerializer" >  
  47.             <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
  48.         property>  
  49.         <property name="hashKeySerializer">  
  50.             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>  
  51.         property>  
  52.         <property name="hashValueSerializer">  
  53.             <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>  
  54.         property>  
  55.     bean >  
  56.   
  57.     <bean id="redisHttpSessionConfiguration"  
  58.           class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">  
  59.          
  60.         <property name="maxInactiveIntervalInSeconds" value="1800" />  
  61.     bean>  
  62.   
  63. beans>  

3:redis.properties配置文件

[plain]  view plain  copy
  1. #redis中心  
  2. #redis的服务器地址  
  3. redis.host=127.0.0.1  
  4. #redis的服务端口  
  5. redis.port=6379  
  6. #密码  
  7. redis.password=  
  8. #最大空闲数  
  9. redis.maxIdle=100  
  10. #最大连接数  
  11. redis.maxActive=300  
  12. #最大建立连接等待时间  
  13. redis.maxWait=1000  
  14. #客户端超时时间单位是毫秒  
  15. redis.timeout=100000  
  16. redis.maxTotal=1000  
  17. redis.minIdle=8  
  18. #明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个  
  19. redis.testOnBorrow=true  
  20.   
  21.   
  22. #rediscluster  
  23. spring.redis.cluster.nodes=127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004,127.0.0.1:7005,127.0.0.1:7006,127.0.0.1:7007  
  24. spring.redis.cluster.max-redirects=3  
  25. #rediscluster  

4:直接注入就可以使用,测试通过

你可能感兴趣的:(redis&缓存)