spring集成 JedisCluster 连接 redis3.0 集群

97 篇文章 0 订阅

客户端采用最新的jedis 2.7

1.

maven依赖:

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.7.2</version>

</dependency>

 

2.

增加spring 配置

Java代码   收藏代码
  1. <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" >  
  2.         <property name="maxWaitMillis" value="-1" />  
  3.         <property name="maxTotal" value="1000" />  
  4.         <property name="minIdle" value="8" />  
  5.         <property name="maxIdle" value="100" />  
  6. </bean>  
  7.   
  8. <bean id="jedisCluster" class="xxx.JedisClusterFactory">  
  9.     <property name="addressConfig">  
  10.         <value>classpath:connect-redis.properties</value>  
  11.     </property>  
  12.     <property name="addressKeyPrefix" value="address" />   <!--  属性文件里  key的前缀 -->  
  13.       
  14.     <property name="timeout" value="300000" />  
  15.     <property name="maxRedirections" value="6" />  
  16.     <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />  
  17. </bean>  

 

3.

增加connect-redis.properties  配置文件

这里配置了6个节点

Java代码   收藏代码
  1. address1=172.16.23.27:6379  
  2. address2=172.16.23.27:6380  
  3. address3=172.16.23.27:6381  
  4. address4=172.16.23.27:6382  
  5. address5=172.16.23.27:6383  
  6. address6=172.16.23.27:6384  

 

4.

增加java类:

Java代码   收藏代码
  1. import java.util.HashSet;  
  2. import java.util.Properties;  
  3. import java.util.Set;  
  4. import java.util.regex.Pattern;  
  5.   
  6. import org.apache.commons.pool2.impl.GenericObjectPoolConfig;  
  7. import org.springframework.beans.factory.FactoryBean;  
  8. import org.springframework.beans.factory.InitializingBean;  
  9. import org.springframework.core.io.Resource;  
  10.   
  11. import redis.clients.jedis.HostAndPort;  
  12. import redis.clients.jedis.JedisCluster;  
  13.   
  14. public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {  
  15.   
  16.     private Resource addressConfig;  
  17.     private String addressKeyPrefix ;  
  18.   
  19.     private JedisCluster jedisCluster;  
  20.     private Integer timeout;  
  21.     private Integer maxRedirections;  
  22.     private GenericObjectPoolConfig genericObjectPoolConfig;  
  23.       
  24.     private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");  
  25.   
  26.     @Override  
  27.     public JedisCluster getObject() throws Exception {  
  28.         return jedisCluster;  
  29.     }  
  30.   
  31.     @Override  
  32.     public Class<? extends JedisCluster> getObjectType() {  
  33.         return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);  
  34.     }  
  35.   
  36.     @Override  
  37.     public boolean isSingleton() {  
  38.         return true;  
  39.     }  
  40.   
  41.   
  42.   
  43.     private Set<HostAndPort> parseHostAndPort() throws Exception {  
  44.         try {  
  45.             Properties prop = new Properties();  
  46.             prop.load(this.addressConfig.getInputStream());  
  47.   
  48.             Set<HostAndPort> haps = new HashSet<HostAndPort>();  
  49.             for (Object key : prop.keySet()) {  
  50.   
  51.                 if (!((String) key).startsWith(addressKeyPrefix)) {  
  52.                     continue;  
  53.                 }  
  54.   
  55.                 String val = (String) prop.get(key);  
  56.   
  57.                 boolean isIpPort = p.matcher(val).matches();  
  58.   
  59.                 if (!isIpPort) {  
  60.                     throw new IllegalArgumentException("ip 或 port 不合法");  
  61.                 }  
  62.                 String[] ipAndPort = val.split(":");  
  63.   
  64.                 HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));  
  65.                 haps.add(hap);  
  66.             }  
  67.   
  68.             return haps;  
  69.         } catch (IllegalArgumentException ex) {  
  70.             throw ex;  
  71.         } catch (Exception ex) {  
  72.             throw new Exception("解析 jedis 配置文件失败", ex);  
  73.         }  
  74.     }  
  75.       
  76.     @Override  
  77.     public void afterPropertiesSet() throws Exception {  
  78.         Set<HostAndPort> haps = this.parseHostAndPort();  
  79.           
  80.         jedisCluster = new JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);  
  81.           
  82.     }  
  83.     public void setAddressConfig(Resource addressConfig) {  
  84.         this.addressConfig = addressConfig;  
  85.     }  
  86.   
  87.     public void setTimeout(int timeout) {  
  88.         this.timeout = timeout;  
  89.     }  
  90.   
  91.     public void setMaxRedirections(int maxRedirections) {  
  92.         this.maxRedirections = maxRedirections;  
  93.     }  
  94.   
  95.     public void setAddressKeyPrefix(String addressKeyPrefix) {  
  96.         this.addressKeyPrefix = addressKeyPrefix;  
  97.     }  
  98.   
  99.     public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {  
  100.         this.genericObjectPoolConfig = genericObjectPoolConfig;  
  101.     }  
  102.   
  103. }  

 

 

5.

到此配置完成

使用时,直接注入即可, 如下所示:

 

@Autowired

JedisCluster jedisCluster;

 

http://xyqck163.iteye.com/blog/2211108

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值