java连接redis集群

向右看齐 2022-06-14 06:55 267阅读 0赞
  1. package redis;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
  5. import redis.clients.jedis.HostAndPort;
  6. import redis.clients.jedis.JedisCluster;
  7. public class ClusterDemo {
  8. private static JedisCluster jedisCluster=null;
  9. private static Set<HostAndPort> hostAndPorts=null;
  10. public static Set<HostAndPort> getHostAndPort(String hostAndPort){
  11. Set<HostAndPort> hap = new HashSet<HostAndPort>();
  12. String[] hosts = hostAndPort.split(",");
  13. String[] hs = null;
  14. for(String host:hosts){
  15. hs=host.split(":");
  16. hap.add(new HostAndPort(hs[0], Integer.parseInt(hs[1])));
  17. }
  18. return hap;
  19. }
  20. public static JedisCluster getJedisCluster(){
  21. GenericObjectPoolConfig gopc = new GenericObjectPoolConfig();
  22. gopc.setMaxTotal(32);
  23. gopc.setMaxIdle(4);
  24. gopc.setMaxWaitMillis(6000);
  25. hostAndPorts = getHostAndPort("192.168.10.250:6379");
  26. jedisCluster = new JedisCluster(hostAndPorts, 2000, 2000, 3,"redis123456",gopc);
  27. return jedisCluster;
  28. }
  29. public static void main(String[] args) {
  30. jedisCluster = getJedisCluster();
  31. System.out.println(jedisCluster.get("name"));
  32. }
  33. }

发表评论

表情:
评论列表 (有 0 条评论,267人围观)

还没有评论,来说两句吧...

相关阅读