Redis(四):Spring + JedisCluster操作Redis(集群)

1.maven依赖:

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

2.增加spring 配置

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

3.增加connect-redis.properties 配置文件

这里配置了6个节点

address1=192.168.221.128:7000  
address2=192.168.221.128:7001  
address3=192.168.221.128:7002  
address4=192.168.221.129:7003  
address5=192.168.221.129:7004  
address6=192.168.221.129:7005  

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;  

转载于:https://www.cnblogs.com/yifanSJ/p/9105958.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/266981.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

js对象数组转java对象数组对象数组对象数组对象,前台js数组json字符串,后台json转为对象数组的具体实现...

$("#savaUserSet").click(function(){var JSONArr[];$("i[nameeventName]").each(function() {//获取所有name属性为eventName的i标签,并遍历if(!($(this).hasClass("active"))){var eventCode$(this).attr("id");var eventName$(this…

matlab绘制烟花,[原创]利用MATLAB燃放烟花(礼花)

function firework% 烟花烟花满天飞% CopyRight&#xff1a;xiezhh(谢中华)% 2011.6.25OldHandle findobj( Type, figure, Tag, FireWork ) ;if ishandle(OldHandle)close(OldHandle) ;end% 图形窗口初始化fig figure(units,normalized,position,[0.1 0.1 0.8 0.8],...menuba…

33 -jQuery 属性操作,文档操作(未完成)

转载于:https://www.cnblogs.com/venicid/p/9110130.html

GNU C - 关于8086的内存访问机制以及内存对齐(memory alignment)

接着前面的文章&#xff0c;这篇文章就来说说menory alignment -- 内存对齐. 一、为什么需要内存对齐&#xff1f; 无论做什么事情&#xff0c;我都习惯性的问自己&#xff1a;为什么我要去做这件事情&#xff1f; 是啊&#xff0c;这可能也是个大家都会去想的问题&#xff0c;…

mysql权限说法正确的是,【多选题】下面关于修改 MySQL 配置的说法中,正确的是...

参考答案如下【单选题】4.正常枕先露分娩时&#xff0c;多选的说仰伸发生于()39、题下【单选题】人们常常用来判断一种活动是不是游戏的一项外部指标是( )面关【多选题】S-S法阶段2训练内容包括于修【判断题】痉挛性睑内翻多发生于下睑。配置【判断题】萤火虫不仅成虫会发光,其…

读取exchange邮件的未读数(转载)

protected void Page_Load(object sender, EventArgs e) { Response.Write("administrator的未读邮件数是&#xff1a;" UnReadCount("administratordomainname")); } int UnReadCount(string userMailAddress) {…

嵌入式Linux下Qt的中文显示

一般情况下&#xff0c;嵌入式Qt界面需要中文显示&#xff0c;下面总结自己在项目中用到的可行的办法 1&#xff0c;下载一种中文简体字体&#xff0c;比如我用的是”方正准圆简体“&#xff0c;把字体文件放在ARM开发板系统的Qt字库中&#xff0c;即/usr/lib/fonts下 2&#x…

Robot Framework + Selenium library + IEDriver环境搭建

转载&#xff1a;https://www.cnblogs.com/Ming8006/p/4998492.html#c.d 目录&#xff1a; 1 安装文件准备2 Robot框架结构3 环境搭建 3.1 安装Python 3.2 安装Robot Framework 3.3 安装wxPython 3.4 安装RIDE 3.5 安装Selenium2Library 3.6 安装IEDriverServer 1 安装文…

php静态地图api,静态图API | 百度地图API SDK

百度地图静态图API&#xff0c;可实现将百度地图以图片形式嵌入到您的网页中。您只需发送HTTP请求访问百度地图静态图服务&#xff0c;便可在网页上以图片形式显示您的地图。静态图API较之JavaScript API载入的动态网站&#xff0c;既能满足基本的地图信息浏览&#xff0c;又能…

[XMOVE自主设计的体感方案] XMove Studio管理系统(二)应用开发API简要介绍

一. XMove的开放式应用开发框架简介 XMove4.0以开放式的结构满足扩展性的要求。所有无线协议&#xff0c;底层算法和控制逻辑全部上移到PC端。节点只根据接受的控制逻辑返回传感器数据。新的架构使得开发新应用非常方便。 本节将主要介绍XMove应用开发API及其使用。 二. 注册新…

搭建服务器Apache+PHP+MySql需要注意的问题

参见https://www.cnblogs.com/bytebull/p/7927542.html 一、软件下载的都是用zip压缩文件&#xff0c;三个软件均需手动配置&#xff0c;若想省事&#xff0c;可考虑phpstudy&#xff0c;一键安装。 我的服务器文件目录&#xff1a; 二、安装PHP时需注意&#xff0c;新版本的PH…

php行为日志,利用ThinkPHP的行为扩展做系统日志

1&#xff1a;模块配置&#xff1a;return array(action_end > array(Admin\\Behaviors\\LogBehavior),);2&#xff1a;数据库建表&#xff1a;create table logs(id int(11) primary key auto_increment,url char(30) not null,operator int(11) not null,description char…

nagios搭建(一):nagios3.2的搭建

此文章的大多地方采用的是elain的博客内容&#xff1a;http://elain.blog.51cto.com/3339379/711549小部分内容是自己的从别的文章总结过来的&#xff0c;已经试验过了1.需要的软件包&#xff1a;nagios-3.2.0.tar.gz nagios的主软件包nagios-cn-3.2.0.tar.…

0530JavaScript基础2

常用内置对象 所谓内置对象就是ECMAScript提供出来的一些对象&#xff0c;我们知道对象都是有相应的属性和方法 数组Array&#xff08;部分相当于列表&#xff09; 1.数组的创建方式 var colors [red,color,yellow]; 使用构造函数&#xff08;后面会讲&#xff09;的方式创建 …

html php获取post数据格式,html - php文件无法得到POST过来的数据

php文件无法得到POST过来的数据&#xff0c;通过$_SERVER得到如下Array([HOSTNAME] > localhost.localdomain[PATH] > /usr/local/bin:/usr/bin:/bin[TMP] > /tmp[TMPDIR] > /tmp[TEMP] > /tmp[OSTYPE] >[MACHTYPE] >[MALLOC_CHECK_] > 2[USER] > w…

.net mvc 超过了最大请求长度 限制文件上传大小

在我们的项目中遇到"超过了最大请求长度"如下图所示,是因为IIS默认请求长度4M,当请求长度大于这个值的时候报错,下面是解决方案. 解决方案:修改web.config文件 1、注意在mvc中有两个web.config文件&#xff0c;如下图&#xff0c;一个位于Views下&#xff0c;是用来控…

ubuntu php 树莓派,树莓派3 安装ROS环境(ubuntu mate 16.04+kinetic)

1.刷系统下载ubuntu-mate-16.04镜像下载页面 https://ubuntu-mate.org/download/点击下载64位ubuntu-mate-16.04-desktop-armhf-raspberry-pi2.安装镜像树莓派3-系统安装-Windows下利用Win32DiskImager进行系统安装3.设置网络连接网线&#xff0c;使用DHCP自动获取IP地址和DNS。…

Android AlertDialog学习

1. 有两个按钮的对话框 Builder buildernew AlertDialog.Builder(AlertDialogActivity.this); builder.setIcon(android.R.drawable.btn_plus); builder.setTitle("btnplus"); builder.setMessage("去不去&#xff1f;"); builder.setPositiveButton("…

分布式之缓存击穿

什么是缓存击穿 在谈论缓存击穿之前&#xff0c;我们先来回忆下从缓存中加载数据的逻辑&#xff0c;如下图所示 因此&#xff0c;如果黑客每次故意查询一个在缓存内必然不存在的数据&#xff0c;导致每次请求都要去存储层去查询&#xff0c;这样缓存就失去了意义。如果在大流量…

php为什么都不想去二次开发,php学习误区:不要盲目的去读程序

最近感到很郁闷&#xff0c;细数了一下自己读过的程序&#xff0c;真的是多之又多&#xff0c;比如比较流行的有&#xff1a;织梦系统(dedecms) php168phpcms ,论坛类的 discuz phpwind还有现在用的wordpress程序自己都读过&#xff0c;但是朋友问你一个关于这几个系统的几个函…