JedisCluster 鏈接redis集群


先貼代碼:

<!-- redis客戶端 -->
<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.8.2</version>
</dependency>

//相關代碼如下:

JedisPoolConfig config = new JedisPoolConfig();
config =new JedisPoolConfig();
       config.setMaxTotal(60000);//設置最大連接數  
       config.setMaxIdle(1000); //設置最大空閑數 
       config.setMaxWaitMillis(3000);//設置超時時間  
       config.setTestOnBorrow(true);


// 集群結點
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("192.168.246.128", Integer.parseInt("7001")));
jedisClusterNode.add(new HostAndPort("192.168.246.128", Integer.parseInt("7002")));
jedisClusterNode.add(new HostAndPort("192.168.246.128", Integer.parseInt("7003")));
jedisClusterNode.add(new HostAndPort("192.168.246.128", Integer.parseInt("7004")));
jedisClusterNode.add(new HostAndPort("192.168.246.128", Integer.parseInt("7005")));
jedisClusterNode.add(new HostAndPort("192.168.246.128", Integer.parseInt("7006")));

JedisCluster jc = new JedisCluster(jedisClusterNode, config);
//JedisCluster jc = new JedisCluster(jedisClusterNode);
jc.set("name", "zhangsan");
String value = jc.get("name");
System.out.println(value);

 注意事項

redis配置文件中  bind配置注釋掉  或者bind 0.0.0.0  表示所有的服務器都可以連接

如果配置為  bind  127.0.0.1  會報Could not get a resource from the pool 錯誤

創建集群命令為./redis-trib.rb create --replicas 1 45.78.76.17:7001 45.78.76.17:7002 45.78.76.17:7003 45.78.76.17:7004 45.78.76.17:7005 45.78.76.17:7006 45.78.76.17:7007 45.78.76.17:7008

其中  45.78.76.17為本機IP


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM