产品动态
公告
config-file.yaml配置文件,设置 Redis 服务器的地址、端口、密码,以及 timeout (3000ms)等信息,根据配置信息,建立 Redis 的连接。package org.example;// 导入Redisson相关的包import org.redisson.Redisson;import org.redisson.api.RAtomicLong;import org.redisson.api.RedissonClient;import org.redisson.config.Config;import org.redisson.connection.balancer.RoundRobinLoadBalancer;// 导入Java IO相关的包import java.io.File;import java.io.IOException;public class Main {public static void main(String[] args) throws IOException {// 从YAML文件中读取Redisson的配置信息Config config = Config.fromYAML(new File("/data/home/test**/redissontest/src/main/resources/config-file.yaml"));RedissonClient redisson = Redisson.create(config);// 初始化一个计数器变量int i = 0;// 循环1000000次while(i++ < 1000000){// 获取一个原子长整型对象,键值为当前计数器的值RAtomicLong atomicLong = redisson.getAtomicLong(Integer.toString(i));// 对原子长整型对象的值进行原子减1操作atomicLong.getAndDecrement();}// 关闭Redisson客户端redisson.shutdown();}}
sudo iptables -A INPUT -s 10.0.16.6 -p tcp --sport 6379 -m conntrack --ctstate NEW,ESTABLISHED -j DROP

sudo iptables -D INPUT -s 10.0.16.6 -p tcp --sport 6379 -m conntrack --ctstate NEW,ESTABLISHED -j DROP


Unable to send PING command over channel,查看 PingConnectionHandler 代码,如下所示。可以看出,PING 重连探测失败报错后,执行ctx.channel().close(),关闭了异常的 channel,并且执行connection.getRedisClient().getConfig().getFailedNodeDetector().onPingFailed();检测当前连接客户端的状态。if(connection.getUsage() == 0 && future != null && (future.cancel(false) || cause(future) != null)) {Throwable cause = cause(future);if (!(cause instanceof RedisRetryException)) {if (!future.isCancelled()) {log.error("Unable to send PING command over channel: {}", ctx.channel(), cause);}log.debug("channel: {} closed due to PING response timeout set in {} ms", ctx.channel(), config.getPingConnectionInterval());ctx.channel().close();connection.getRedisClient().getConfig().getFailedNodeDetector().onPingFailed();} else {connection.getRedisClient().getConfig().getFailedNodeDetector().onPingSuccessful();sendPing(ctx);}} else {connection.getRedisClient().getConfig().getFailedNodeDetector().onPingSuccessful();sendPing(ctx);}
RedisConnection connection = RedisConnection.getFrom(ctx.channel());查看创建连接的构造函数 RedisConnection(),如下所示。该函数创建更新 channel 属性和记录最后使用连接的时间,但并没有切换新 Channel 尝试连接的行为。public <C> RedisConnection(RedisClient redisClient, Channel channel, CompletableFuture<C> connectionPromise) {this.redisClient = redisClient;this.connectionPromise = connectionPromise;updateChannel(channel);lastUsageTime = System.nanoTime();LOG.debug("Connection created {}", redisClient);}// updateChannel 更新 Channel的属性public void updateChannel(Channel channel) {if (channel == null) {throw new NullPointerException();}this.channel = channel;channel.attr(CONNECTION).set(this);}
ERROR org.redisson.cluster.ClusterConnectionManager - Can't update cluster stateorg.redisson.client.RedisTimeoutException: Command execution timeout for command: (CLUSTER NODES), params: [], Redis client: [addr=redis://10.0.16.7:6379] at org.redisson.client.RedisConnection.lambda$async$0(RedisConnection.java:256) at io.netty.util.HashedWheelTimer$HashedWheelTimeout.run(HashedWheelTimer.java:715) at io.netty.util.concurrent.ImmediateExecutor.execute(ImmediateExecutor.java:34) at io.netty.util.HashedWheelTimer$HashedWheelTimeout.expire(HashedWheelTimer.java:703) at io.netty.util.HashedWheelTimer$HashedWheelBucket.expireTimeouts(HashedWheelTimer.java:790) at io.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:503) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:829)
ClusterConnectionManager.java 中可以定位到更新集群的报错信息片段。可以看出,在抛出Can't update cluster state异常后,仅检查了集群状态的变更,而并没有从连接池中拿取新连接进行操作的行为,直接返回。private void checkClusterState(ClusterServersConfig cfg, Iterator<RedisURI> iterator, AtomicReference<Throwable> lastException) {if (!iterator.hasNext()) {if (lastException.get() != null) {log.error("Can't update cluster state", lastException.get());}// 检查集群状态的变更scheduleClusterChangeCheck(cfg);return;}......}
package org.example;import org.redisson.Redisson;import org.redisson.api.RAtomicLong;import org.redisson.api.RedissonClient;import org.redisson.config.Config;import org.redisson.connection.balancer.RoundRobinLoadBalancer;import java.io.File;import java.io.IOException;public class Main {public static void main(String[] args) throws IOException {boolean connected = false;RedissonClient redisson = null;while (!connected) {try {Config config = Config.fromYAML(new File("/data/home/sharmaxia/redissontest/src/main/resources/config-file.yaml"));redisson = Redisson.create(config);connected = true;} catch (Exception e) {e.printStackTrace();try {Thread.sleep(1000);} catch (InterruptedException ex) {Thread.currentThread().interrupt(); //主动打断当前thread重新从连接池中拿连接}}}int i = 0;while (i++ < 1000000) {try {RAtomicLong atomicLong = redisson.getAtomicLong(Integer.toString(i));atomicLong.getAndDecrement();} catch (Exception e) {e.printStackTrace();try {Thread.sleep(1000);} catch (InterruptedException ex) {Thread.currentThread().interrupt(); //主动打断当前thread重新从连接池中拿连接}i--;}}redisson.shutdown();}}


文档反馈