整合步驟
1 配置application.properties
spring.redis.cluster.nodes=192.168.60.131:8000,192.168.60.131:8001,192.168.60.131:8002 spring.redis.maxTotal=200 spring.redis.maxIdle=8 spring.redis.minIdle=1
2 編寫配置類(完成初始化對象的過程)
4 @Bean 5 public JedisCluster getInstance(){ 6 //收集信息 7 Set<HostAndPort> infoSet=new HashSet<HostAndPort>(); 8 String[] node=nodes.split(",");//192.168.60.131:8000數組 9 for (String hostAndPort : node) { 10 //每次獲取192.168.60.131:8000 11 String host=hostAndPort.split(":")[0]; 12 Integer port=Integer.parseInt(hostAndPort.split(":")[1]); 13 infoSet.add(new HostAndPort(host, port));} 14 //配置對象 15 GenericObjectPoolConfig config=new GenericObjectPoolConfig(); 16 config.setMaxIdle(maxIdle); 17 config.setMaxTotal(maxTotal); 18 config.setMinIdle(minIdle); 19 JedisCluster cluster=new JedisCluster(infoSet,config); 20 return cluster;}
3 封裝底層api的類(RedisClusterService)
4 測試
需求:
• 存數據,從瀏覽器傳遞一些參數id,name
• 在代碼中生成key值,將value存儲在cluster集群
• 通過key值獲取集群的value,返回瀏覽器;
測試代碼端的高可用
將前面存儲的key值所在的節點宕機,再來訪問查詢的功能觀察結果(不能查到,能查到,過一段時間能查到)
jedisCluster代碼客戶端高可用
初始化過程
以set方法出現連接異常為例