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.pswp.cn/news/278715.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278715.shtml
英文地址,請注明出處:http://en.pswp.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組件 把儀表盤加入導航結構中 路由是導航的另一個名字。路由器就是從一個視圖導航到另…