最近在公司做了 jedisCluster整合spring 的配置, 分享如下

客户端采用最新的jedis 2.7

1.

maven依赖:

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.7.2</version></dependency> 

2.

增加spring 配置

<bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" >  <property name="maxWaitMillis" value="-1" />  <property name="maxTotal" value="1000" />  <property name="minIdle" value="8" />  <property name="maxIdle" value="100" />
</bean>  <bean id="jedisCluster" class="xxx.JedisClusterFactory">  <property name="addressConfig">  <value>classpath:connect-redis.properties</value>  </property>  <property name="addressKeyPrefix" value="address" />   <!--  属性文件里  key的前缀 -->  <property name="timeout" value="300000" />  <property name="maxRedirections" value="6" />  <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />
</bean>  

3.

增加connect-redis.properties  配置文件

这里配置了6个节点

address1=172.16.23.27:6379
address2=172.16.23.27:6380
address3=172.16.23.27:6381
address4=172.16.23.27:6382
address5=172.16.23.27:6383
address6=172.16.23.27:6384  

4.

增加java类:

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;  import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;  import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;  public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {  private Resource addressConfig;  private String addressKeyPrefix ;  private JedisCluster jedisCluster;  private Integer timeout;  private Integer maxRedirections;  private GenericObjectPoolConfig genericObjectPoolConfig;  private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");  @Override  public JedisCluster getObject() throws Exception {  return jedisCluster;  }  @Override  public Class<? extends JedisCluster> getObjectType() {  return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);  }  @Override  public boolean isSingleton() {  return true;  }  private Set<HostAndPort> parseHostAndPort() throws Exception {  try {  Properties prop = new Properties();  prop.load(this.addressConfig.getInputStream());  Set<HostAndPort> haps = new HashSet<HostAndPort>();  for (Object key : prop.keySet()) {  if (!((String) key).startsWith(addressKeyPrefix)) {  continue;  }  String val = (String) prop.get(key);  boolean isIpPort = p.matcher(val).matches();  if (!isIpPort) {  throw new IllegalArgumentException("ip 或 port 不合法");  }  String[] ipAndPort = val.split(":");  HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));  haps.add(hap);  }  return haps;  } catch (IllegalArgumentException ex) {  throw ex;  } catch (Exception ex) {  throw new Exception("解析 jedis 配置文件失败", ex);  }  }  @Override  public void afterPropertiesSet() throws Exception {  Set<HostAndPort> haps = this.parseHostAndPort();  jedisCluster = new JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);  }  public void setAddressConfig(Resource addressConfig) {  this.addressConfig = addressConfig;  }  public void setTimeout(int timeout) {  this.timeout = timeout;  }  public void setMaxRedirections(int maxRedirections) {  this.maxRedirections = maxRedirections;  }  public void setAddressKeyPrefix(String addressKeyPrefix) {  this.addressKeyPrefix = addressKeyPrefix;  }  public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {  this.genericObjectPoolConfig = genericObjectPoolConfig;  }  }  

5.

到此配置完成

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

@Autowired

JedisCluster jedisCluster;

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

spring集成 JedisCluster 连接 redis3.0 集群相关推荐

  1. Redis3.0 集群

    1  Redis3.0集群 Redis3.0之前项目架构 转载于:https://blog.51cto.com/liuzedong/1663726

  2. java redis集群操作,java操作redis3.0集群

    java(JedisCluster)操作redis集群 这里只是几个简单的demo,直接上代码吧,没啥好说的 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...

  3. redis3.0.0 集群安装详细步骤

    2019独角兽企业重金招聘Python工程师标准>>> redis3.0.0 集群安装详细步骤 博客分类: 缓存 Redis集群部署文档(centos6系统) (要让集群正常工作至少 ...

  4. python连接redis集群如何释放内存_python 连接 redis cluster 集群

    一. redis集群模式有多种, cluster模式只是其中的一种实现方式, 其原理请自行谷歌或者百度, 这里只举例如何使用Python操作 redis cluster 集群 二. python 连接 ...

  5. docker下,一行命令搭建elasticsearch6.5.0集群(带head插件和ik分词器)

    docker下,一行命令搭建elasticsearch6.5.0集群(带head插件和ik分词器) 2019年01月27日 21:06:12 博陵精骑 阅读数:794 标签: dockerelasti ...

  6. 淘淘商城第39讲——使用Spring来管理单机版Redis与集群版Redis

    我们知道Jedis在处理Redis的单机版和集群版时是完全不同的,有可能在开发的时候使用的是单机版,但是当项目上线后使用的则是集群版,这就需要能够方便的在单机版和集群版之间进行切换了.我们的做法便是定 ...

  7. hadoop-1.2.0集群安装与配置

    http://bbs.itcast.cn/thread-17487-1-1.html .硬件环境1.windows7旗舰版64位 2.VMwareWorkstationACE版6.0.2 3.Redh ...

  8. Hadoop2.2.0集群在RHEL6.2下的安装实战

    题记 本文介绍了一个Hadoop2.2.0集群的搭建过程,在2台4G内存的酷睿双核PC机上,使用VMWare WorkStation虚拟了4个RHEL6.2(1G内存.单核CPU.10G硬盘),总计用 ...

  9. hadoop(05)、使用Eclipse连接远程Hadoop集群

    2019独角兽企业重金招聘Python工程师标准>>> 在前面的文中我们分别搭建了单机和集群的Hadoop环境,今天我们将实践使用Eclispe开发工具安装 hadoop的开发插件, ...

最新文章

  1. 数据结构与算法(8-2)有序表查找(折半查找(二分查找)、插值查找)
  2. 为什么SpringBoot的 jar 可以直接运行?
  3. C语言进阶剖析第三课--浮点数的秘密
  4. boost::units::unscale相关的测试程序
  5. LA 2402 (枚举) Fishnet
  6. SocketServer模块,hmac模块验证client合法性
  7. 使用Python-Flask框架开发Web网站系列课程(一)构建项目
  8. jsoup html转义处理,jsoup解析网页出现转义符问题
  9. MySQLi学习笔记 :一 1. 数据库的基本概念 2. MySQL数据库软件 安装-- 卸载--. 配置 3. SQL
  10. OpenOffice源代码编译及安装
  11. 【笔记】Telink BDT EVK烧录时错误
  12. WordPress – wp-rocket插件的简单设置以及如何加速网站
  13. 科普:飞针测试机探针分类概要
  14. 使用FreeMarker导出Word文档(感觉是重要收获)
  15. 指法练习软件需求说明书
  16. css名词解析,小说CSS样式详解
  17. 路由器分出ITV与网络信号资料
  18. 结绳编程【简单计算】
  19. 橙汁(柠檬)戚风蛋糕配方和做法
  20. 国外优秀 Flex 网站源码模板与实例

热门文章

  1. 博图如何读取mysql数据_博途使用小结:从SQL中读取数据并给变量赋值
  2. python短期预测图_Python中利用长短期记忆模型LSTM进行时间序列预测分析
  3. 人工智能学习--文本识别实践-tesseract-ocr
  4. sptk安装,编译流程
  5. Python实现 logistic 回归算法
  6. 第四范式成为金融信创生态实验室首个AI合作伙伴
  7. Debug Pytorch: ValueError: Expected more than 1 value per channel when training, got input size tor
  8. python3精要(17)-迭代器iter,下一个next,列表解析
  9. 【面试招聘】非科班小白上岸的学习路线
  10. 【Python基础】50个令人大开眼界的 Matplotlib 可视化项目