解决测试redis集群时报"java.lang.NumberFormatException: For input string: "7003@17003..7002@17002"等异常
一、前言
关于redis5.0的集群模式下,通过客户端测试代码调试报"Exception in thread "main" java.lang.NumberFormatException: For input string: "7003@17003..7002@17002"等异常,详情日志如下>>
Exception in thread "main" java.lang.NumberFormatException: For input string: "7003@17003" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:492) at java.lang.Integer.valueOf(Integer.java:582) at redis.clients.util.ClusterNodeInformationParser.getHostAndPortFromNodeLine(ClusterNodeInformationParser.java:40) at redis.clients.util.ClusterNodeInformationParser.parse(ClusterNodeInformationParser.java:14) at redis.clients.jedis.JedisClusterInfoCache.discoverClusterNodesAndSlots(JedisClusterInfoCache.java:40) at redis.clients.jedis.JedisClusterConnectionHandler.initializeSlotsCache(JedisClusterConnectionHandler.java:50) at redis.clients.jedis.JedisClusterConnectionHandler.<init>(JedisClusterConnectionHandler.java:31) at redis.clients.jedis.JedisSlotBasedConnectionHandler.<init>(JedisSlotBasedConnectionHandler.java:17) at redis.clients.jedis.JedisCluster.<init>(JedisCluster.java:51) at redis.clients.jedis.JedisCluster.<init>(JedisCluster.java:35) at redis.clients.jedis.JedisCluster.<init>(JedisCluster.java:26) at redis.clients.jedis.JedisCluster.<init>(JedisCluster.java:30) at com.xwood.redis.cluster.JedisClusterTest.main(JedisClusterTest.java:23)
客户端代码示例如下:
package com.xwood.redis.cluster; import java.util.HashSet; import java.util.Set; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; public class JedisClusterTest { public static void main(String[] args) { Set<HostAndPort> nodes = new HashSet<HostAndPort>(); nodes.add(new HostAndPort("192.168.1.204", 7001)); nodes.add(new HostAndPort("192.168.1.204", 7002)); nodes.add(new HostAndPort("192.168.1.204", 7003)); nodes.add(new HostAndPort("192.168.1.204", 7004)); nodes.add(new HostAndPort("192.168.1.204", 7005)); nodes.add(new HostAndPort("192.168.1.204", 7006)); JedisCluster cluster = new JedisCluster(nodes); cluster.set("key424", "123456"); String result = cluster.get("key424"); System.out.println("cluster=========>"+result); cluster.close(); } }
二、解决方法
因jedis-2.7.0.jar版本问题,改为jedis-3.0.1.jar版本就能解决问题,因为redis5.0版本需要客户端相应高版本驱动包