spring集成 JedisCluster 连接 redis3.0 集群

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

spring集成 JedisCluster 连接 redis3.0 集群 博客分类: 缓存 spring

客户端采用最新的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

转载于:https://my.oschina.net/xiaominmin/blog/1599371

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

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

相关文章

html-盒子模型及pading和margin相关

margin: <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><style>* {margin: 0;padding: 0;}/*margin 外边距 元素与其他元素的距离&#xff08;边框以外的距离&#xff09;一…

火狐浏览器复制网页文字_从Firefox中的网页链接的多种“复制”格式中选择

火狐浏览器复制网页文字Tired of having to copy, paste, and then format links for use in your blogs, e-mails, or documents? Then see how easy it is to choose a click-and-go format that will save you a lot of time and effort with the CoLT extension for Firef…

vscode配置、使用git

文章目录一、下载、配置git二、vscode配置并使用git三、记住密码一、下载、配置git 1、git-win-x64点击下载后安装直接安装&#xff08;建议复制链接用迅雷等下载器下载&#xff0c;浏览器太慢&#xff0c;记住安装位置&#xff09;。 2、配置git环境变量&#xff1a; 右键 此…

BTrace功能

2019独角兽企业重金招聘Python工程师标准>>> BTrace功能 一、背景 在生产环境中可能经常遇到各种问题&#xff0c;定位问题需要获取程序运行时的数据信息&#xff0c;如方法参数、返回值、全局变量、堆栈信息等。为了获取这些数据信息&#xff0c;我们可以…

.NET(c#) 移动APP开发平台 - Smobiler(1)

原文&#xff1a;https://www.cnblogs.com/oudi/p/8288617.html 如果说基于.net的移动开发平台&#xff0c;目前比较流行的可能是xamarin了&#xff0c;不过除了这个&#xff0c;还有一个比xamarin更好用的国内的.net移动开发平台&#xff0c;smobiler&#xff0c;不用学习另外…

如何在Vizio电视上禁用运动平滑

Vizio维齐奥New Vizio TVs use motion smoothing to make the content you watch appear smoother. This looks good for some content, like sports, but can ruin the feel of movies and TV shows. 新的Vizio电视使用运动平滑来使您观看的内容显得更平滑。 这对于某些内容(例…

无服务器架构 - 从使用场景分析其6大特性

2019独角兽企业重金招聘Python工程师标准>>> 无服务器架构 - 从使用场景分析其6大特性 博客分类&#xff1a; 架构 首先我应该提到&#xff0c;“无服务器”技术肯定有服务器涉及。 我只是使用这个术语来描述这种方法和技术&#xff0c;它将任务处理和调度抽象为与…

ES6实用方法Object.assign、defineProperty、Symbol

文章目录1.合并对象 - Object.assign()介绍进阶注意用途2.定义对象 - Object.defineProperty(obj, prop, descriptor)3.新数据类型- Symbol定义应用1.合并对象 - Object.assign() 介绍 assign方法可以将多个对象&#xff08;字典&#xff09;&#xff0c;语法&#xff1a;Obj…

Enable Authentication on MongoDB

1、Connect to the server using the mongo shell mongo mongodb://localhost:270172、Create the user administrator Change to the admin database: use admindb.createUser({user: "Admin",pwd: "Admin123",roles: [ { role: "userAdminAnyDataba…

windows驱动程序编写_如何在Windows中回滚驱动程序

windows驱动程序编写Updating a driver on your PC doesn’t always work out well. Sometimes, they introduce bugs or simply don’t run as well as the version they replaced. Luckily, Windows makes it easy to roll back to a previous driver in Windows 10. Here’s…

运行tomcat报Exception in thread ContainerBackgroundProcessor[StandardEngine[Catalina]]

解决方法1&#xff1a; 手动设置MaxPermSize大小&#xff0c;如果是linux系统&#xff0c;修改TOMCAT_HOME/bin/catalina.sh&#xff0c;如果是windows系统&#xff0c;修改TOMCAT_HOME/bin/catalina.bat&#xff0c; 在“echo "Using CATALINA_BASE: $CATALINA_BASE&q…

new子类会先运行父类的构造函数

发现子类构造函数运行时&#xff0c;先运行了父类的构造函数。为什么呢? 原因&#xff1a;子类的所有构造函数中的第一行&#xff0c;其实都有一条隐身的语句super(); super(): 表示父类的构造函数&#xff0c;并会调用于参数相对应的父类中的构造函数。而super():是在调用父类…

在Windows 7中的Windows Media Player 12中快速预览歌曲

Do you ever wish you could quickly preview a song without having to play it? Today we look at a quick and easy way to do that in Windows Media Player 12. 您是否曾经希望无需播放就可以快速预览歌曲&#xff1f; 今天&#xff0c;我们探讨一种在Windows Media Play…

Vue.js中的8种组件间的通信方式;3个组件实例是前6种通信的实例,组件直接复制粘贴即可看到运行结果

文章目录一、$children / $parent二、props / $emit三、eventBus四、ref五、provide / reject六、$attrs / $listeners七、localStorage / sessionStorage八、Vuex实例以element ui为例。例子从上往下逐渐变复杂&#xff08;后面例子没有删前面的无用代码&#xff0c;有时间重新…

不可思议的混合模式 background-blend-mode

本文接前文&#xff1a;不可思议的混合模式 mix-blend-mode 。由于 mix-blend-mode 这个属性的强大&#xff0c;很多应用场景和动效的制作不断完善和被发掘出来&#xff0c;遂另起一文继续介绍一些使用 mix-blend-mode 制作的酷炫动画。 CSS3 新增了一个很有意思的属性 -- mix-…

adb错误 - INSTALL_FAILED_NO_MATCHING_ABIS

#背景 换组啦&#xff0c;去了UC国际浏览器&#xff0c;被拥抱变化了。还在熟悉阶段&#xff0c;尝试了下adb&#xff0c;然后就碰到了这个INSTALL_FAILED_NO_MATCHING_ABIS的坑。。。 #解决方法 INSTALL_FAILED_NO_MATCHING_ABIS is when you are trying to install an app th…

如何更改从Outlook发送的电子邮件中的“答复”地址

If you’re sending an email on behalf of someone else, you might want people to reply to that person instead of you. Microsoft Outlook gives you the option to choose a different default Reply address to cover this situation. 如果您要代表其他人发送电子邮件&…

visio自定义图形填充

选中图形&#xff0c;最上面一栏&#xff1a;开发工具-操作&#xff08;-组合-连接&#xff09;-拆分

Ansible 详解2-Playbook使用

aaa转载于:https://www.cnblogs.com/Presley-lpc/p/10107491.html

Angular2官网项目 (4)--路由

行动计划 把AppComponent变成应用程序的“壳”&#xff0c;它只处理导航 把现在由AppComponent关注的英雄们移到一个独立的GeneralComponent中 添加路由 创建一个新的DashboardComponent组件 把仪表盘加入导航结构中 路由是导航的另一个名字。路由器就是从一个视图导航到另…