jedis集群方式 连接redis集群

2022年4月26日 2017点热度 0人点赞 0条评论
	public static JedisCluster getJedisCluster(Set jedisClusterNodes,String password) {
		// 创建连接池配置并设置值
		JedisPoolConfig config = new JedisPoolConfig();
		//最大活动对象数
		config.setMaxTotal(2);
		//最大能够保持idel状态的对象数
		config.setMaxIdle(2);
		//最小能够保持idel状态的对象数
		config.setMinIdle(2);
		//当池内没有返回对象时,最大等待时间
		config.setMaxWaitMillis(10_000);
		//在获取连接的时候检查有效性, 默认false
		//当调用borrow Object方法时,是否进行有效性检查
		config.setTestOnBorrow(false);
		config.setTestOnReturn(false);
		//在空闲时检查有效性, 默认false
		config.setTestWhileIdle(true);
		config.setNumTestsPerEvictionRun(1);
		config.setTestOnCreate(false);
		//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
		config.setBlockWhenExhausted(true);
		
		
		int connectionTimeout=5000;
		int soTimeout=5000;
	    int maxAttempts=10;
	    
		return new JedisCluster(jedisClusterNodes,connectionTimeout,soTimeout,maxAttempts,password, config);   
	}

错误原因:没有设置密码

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required.
at redis.clients.jedis.Protocol.processError(Protocol.java:139)
at redis.clients.jedis.Protocol.process(Protocol.java:173)
at redis.clients.jedis.Protocol.read(Protocol.java:227)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:352)
at redis.clients.jedis.Connection.getUnflushedObjectMultiBulkReply(Connection.java:314)
at redis.clients.jedis.Connection.getObjectMultiBulkReply(Connection.java:319)
at redis.clients.jedis.Jedis.clusterSlots(Jedis.java:4019)
at redis.clients.jedis.JedisClusterInfoCache.discoverClusterNodesAndSlots(JedisClusterInfoCache.java:147)
at redis.clients.jedis.JedisClusterConnectionHandler.initializeSlotsCache(JedisClusterConnectionHandler.java:127)
at redis.clients.jedis.JedisClusterConnectionHandler.<init>(JedisClusterConnectionHandler.java:100)
at redis.clients.jedis.JedisClusterConnectionHandler.<init>(JedisClusterConnectionHandler.java:79)
at redis.clients.jedis.JedisClusterConnectionHandler.<init>(JedisClusterConnectionHandler.java:40)
at redis.clients.jedis.JedisClusterConnectionHandler.<init>(JedisClusterConnectionHandler.java:34)
at redis.clients.jedis.JedisSlotBasedConnectionHandler.<init>(JedisSlotBasedConnectionHandler.java:41)
at redis.clients.jedis.BinaryJedisCluster.<init>(BinaryJedisCluster.java:81)
at redis.clients.jedis.BinaryJedisCluster.<init>(BinaryJedisCluster.java:74)
at redis.clients.jedis.BinaryJedisCluster.<init>(BinaryJedisCluster.java:68)
at redis.clients.jedis.BinaryJedisCluster.<init>(BinaryJedisCluster.java:63)
at redis.clients.jedis.BinaryJedisCluster.<init>(BinaryJedisCluster.java:58)
at redis.clients.jedis.JedisCluster.<init>(JedisCluster.java:146)
at redis.clients.jedis.JedisCluster.<init>(JedisCluster.java:136)

 

错误原因:scan集群不支持扫描。

解决办法

List list=new ArrayList<>();
		ScanParams params=new ScanParams();
		params.match(keyPrefix);
		params.count(100);
	

	    jc.getClusterNodes().values().stream().forEach(pool -> {
	        boolean done = false;
	    	String cursor="0";
	        try (Jedis jedisNode = pool.getResource()) {
	          while (!done) {
	            ScanResult resp = jedisNode.scan(cursor, params);
	            List result = resp.getResult();
	            if(CollectionUtils.isNotEmpty(result)) {
	            	list.addAll(result);
	            	for (String key : list) {
		            	System.out.println(key+"-"+jc.get(key));
					}
	            }
	            cursor = resp.getCursor();
	            if ("0".equals(cursor)) {
	              done = true;
	            }
	          }
	        }
	      });

//		while(true) {
//			ScanResult scanResult = jc.scan(cursor, params, key);
//			List result = scanResult.getResult();
//			if(CollectionUtils.isNotEmpty(result)) {
//				list.addAll(result);
//			}
//			cursor = scanResult.getCursor();
//			if("0".equals(cursor)) {
//				break;
//			}
//		}



Exception in thread "main" java.lang.IllegalArgumentException: JedisCluster only supports SCAN commands with MATCH patterns containing hash-tags ( curly-brackets enclosed strings )
at redis.clients.jedis.JedisCluster.scan(JedisCluster.java:1800)

错误原因:Moved表示使用的是Redis集群。而Jedis不是集群模式。

解决办法
将连接对象从Jedis换成JedisCluster。或者将配置文件中的信息由单结点模式变成集群模式。

Exception in thread "main" redis.clients.jedis.exceptions.JedisMovedDataException: MOVED 4139 10.87.20.120:7001
at redis.clients.jedis.Protocol.processError(Protocol.java:121)
at redis.clients.jedis.Protocol.process(Protocol.java:173)
at redis.clients.jedis.Protocol.read(Protocol.java:227)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:352)
at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:289)
at redis.clients.jedis.Connection.getBulkReply(Connection.java:279)
at redis.clients.jedis.Jedis.get(Jedis.java:259)
at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1625)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)


连接耗尽 Could not get a resource since the pool is exhausted

解决办法:config.setBlockWhenExhausted(true);

config.setTestOnBorrow(false);

config.setTestOnReturn(false);

Exception in thread "main" redis.clients.jedis.exceptions.JedisExhaustedPoolException: Could not get a resource since the pool is exhausted
at redis.clients.jedis.util.Pool.getResource(Pool.java:78)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:370)
at redis.clients.jedis.JedisSlotBasedConnectionHandler.getConnectionFromSlot(JedisSlotBasedConnectionHandler.java:141)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:118)
at redis.clients.jedis.JedisClusterCommand.run(JedisClusterCommand.java:45)
at redis.clients.jedis.JedisCluster.get(JedisCluster.java:313)
at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1625)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:446)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:356)
at redis.clients.jedis.util.Pool.getResource(Pool.java:75)
... 10 more

管理员

这个人很懒,什么都没留下

文章评论