redis学习之集群报错Node is not empty

合集下载

多线程测试redisson实现分布式锁出现org.redisson.RedissonShu。。。

多线程测试redisson实现分布式锁出现org.redisson.RedissonShu。。。

多线程测试redisson实现分布式锁出现org.redisson.RedissonShu。

多线程测试redisson实现分布式锁出现org.redisson.RedissonShutdownException: Redisson is shutdown。

原因:多线程还没跑完,主线程就跑完了。

主线程⾛完,关闭了资源。

redisson关闭,多线程操作redisson报错:Redisson is shutdown。

解决办法:主线程等待多线程跑完。

Thread.sleep(30000);。

1.Junit测试类:RedisDistributedLockTestpackage er.test.spring_redis;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import er.service.redis.SecondKillService;import er.service.redis.SecondKillServiceImp;import er.service.redis.SecondKillThread;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration({"classpath:applicationContext.xml"})public class RedisDistributedLockTest extends AbstractJUnit4SpringContextTests{@Autowiredprivate SecondKillService secondKillService;@Autowiredprivate SecondKillThread secondKillThread;/*** 模拟秒杀*/@Testpublic void secKill(){System.out.println("秒杀活动开始---");try {for(int i=0;i<2000;i++){new Thread(secondKillThread,"Thread" + i).start();}} catch (Exception e) {e.printStackTrace();}try {// 主线程需要等待线程执⾏完,否则,其他线程还没执⾏完,主线程就⾛完了,redisson会报错:Redisson is shutdownThread.sleep(30000);} catch (InterruptedException e1) {e1.printStackTrace();}System.out.println(SecondKillServiceImp.list);Set set = new HashSet();for(int i : SecondKillServiceImp.list){int count = 0;for(int j : SecondKillServiceImp.list){if(i == j){count = count + 1;}}if(count > 1){set.add(i);}}if(set != null && set.size() > 0){// Iterator it = set.iterator();// while(it.hasNext()){// System.out.println(it.next());// }System.out.println(set);}else{System.out.println("没有重复的记录!");}}}2.线程类:SecondKillThreadpackage er.service.redis;import org.springframework.beans.factory.annotation.Autowired;import ponent;@Componentpublic class SecondKillThread implements Runnable{@Autowiredprivate SecondKillService secondKillService;@Overridepublic void run() {secondKillService.seckill();}}3.秒杀业务接⼝:SecondKillServicepackage er.service.redis;public interface SecondKillService {public void seckill();}4.秒杀业务实现类:SecondKillServiceImppackage er.service.redis;import java.util.ArrayList;import java.util.List;import java.util.concurrent.TimeUnit;import ng3.StringUtils;import org.redisson.api.RLock;import org.redisson.api.RedissonClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import er.base.utils.redis.DistributedLockUtils;import er.base.utils.redis.DistributedLockUtils2;import er.base.utils.redis.redisson.RedissonConfig;@Servicepublic class SecondKillServiceImp implements SecondKillService{@Autowiredprivate RedissonClient redissonClient;private static int count = 2000;public static List<Integer> list = new ArrayList<>();@Overridepublic void seckill() {// count = count - 1;// list.add(count);// System.out.println(Thread.currentThread().getName() + "秒杀操作,singleRedis," + "剩余数量:" + count);// 可以防⽌重复提交的数据。

python redis-om使用原理 -回复

python redis-om使用原理 -回复

python redis-om使用原理-回复Redis是一个开源的键值存储系统,常用于缓存、队列和分布式存储等场景。

Redis官方提供了多种客户端库,其中之一就是redis-py。

而redis-py 中的redis-py-cluster模块则在redis-py的基础上提供了对Redis Cluster集群的支持。

redis-py-cluster使用python redis模块进行了封装,提供了更高层次的接口和功能,使得开发者可以更加便捷地与Redis Cluster进行交互。

本文将深入探讨redis-py-cluster的使用原理,并逐步分析它是如何实现对Redis Cluster的支持的。

一、Redis Cluster简介Redis Cluster是Redis的分布式解决方案之一,它将数据分布在多个节点上,提供了分布式缓存和存储的能力。

在Redis Cluster中,每个节点负责处理一部分数据,同时也负责将数据迁移到其他节点上,以实现数据的平衡和容错性。

Redis Cluster通过使用槽位(slot)来划分数据,一共有16384个槽位。

每个槽位可以存放一个键值对,每个节点负责处理一部分槽位。

当需要访问某个键值对时,Redis Cluster会根据键值对的哈希值确定该键值对所在的槽位,并将请求转发到负责该槽位的节点上。

二、redis-py-cluster的使用方式redis-py-cluster提供了RedisCluster这个类,开发者可以通过实例化RedisCluster对象来与Redis Cluster进行交互。

在实例化RedisCluster 对象时,需要传入一个节点列表和一些其他的配置。

节点列表用于指定Redis Cluster中的节点地址,配置则用于指定连接的一些参数和行为。

下面是一个示例代码,展示了如何实例化RedisCluster对象:pythonfrom rediscluster import RedisClusterstartup_nodes = [{'host': '127.0.0.1', 'port': 7000},{'host': '127.0.0.1', 'port': 7001},{'host': '127.0.0.1', 'port': 7002}]rc = RedisCluster(startup_nodes=startup_nodes,decode_responses=True)在上面的代码中,我们指定了一个包含了三个节点的节点列表,并将其传递给RedisCluster的startup_nodes参数。

lua脚本在redis集群中执行报错--Luascriptattemptedtoacces。。。

lua脚本在redis集群中执行报错--Luascriptattemptedtoacces。。。

lua脚本在redis集群中执⾏报错--Luascriptattemptedtoacces。

EVAL、EVALSHA命令Redis从2.6.0版本开始提供了eval命令,通过内置的Lua解释器,可以让⽤户执⾏⼀段Lua脚本并返回数据。

因为Redis单线程模型的特点,可以保证多个命令的原⼦性(因为最近的项⽬需要⽤到简单的分布式锁,所以会⽤到lua来释放锁)脚本性能1. Redis保证了脚本执⾏的原⼦性,所以在当前脚本没执⾏完之前,别的命令和脚本都是等待状态,所以⼀定要控制好脚本中的内容,防⽌出现需要消耗⼤量时间的内容(逻辑相对简单)。

带宽优化1. 为了避免每次执⾏都重复的将Lua脚本内容发送,Redis提供了evalsha命令,只需要将Lua脚本内容的SHA1校验和发送即可(evalsha6b1bf486c81ceb7edf3c093f4c48582e38c0e791 0)。

2. Lua脚本中的变量(动态数据)请使⽤KEYS和ARGV获取,如果把变量放在脚本中,必然会导致每次的脚本内容都不同(SHA1),Redis缓存⼤量⽆⽤或者⼀次性的脚本内容。

Redis Cluster 或阿⾥云Redis集群版使⽤注意事项Redis从3.0开始⽀持了Cluster功能,之前使⽤eval的时候可能没什么问题,但当切换成Cluster模式的时候,可能会出现⼀些问题:1. ERR Error running script (call to f_4a610f5543b3c3450220da7bd47825d3b6bffae8): @user_script:1: @user_script: 1: Lua script attempted to access a non localkey in a cluster node2. ERR eval/evalsha command keys must be in same slot(阿⾥云Redis集群版)上⾯的错误是因为Redis要求单个Lua脚本操作的key必须在同⼀个节点上,但是Cluster会将数据⾃动分布到不同的节点(虚拟的16384个slot,具体看官⽅⽂档),阿⾥云集群版的官⽹其实也有对应说明:在Redis集群版实例中,事务、脚本等命令要求所有的key必须在同⼀个slot中,如果不在同⼀个slot中将返回以下错误信息(:command keys must in same slot)CLUSTER KEYSLOT key的⽂档中提供了解决⽅法,你需要将把key中的⼀部分使⽤{}包起来,redis将通过{}中间的内容作为计算slot的key,类似key1{mykey}、key2{mykey}(如果你的key是“REDIS_LOCK_FORPR”,可以讲该key的⼀部分⽤{}括起来,例如“REDIS_LOCK_{FORPR}”)这样的都会存放到同⼀个slot中(缺点是不能平滑的过度⽼业务,需要修改原来使⽤的key,如果之前的key是统⼀管理的,也没那么⿇烦)// 部分代码private static final String DISTRIBUTE_LOCK_SCRIPT_UNLOCK_VAL = "if" +" redis.call('get', KEYS[1]) == ARGV[1]" +" then" +" return redis.call('del', KEYS[1])" +" else" +" return 0" +" end";Object eval = 0;List<String> keys = new ArrayList<>();keys.add(REDIS_LOCK_PREFIX + lockKey);List<String> argv = new ArrayList<>();argv.add(lockValue);try {// 这⾥不⽤指名有⼏个key,jedis内部会根据keys集合⼤⼩来获取eval = jedis.eval(DISTRIBUTE_LOCK_SCRIPT_UNLOCK_VAL, keys, argv);} catch (Exception e) {logger.error("解锁失败:" + e.getMessage());} finally {if (jedis != null) {jedis.close();}}集群环境中 lua 处理redis 集群中,会将键分配的不同的槽位上,然后分配到对应的机器上,当操作的键为⼀个的时候,⾃然没问题,但如果操作的键为多个的时候,集群如何知道这个操作落到那个机器呢?⽐如简单的mget命令,mget test1 test2 test3,还有我们上⾯执⾏脚本时候传⼊多个参数,带着这个问题我们继续。

Redis服务之Redis5集群相关命令说明

Redis服务之Redis5集群相关命令说明

Redis服务之Redis5集群相关命令说明 创建集群[root@node1 redis]# redis-cli -a admin --cluster create 192.168.0.41:6379 192.168.0.42:6379 192.168.0.43:6379 192.168.0.41:6380 192.168.0.42:6380 192.168.0.43:6380 --cluster-replicas 1 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Performing hash slots allocation on 6 nodes...Master[0] -> Slots 0 - 5460Master[1] -> Slots 5461 - 10922Master[2] -> Slots 10923 - 16383Adding replica 192.168.0.42:6380 to 192.168.0.41:6379Adding replica 192.168.0.43:6380 to 192.168.0.42:6379Adding replica 192.168.0.41:6380 to 192.168.0.43:6379M: 91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379slots:[0-5460] (5461 slots) masterM: 506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379slots:[5461-10922] (5462 slots) masterM: 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379slots:[10923-16383] (5461 slots) masterS: 646b85ae18a63f36866c64edfba374559252dd1b 192.168.0.41:6380replicates 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617S: eb23a3ff659ee22d03661cf312d5bf38b32686b5 192.168.0.42:6380replicates 91e34830f16e45110e37618540628587d5ecc7e2S: 6274aa87409a801311e580cb19d185c1d53bfa1f 192.168.0.43:6380replicates 506776f910a52e45987785199e4fd293956ac1c5Can I set the above configuration? (type 'yes' to accept): yes>>> Nodes configuration updated>>> Assign a different config epoch to each node>>> Sending CLUSTER MEET messages to join the clusterWaiting for the cluster to join.....>>> Performing Cluster Check (using node 192.168.0.41:6379)M: 91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379slots:[0-5460] (5461 slots) master1 additional replica(s)M: 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)S: eb23a3ff659ee22d03661cf312d5bf38b32686b5 192.168.0.42:6380slots: (0 slots) slavereplicates 91e34830f16e45110e37618540628587d5ecc7e2S: 6274aa87409a801311e580cb19d185c1d53bfa1f 192.168.0.43:6380slots: (0 slots) slavereplicates 506776f910a52e45987785199e4fd293956ac1c5M: 506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)S: 646b85ae18a63f36866c64edfba374559252dd1b 192.168.0.41:6380slots: (0 slots) slavereplicates 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.[root@node1 redis]# 提⽰:-a表⽰指定连接集群的密码,--cluster create 表⽰创建集群,后⾯跟要加⼊集群的各节点ip地址和端⼝(包括master和slave);--cluster replicas ⽤来指定集群master对应的副本(slave)数量;它会根据我们指定的数量,去创建master和slave,并把16384个槽位平均分配给对应的master节点,并且会⾃动关联master和slave的关系; 查看集群信息[root@node1 redis]# cd[root@node1 ~]# redis-cli --cluster info 192.168.0.41:6379[ERR] Node 192.168.0.41:6379 NOAUTH Authentication required.[root@node1 ~]# redis-cli -a admin --cluster info 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.0.41:6379 (91e34830...) -> 0 keys | 5461 slots | 1 slaves.192.168.0.43:6379 (0479a31d...) -> 0 keys | 5461 slots | 1 slaves.192.168.0.42:6379 (506776f9...) -> 0 keys | 5462 slots | 1 slaves.[OK] 0 keys in 3 masters.0.00 keys per slot on average.[root@node1 ~]# redis-cli -a admin --cluster check 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.0.41:6379 (91e34830...) -> 0 keys | 5461 slots | 1 slaves.192.168.0.43:6379 (0479a31d...) -> 0 keys | 5461 slots | 1 slaves.192.168.0.42:6379 (506776f9...) -> 0 keys | 5462 slots | 1 slaves.[OK] 0 keys in 3 masters.0.00 keys per slot on average.>>> Performing Cluster Check (using node 192.168.0.41:6379)M: 91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379slots:[0-5460] (5461 slots) master1 additional replica(s)M: 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)S: eb23a3ff659ee22d03661cf312d5bf38b32686b5 192.168.0.42:6380slots: (0 slots) slavereplicates 91e34830f16e45110e37618540628587d5ecc7e2S: 6274aa87409a801311e580cb19d185c1d53bfa1f 192.168.0.43:6380slots: (0 slots) slavereplicates 506776f910a52e45987785199e4fd293956ac1c5M: 506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)S: 646b85ae18a63f36866c64edfba374559252dd1b 192.168.0.41:6380slots: (0 slots) slavereplicates 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.[root@node1 ~]# 查看集群 node 对应关系[root@node1 ~]# redis-cli -a admin -c cluster nodesWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379@16379 master - 0 1592495162494 3 connected 10923-16383eb23a3ff659ee22d03661cf312d5bf38b32686b5 192.168.0.42:6380@16380 slave 91e34830f16e45110e37618540628587d5ecc7e2 0 1592495164579 5 connected91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379@16379 myself,master - 0 1592495158000 1 connected 0-54606274aa87409a801311e580cb19d185c1d53bfa1f 192.168.0.43:6380@16380 slave 506776f910a52e45987785199e4fd293956ac1c5 0 1592495159344 6 connected506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379@16379 master - 0 1592495163529 2 connected 5461-10922646b85ae18a63f36866c64edfba374559252dd1b 192.168.0.41:6380@16380 slave 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 0 1592495161445 4 connected[root@node1 ~]# 查看集群状态[root@node1 ~]# redis-cli -a admin -c cluster infoWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.cluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:6cluster_my_epoch:1cluster_stats_messages_ping_sent:159cluster_stats_messages_pong_sent:158cluster_stats_messages_sent:317cluster_stats_messages_ping_received:153cluster_stats_messages_pong_received:159cluster_stats_messages_meet_received:5cluster_stats_messages_received:317[root@node1 ~]# 提⽰:其实以上命令就是使⽤⾮交互式命令和redis交互,有点类似mysql中的-e选项,连接到对应redisserver上执⾏命令后返回,在mysql中⽤-e指定要执⾏的命令,在redis中⽤-c指定要执⾏的命令; 添加⼀个新节点到现有集群 实验环境说明 环境⽬录结构[root@node03 redis]# tree.├── 6379│└── etc│└── redis.conf├── 6380│└── etc│└── redis.conf├── 6381│└── etc│└── redis.conf├── 6382│└── etc│└── redis.conf├── bin│├── redis-benchmark│├── redis-check-aof│├── redis-check-rdb│├── redis-cli│├── redis-sentinel -> redis-server│└── redis-server├── dump_6379.rdb├── dump_6380.rdb├── logs│├── redis_6379.log│└── redis_6380.log├── redis-cluster-6379.conf├── redis-cluster-6380.conf└── run11 directories, 16 files[root@node03 redis]# 确认配置⽂件信息 提⽰:需要注意bind的地址必须能够和集群其他节点通信,端⼝不能和本机其他端⼝冲突,密码要和集群中的节点其他密码相同; 启动192.168.0.43:6081/6082 把192.168.0.43:6381/6382添加到现有集群[root@node1 ~]# redis-cli -a admin --cluster add-node 192.168.0.43:6381 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Adding node 192.168.0.43:6381 to cluster 192.168.0.41:6379>>> Performing Cluster Check (using node 192.168.0.41:6379)M: 91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379slots:[0-5460] (5461 slots) master1 additional replica(s)M: 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)S: eb23a3ff659ee22d03661cf312d5bf38b32686b5 192.168.0.42:6380slots: (0 slots) slavereplicates 91e34830f16e45110e37618540628587d5ecc7e2S: 6274aa87409a801311e580cb19d185c1d53bfa1f 192.168.0.43:6380slots: (0 slots) slavereplicates 506776f910a52e45987785199e4fd293956ac1c5M: 506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)S: 646b85ae18a63f36866c64edfba374559252dd1b 192.168.0.41:6380slots: (0 slots) slavereplicates 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.>>> Send CLUSTER MEET to node 192.168.0.43:6381 to make it join the cluster.[OK] New node added correctly.[root@node1 ~]# redis-cli -a admin --cluster add-node 192.168.0.43:6382 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Adding node 192.168.0.43:6382 to cluster 192.168.0.41:6379>>> Performing Cluster Check (using node 192.168.0.41:6379)M: 91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379slots:[0-5460] (5461 slots) master1 additional replica(s)M: 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)S: eb23a3ff659ee22d03661cf312d5bf38b32686b5 192.168.0.42:6380slots: (0 slots) slavereplicates 91e34830f16e45110e37618540628587d5ecc7e2S: 6274aa87409a801311e580cb19d185c1d53bfa1f 192.168.0.43:6380slots: (0 slots) slavereplicates 506776f910a52e45987785199e4fd293956ac1c5M: 95fa21156efabc1e07b07b0bdbe52bf8fe004b86 192.168.0.43:6381slots: (0 slots) masterM: 506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)S: 646b85ae18a63f36866c64edfba374559252dd1b 192.168.0.41:6380slots: (0 slots) slavereplicates 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.>>> Send CLUSTER MEET to node 192.168.0.43:6382 to make it join the cluster.[OK] New node added correctly.[root@node1 ~]# 查看现有集群信息[root@node1 ~]# redis-cli -a admin --cluster check 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.0.41:6379 (91e34830...) -> 0 keys | 5461 slots | 1 slaves.192.168.0.43:6379 (0479a31d...) -> 0 keys | 5461 slots | 1 slaves.192.168.0.43:6382 (43d6e13d...) -> 0 keys | 0 slots | 0 slaves.192.168.0.43:6381 (95fa2115...) -> 0 keys | 0 slots | 0 slaves.192.168.0.42:6379 (506776f9...) -> 0 keys | 5462 slots | 1 slaves.[OK] 0 keys in 5 masters.0.00 keys per slot on average.>>> Performing Cluster Check (using node 192.168.0.41:6379)M: 91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379slots:[0-5460] (5461 slots) master1 additional replica(s)M: 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)S: eb23a3ff659ee22d03661cf312d5bf38b32686b5 192.168.0.42:6380slots: (0 slots) slavereplicates 91e34830f16e45110e37618540628587d5ecc7e2S: 6274aa87409a801311e580cb19d185c1d53bfa1f 192.168.0.43:6380slots: (0 slots) slavereplicates 506776f910a52e45987785199e4fd293956ac1c5M: 43d6e13d06019af045e378ddc3e82b15b41ebdcd 192.168.0.43:6382slots: (0 slots) masterM: 95fa21156efabc1e07b07b0bdbe52bf8fe004b86 192.168.0.43:6381slots: (0 slots) masterM: 506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)S: 646b85ae18a63f36866c64edfba374559252dd1b 192.168.0.41:6380slots: (0 slots) slavereplicates 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.[root@node1 ~]# redis-cli -a admin -c cluster nodesWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379@16379 master - 0 1592498237487 3 connected 10923-16383eb23a3ff659ee22d03661cf312d5bf38b32686b5 192.168.0.42:6380@16380 slave 91e34830f16e45110e37618540628587d5ecc7e2 0 1592498238525 5 connected 91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379@16379 myself,master - 0 1592498215000 1 connected 0-54606274aa87409a801311e580cb19d185c1d53bfa1f 192.168.0.43:6380@16380 slave 506776f910a52e45987785199e4fd293956ac1c5 0 1592498234346 6 connected 43d6e13d06019af045e378ddc3e82b15b41ebdcd 192.168.0.43:6382@16382 master - 0 1592498236438 7 connected95fa21156efabc1e07b07b0bdbe52bf8fe004b86 192.168.0.43:6381@16381 master - 0 1592498235388 0 connected506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379@16379 master - 0 1592498239566 2 connected 5461-10922646b85ae18a63f36866c64edfba374559252dd1b 192.168.0.41:6380@16380 slave 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 0 1592498233609 4 connected [root@node1 ~]# 提⽰:可以看到对应集群上新加了两个节点,都是master⾓⾊,并且新加的两个节点都没有slave和slot; 分配槽位给192.168.0.43:6381[root@node1 ~]# redis-cli -a admin --cluster check 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.0.41:6379 (91e34830...) -> 0 keys | 5461 slots | 1 slaves.192.168.0.43:6379 (0479a31d...) -> 0 keys | 5461 slots | 1 slaves.192.168.0.43:6382 (43d6e13d...) -> 0 keys | 0 slots | 0 slaves.192.168.0.43:6381 (95fa2115...) -> 0 keys | 0 slots | 0 slaves.192.168.0.42:6379 (506776f9...) -> 0 keys | 5462 slots | 1 slaves.[OK] 0 keys in 5 masters.0.00 keys per slot on average.>>> Performing Cluster Check (using node 192.168.0.41:6379)M: 91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379slots:[0-5460] (5461 slots) master1 additional replica(s)M: 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)S: eb23a3ff659ee22d03661cf312d5bf38b32686b5 192.168.0.42:6380slots: (0 slots) slavereplicates 91e34830f16e45110e37618540628587d5ecc7e2S: 6274aa87409a801311e580cb19d185c1d53bfa1f 192.168.0.43:6380slots: (0 slots) slavereplicates 506776f910a52e45987785199e4fd293956ac1c5M: 43d6e13d06019af045e378ddc3e82b15b41ebdcd 192.168.0.43:6382slots: (0 slots) masterM: 95fa21156efabc1e07b07b0bdbe52bf8fe004b86 192.168.0.43:6381slots: (0 slots) masterM: 506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)S: 646b85ae18a63f36866c64edfba374559252dd1b 192.168.0.41:6380slots: (0 slots) slavereplicates 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.[root@node1 ~]# redis-cli -a admin --cluster reshard 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Performing Cluster Check (using node 192.168.0.41:6379)M: 91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379slots:[0-5460] (5461 slots) master1 additional replica(s)M: 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)S: eb23a3ff659ee22d03661cf312d5bf38b32686b5 192.168.0.42:6380slots: (0 slots) slavereplicates 91e34830f16e45110e37618540628587d5ecc7e2S: 6274aa87409a801311e580cb19d185c1d53bfa1f 192.168.0.43:6380slots: (0 slots) slavereplicates 506776f910a52e45987785199e4fd293956ac1c5M: 43d6e13d06019af045e378ddc3e82b15b41ebdcd 192.168.0.43:6382slots: (0 slots) masterM: 95fa21156efabc1e07b07b0bdbe52bf8fe004b86 192.168.0.43:6381slots: (0 slots) masterM: 506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)S: 646b85ae18a63f36866c64edfba374559252dd1b 192.168.0.41:6380slots: (0 slots) slavereplicates 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.How many slots do you want to move (from 1 to 16384)? 4096What is the receiving node ID? 95fa21156efabc1e07b07b0bdbe52bf8fe004b86Please enter all the source node IDs.Type 'all' to use all the nodes as source nodes for the hash slots.Type 'done' once you entered all the source nodes IDs.Source node #1: allReady to move 4096 slots.Source nodes:M: 91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379slots:[0-5460] (5461 slots) master1 additional replica(s)M: 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)M: 43d6e13d06019af045e378ddc3e82b15b41ebdcd 192.168.0.43:6382slots: (0 slots) masterM: 506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)Destination node:M: 95fa21156efabc1e07b07b0bdbe52bf8fe004b86 192.168.0.43:6381slots: (0 slots) masterResharding plan:Moving slot 5461 from 506776f910a52e45987785199e4fd293956ac1c5Moving slot 5462 from 506776f910a52e45987785199e4fd293956ac1c5Moving slot 5463 from 506776f910a52e45987785199e4fd293956ac1c5Moving slot 5464 from 506776f910a52e45987785199e4fd293956ac1c5Moving slot 5465 from 506776f910a52e45987785199e4fd293956ac1c5Moving slot 5466 from 506776f910a52e45987785199e4fd293956ac1c5Moving slot 5467 from 506776f910a52e45987785199e4fd293956ac1c5Moving slot 5468 from 506776f910a52e45987785199e4fd293956ac1c5Moving slot 5469 from 506776f910a52e45987785199e4fd293956ac1c5……省略部分内容……Moving slot 12284 from 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617Moving slot 12285 from 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617Moving slot 12286 from 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617Moving slot 12287 from 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617Do you want to proceed with the proposed reshard plan (yes/no)? yesMoving slot 5461 from 192.168.0.42:6379 to 192.168.0.43:6381:Moving slot 5462 from 192.168.0.42:6379 to 192.168.0.43:6381:Moving slot 5463 from 192.168.0.42:6379 to 192.168.0.43:6381:Moving slot 5464 from 192.168.0.42:6379 to 192.168.0.43:6381:……省略部分内容……Moving slot 12285 from 192.168.0.43:6379 to 192.168.0.43:6381:Moving slot 12286 from 192.168.0.43:6379 to 192.168.0.43:6381:Moving slot 12287 from 192.168.0.43:6379 to 192.168.0.43:6381:[root@node1 ~]# 提⽰:这个和redis-trib.rb命令使⽤没有什么区别; 查看新分配的槽位信息[root@node1 ~]# redis-cli -a admin --cluster check 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.0.41:6379 (91e34830...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.43:6379 (0479a31d...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.43:6382 (43d6e13d...) -> 0 keys | 0 slots | 0 slaves.192.168.0.43:6381 (95fa2115...) -> 0 keys | 4096 slots | 0 slaves.192.168.0.42:6379 (506776f9...) -> 0 keys | 4096 slots | 1 slaves.[OK] 0 keys in 5 masters.0.00 keys per slot on average.>>> Performing Cluster Check (using node 192.168.0.41:6379)M: 91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379slots:[1365-5460] (4096 slots) master1 additional replica(s)M: 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379slots:[12288-16383] (4096 slots) master1 additional replica(s)S: eb23a3ff659ee22d03661cf312d5bf38b32686b5 192.168.0.42:6380slots: (0 slots) slavereplicates 91e34830f16e45110e37618540628587d5ecc7e2S: 6274aa87409a801311e580cb19d185c1d53bfa1f 192.168.0.43:6380slots: (0 slots) slavereplicates 506776f910a52e45987785199e4fd293956ac1c5M: 43d6e13d06019af045e378ddc3e82b15b41ebdcd 192.168.0.43:6382slots: (0 slots) masterM: 95fa21156efabc1e07b07b0bdbe52bf8fe004b86 192.168.0.43:6381slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) masterM: 506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379slots:[6827-10922] (4096 slots) master1 additional replica(s)S: 646b85ae18a63f36866c64edfba374559252dd1b 192.168.0.41:6380slots: (0 slots) slavereplicates 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.[root@node1 ~]# 提⽰:可以看到现在集群有4个节点都分配了4096个slot,还有⼀个没有slot; 更改节点(192.168.0.43:6382)为新节点(192.168.0.43:6381)的slave[root@node1 ~]# redis-cli -a admin --cluster check 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.0.41:6379 (91e34830...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.43:6379 (0479a31d...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.43:6382 (43d6e13d...) -> 0 keys | 0 slots | 0 slaves.192.168.0.43:6381 (95fa2115...) -> 0 keys | 4096 slots | 0 slaves.192.168.0.42:6379 (506776f9...) -> 0 keys | 4096 slots | 1 slaves.[OK] 0 keys in 5 masters.0.00 keys per slot on average.>>> Performing Cluster Check (using node 192.168.0.41:6379)M: 91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379slots:[1365-5460] (4096 slots) master1 additional replica(s)M: 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379slots:[12288-16383] (4096 slots) master1 additional replica(s)S: eb23a3ff659ee22d03661cf312d5bf38b32686b5 192.168.0.42:6380slots: (0 slots) slavereplicates 91e34830f16e45110e37618540628587d5ecc7e2S: 6274aa87409a801311e580cb19d185c1d53bfa1f 192.168.0.43:6380slots: (0 slots) slavereplicates 506776f910a52e45987785199e4fd293956ac1c5M: 43d6e13d06019af045e378ddc3e82b15b41ebdcd 192.168.0.43:6382slots: (0 slots) masterM: 95fa21156efabc1e07b07b0bdbe52bf8fe004b86 192.168.0.43:6381slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) masterM: 506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379slots:[6827-10922] (4096 slots) master1 additional replica(s)S: 646b85ae18a63f36866c64edfba374559252dd1b 192.168.0.41:6380slots: (0 slots) slavereplicates 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.[root@node1 ~]# redis-cli -h 192.168.0.43 -p 6382 -a admin -c cluster replicate 95fa21156efabc1e07b07b0bdbe52bf8fe004b86Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.OK[root@node1 ~]# redis-cli -a admin --cluster check 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.0.41:6379 (91e34830...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.43:6379 (0479a31d...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.43:6381 (95fa2115...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.42:6379 (506776f9...) -> 0 keys | 4096 slots | 1 slaves.[OK] 0 keys in 4 masters.0.00 keys per slot on average.>>> Performing Cluster Check (using node 192.168.0.41:6379)M: 91e34830f16e45110e37618540628587d5ecc7e2 192.168.0.41:6379slots:[1365-5460] (4096 slots) master1 additional replica(s)M: 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617 192.168.0.43:6379slots:[12288-16383] (4096 slots) master1 additional replica(s)S: eb23a3ff659ee22d03661cf312d5bf38b32686b5 192.168.0.42:6380slots: (0 slots) slavereplicates 91e34830f16e45110e37618540628587d5ecc7e2S: 6274aa87409a801311e580cb19d185c1d53bfa1f 192.168.0.43:6380slots: (0 slots) slavereplicates 506776f910a52e45987785199e4fd293956ac1c5S: 43d6e13d06019af045e378ddc3e82b15b41ebdcd 192.168.0.43:6382slots: (0 slots) slavereplicates 95fa21156efabc1e07b07b0bdbe52bf8fe004b86M: 95fa21156efabc1e07b07b0bdbe52bf8fe004b86 192.168.0.43:6381slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master1 additional replica(s)M: 506776f910a52e45987785199e4fd293956ac1c5 192.168.0.42:6379slots:[6827-10922] (4096 slots) master1 additional replica(s)S: 646b85ae18a63f36866c64edfba374559252dd1b 192.168.0.41:6380slots: (0 slots) slavereplicates 0479a31d9a7bcb88a9fc1c4227f0f3e06ea3b617[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.[root@node1 ~]# 提⽰:我上⾯使⽤-c在shell上连接redis执⾏命令,这个和交互式执⾏命令⼀样的效果;从上⾯的信息可以看到192.168.0.43:6381已经有⼀个slave了,对应192.168.0.43:6382也没有出现到master列表中了; 验证:向集群中读写数据[root@node1 ~]# redis-cli -a adminWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.127.0.0.1:6379> KEYS *(empty list or set)127.0.0.1:6379> set aa bb(error) MOVED 1180 192.168.0.43:6381127.0.0.1:6379> set cc dd(error) MOVED 700 192.168.0.43:6381127.0.0.1:6379> set k v(error) MOVED 7629 192.168.0.42:6379127.0.0.1:6379> set kk vvOK127.0.0.1:6379> get kk"vv"127.0.0.1:6379> 提⽰:从上⾯的结果看,读写都可以正常执⾏,也能够正常的把对应的key进⾏计算,并告诉我们该到那个节点上执⾏对应的key读写操作; 验证:停掉集群中任意⼀台master,看看对应slave是否提升为master?[root@node2 redis]# redis-cli -a admin --cluster info 192.168.0.42Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.[ERR] Invalid arguments: you need to pass either a valid address (ie. 120.0.0.1:7000) or space separated IP and port (ie. 120.0.0.1 7000)[root@node2 redis]#[root@node2 redis]# redis-cli -a admin --cluster info 192.168.0.42:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.0.42:6379 (506776f9...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.43:6379 (0479a31d...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.41:6379 (91e34830...) -> 1 keys | 4096 slots | 1 slaves.192.168.0.43:6381 (95fa2115...) -> 0 keys | 4096 slots | 1 slaves.[OK] 1 keys in 4 masters.0.00 keys per slot on average.[root@node2 redis]# ps -ef |grep redisroot 20256 1 0 11:40 ? 00:00:15 redis-server 0.0.0.0:6379 [cluster]root 20258 1 0 11:40 ? 00:00:13 redis-server 0.0.0.0:6380 [cluster]root 20318 1926 0 13:04 pts/1 00:00:00 grep --color=auto redis[root@node2 redis]# kill -9 20256[root@node2 redis]# redis-cli -a admin --cluster info 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.Could not connect to Redis at 192.168.0.42:6379: Connection refused*** WARNING: 192.168.0.43:6380 claims to be slave of unknown node ID 506776f910a52e45987785199e4fd293956ac1c5.192.168.0.41:6379 (91e34830...) -> 1 keys | 4096 slots | 1 slaves.192.168.0.43:6379 (0479a31d...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.43:6381 (95fa2115...) -> 0 keys | 4096 slots | 1 slaves.[OK] 1 keys in 3 masters.0.00 keys per slot on average.[root@node2 redis]# redis-cli -a admin --cluster info 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.Could not connect to Redis at 192.168.0.42:6379: Connection refused192.168.0.41:6379 (91e34830...) -> 1 keys | 4096 slots | 1 slaves.192.168.0.43:6379 (0479a31d...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.43:6380 (6274aa87...) -> 0 keys | 4096 slots | 0 slaves.192.168.0.43:6381 (95fa2115...) -> 0 keys | 4096 slots | 1 slaves.[OK] 1 keys in 4 masters.0.00 keys per slot on average.[root@node2 redis]# 提⽰:可以看到对应slave是能够被提升为master的; 验证:把对应宕机的节点启动起来,看看是否⾃动降级为slave呢?[root@node2 redis]# redis-cli -a admin --cluster info 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.Could not connect to Redis at 192.168.0.42:6379: Connection refused192.168.0.41:6379 (91e34830...) -> 1 keys | 4096 slots | 1 slaves.192.168.0.43:6379 (0479a31d...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.43:6380 (6274aa87...) -> 0 keys | 4096 slots | 0 slaves.192.168.0.43:6381 (95fa2115...) -> 0 keys | 4096 slots | 1 slaves.[OK] 1 keys in 4 masters.0.00 keys per slot on average.[root@node2 redis]# redis-server /usr/local/redis/6379/etc/redis.conf[root@node2 redis]# redis-cli -a admin --cluster info 192.168.0.41:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.0.41:6379 (91e34830...) -> 1 keys | 4096 slots | 1 slaves.192.168.0.43:6379 (0479a31d...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.43:6380 (6274aa87...) -> 0 keys | 4096 slots | 1 slaves.192.168.0.43:6381 (95fa2115...) -> 0 keys | 4096 slots | 1 slaves.[OK] 1 keys in 4 masters.0.00 keys per slot on average.[root@node2 redis]# redis-cli -a admin -h 192.168.0.42 -p 6379 -c info replicationWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# Replicationrole:slavemaster_host:192.168.0.43master_port:6380master_link_status:upmaster_last_io_seconds_ago:3master_sync_in_progress:0slave_repl_offset:6594slave_priority:100slave_read_only:1connected_slaves:0master_replid:7b45a2b847aba7e40e3a6b7f39d4511fa2a62b6amaster_replid2:0000000000000000000000000000000000000000master_repl_offset:6594second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:6497repl_backlog_histlen:98[root@node2 redis]#。

Redis集群的关闭与重启操作

Redis集群的关闭与重启操作
主从复制优缺点:
优点:
支持主从复制,主机会自动将数据同步到从机,可以进行读写分离 为了分载Master的读操作压力,Slave服务器可以为客户端提供只读操作的服务,写服务仍然必须由Master来完成 Slave同样可以接受其它Slaves的连接和同步请求,这样可以有效的分载Master的同步压力。 Master Server是以非阻塞的方式为Slaves提供服务。所以在Master-Slave同步期间,客户端仍然可以提交查询或修改请求。 Slave Server同样是以非阻塞的方式完成数据同步。在同步期间,如果有客户端提交查询请求,Redis则返回同步之前的数据
root 12587 9737 0 17:41 pts/7 00:00:00 grep --color=auto redis
(2)集群连接
[root@master bin]# ./redis-cli -p 7001 -a 123456 -h 192.168.230.21 -a 123456 -c Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.230.21:7001> DBSIZE (integer) 2 192.168.230.21:7001> keys * 1) "aa" 2) "ss" 192.168.230.21:7001> set str STR -> Redirected to slot [6928] located at 192.168.230.21:7002 OK 192.168.230.21:7002>

jediscluster maxattempts 默认值 -回复

jediscluster maxattempts 默认值 -回复

jediscluster maxattempts 默认值-回复JedisCluster是一个Java的分布式缓存客户端,它提供了对Redis集群的操作方法。

在使用JedisCluster时,有一个名为maxattempts的参数,默认情况下,它的值是5。

本文将详细介绍maxattempts的作用、默认值以及一步一步解析它的含义。

首先,让我们了解一下Redis集群的工作原理。

Redis集群是通过将数据分布在多个节点上来实现高可用性和扩展性的。

每个节点负责存储数据的一部分,并共同组成一个完整的集群。

当一个Redis节点无法提供服务时,其他健康的节点将接管它的工作,确保系统的连续性。

JedisCluster是一个与Redis集群通信的客户端,它用于执行各种操作,如存储、读取和删除数据等。

在每次执行命令时,JedisCluster会自动将键值对映射到相应的Redis节点上,并使用集群间的内部通信协议进行交互。

然而,由于网络或其他问题,某个命令可能无法成功执行。

为了解决此类问题,JedisCluster提供了maxattempts参数,它决定了在命令执行失败时的重试次数。

默认情况下,maxattempts的值为5。

这意味着在命令执行失败后,JedisCluster将尝试最多5次重新执行命令。

如果这5次都失败了,JedisCluster将抛出一个异常。

这种机制可以确保在命令执行失败时,系统有机会进行多次尝试,提高命令的成功率。

接下来,我们将详细解析maxattempts的作用。

假设我们将maxattempts 设置为3,并执行一个get命令来读取一个键的值。

如果读取失败,JedisCluster将尝试3次重新执行命令,直到成功为止。

如果第一次、第二次和第三次尝试仍然失败,JedisCluster将抛出一个异常,提示无法读取键的值。

例如,我们执行以下代码:JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes); jedisCluster.set("key", "value");try {String result = jedisCluster.get("key");System.out.println(result);} catch (Exception e) {System.out.println("Failed to retrieve value: " +e.getMessage());}在上述代码中,我们使用JedisCluster从Redis集群中获取名为"key"的键的值。

springboot集成redis报错-ClassNotFoundException:or。。。

springboot集成redis报错-ClassNotFoundException:or。。。

springboot集成redis报错-ClassNotFoundException:or。

当使⽤Springboot 2.0以上版本集成redis的时候遇到报错信息如下:Application run failedorg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisController': Unsatisfied dependency expressed through field'redisService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisService': Unsatisfied dependency expressed through field 'redisTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name'redisTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'redisTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]: Factory method 'redisConnectionFactory' threw exception; nested exception is ng.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~ [spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at mbda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:743) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:390) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]at com.chm.springbootRedis.SpringbootRedisApplication.main(SpringbootRedisApplication.java:10) [classes/:na]Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisService': Unsatisfied dependency expressed through field 'redisTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'redisTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]: Bean instantiation via factory method failed; nested exception isorg.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]: Factory method'redisConnectionFactory' threw exception; nested exception is ng.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfigat org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~ [spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at mbda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1251) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~ [spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]... 19 common frames omittedCaused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisTemplate' defined in class path resource[org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'redisTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource[org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]: Bean instantiation via factory method failed; nested exception isorg.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]: Factory method'redisConnectionFactory' threw exception; nested exception is ng.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfigat org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:509) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321) ~ [spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at mbda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1251) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]... 32 common frames omittedCaused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource[org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]: Bean instantiation via factory method failed; nested exception isorg.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]: Factory method'redisConnectionFactory' threw exception; nested exception is ng.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfigat org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:607) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at mbda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1251) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]... 45 common frames omittedCaused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]: Factorymethod 'redisConnectionFactory' threw exception; nested exception is ng.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfigat org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]... 59 common frames omittedCaused by: ng.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfigat org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration$LettucePoolingClientConfigurationBuilder.<init> (LettucePoolingClientConfiguration.java:91) ~[spring-data-redis-2.1.10.RELEASE.jar:2.1.10.RELEASE]at org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration.builder(LettucePoolingClientConfiguration.java:50) ~[spring-data-redis-2.1.10.RELEASE.jar:2.1.10.RELEASE]at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$PoolBuilderFactory.createBuilder(LettuceConnectionConfiguration.java:141) ~[spring-boot-autoconfigure-2.1.7.RELEASE.jar:2.1.7.RELEASE]at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration.createBuilder(LettuceConnectionConfiguration.java:104) ~[spring-boot-autoconfigure-2.1.7.RELEASE.jar:2.1.7.RELEASE]at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration.getLettuceClientConfiguration(LettuceConnectionConfiguration.java:90) ~[spring-boot-autoconfigure-2.1.7.RELEASE.jar:2.1.7.RELEASE]at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration.redisConnectionFactory(LettuceConnectionConfiguration.java:74) ~[spring-boot-autoconfigure-2.1.7.RELEASE.jar:2.1.7.RELEASE]at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$$EnhancerBySpringCGLIB$$b070fd4c.CGLIB$redisConnectionFactory$0(<generated>)~[spring-boot-autoconfigure-2.1.7.RELEASE.jar:2.1.7.RELEASE]atorg.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$$EnhancerBySpringCGLIB$$b070fd4c$$FastClassBySpringCGLIB$$271c8d68.invoke(<generated>) ~[spring-boot-autoconfigure-2.1.7.RELEASE.jar:2.1.7.RELEASE]at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$$EnhancerBySpringCGLIB$$b070fd4c.redisConnectionFactory(<generated>) ~[spring-boot-autoconfigure-2.1.7.RELEASE.jar:2.1.7.RELEASE]at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211]at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_211]at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_211]at ng.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_211]at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]... 60 common frames omittedCaused by: ng.ClassNotFoundException: mons.pool2.impl.GenericObjectPoolConfigat .URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_211]at ng.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_211]at uncher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_211]at ng.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_211]... 76 common frames omitted看提⽰信息是GenericObjectPoolConfig类⽆法找到。

redis学习之集群报错Nodeisnotempty

redis学习之集群报错Nodeisnotempty

redis‎学习之集群‎报错Nod‎e is not empty‎遇到的问题‎及解决办法‎在redi‎s.conf里‎b i nd 真机ip后‎,接着重新执‎行每个re‎d is.conf,最后再创建‎集群,但报错,如下图所示‎:图中报的错‎即:[ERR] Node 192.168.161.131:7000 is not empty‎. Eithe‎r the node alrea ‎d y knows‎other‎nodes‎(check‎with CLUST‎E R NODES‎)or conta‎i ns some key in datab‎a se 0.这就奇怪了‎,于是我又去‎检查了一下‎r e dis‎.conf,ip我确实‎改过来了想了一会发‎现这三个文‎件a ppe‎n donl‎y.aof dump.rdb nodes‎.conf是‎之前执行i‎p127.0.0.1时生成的‎,在我改为真‎机i p后在‎执行并没有‎生效。

这里解释一‎下d ump‎.rdb文件‎:dump.rdb是由‎R e dis‎服务器自动‎生成的默认情况下‎每隔一段时‎间r edi‎s服务器程‎序会自动对‎数据库做一‎次遍历,把内存快照‎写在一个叫‎做“dump.rdb”的文件里,这个持久化‎机制叫做S‎N APSH‎O T。

有了SNA‎P SHOT‎后,如果服务器‎宕机,重新启动r‎e dis服‎务器程序时‎r e dis‎会自动加载‎d u mp.rdb,将数据库状‎态恢复到上‎一次做SN‎A PSHO‎T时的状态‎。

知道原因后‎就好办了,解决办法:1)将每个节点‎下a of、rdb、nodes‎.conf本‎地备份文件‎删除;2)172.168.63.201:7001> flush‎d b #清空当前数‎据库(可省略)3)之后再执行‎脚本,成功执行;问题解决了‎之后就可以‎成功从ja‎v a客户端‎测试了:ps:这里大家不‎要这样测试‎,可以将其写‎在配置文件‎里,我这里是为‎了方便。

解决CollectionUtils.isNotEmpty()不存在的问题

解决CollectionUtils.isNotEmpty()不存在的问题

collectionUtils.isNotEmpty()用 法
源ห้องสมุดไป่ตู้奉上
public static boolean isEmpty(Collection coll) { return coll == null || coll.isEmpty();
}
其实不难理解 就是为了对集合判空
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
由于idea安装多了某个jar点击出现读条后闪退情况接下来通过本文给大家分享解决idea202012idea打不开的问题非常不错具有一定的参考借鉴价值感兴趣的朋友跟随小编一起看看吧
解决 CollectionUtils.isNotEmpty()不存在的问题
目录
CollectionUtils.isNotEmpty()不存在问题 依赖,如下
collectionUtils.isNotEmpty()用法 源码奉上
CollectionUtils.isNotEmpty()不 存 在 问 题
mons.collections.CollectionUtils的包必须是3.1以上
依赖,如下
<dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.2</version> </dependency>

详细分析Redis集群故障

详细分析Redis集群故障

详细分析Redis集群故障故障表象:业务层⾯显⽰提⽰查询redis失败集群组成:3主3从,每个节点的数据有8GB机器分布:在同⼀个机架中,xx.x.xxx.199xx.x.xxx.200xx.x.xxx.201redis-server进程状态:通过命令ps -eo pid,lstart | grep $pid,发现进程已经持续运⾏了3个⽉发⽣故障前集群的节点状态:xx.x.xxx.200:8371(bedab2c537fe94f8c0363ac4ae97d56832316e65) masterxx.x.xxx.199:8373(792020fe66c00ae56e27cd7a048ba6bb2b67adb6) slavexx.x.xxx.201:8375(5ab4f85306da6d633e4834b4d3327f45af02171b) masterxx.x.xxx.201:8372(826607654f5ec81c3756a4a21f357e644efe605a) slavexx.x.xxx.199:8370(462cadcb41e635d460425430d318f2fe464665c5) masterxx.x.xxx.200:8374(1238085b578390f3c8efa30824fd9a4baba10ddf) slave---------------------------------下⾯是⽇志分析--------------------------------------步1:主节点8371失去和从节点8373的连接:46590:M 09 Sep 18:57:51.379 # Connection with slave xx.x.xxx.199:8373 lost.步2:主节点8370/8375判定8371失联:42645:M 09 Sep 18:57:50.117 * Marking node bedab2c537fe94f8c0363ac4ae97d56832316e65 as failing (quorum reached).步3:从节点8372/8373/8374收到主节点8375说8371失联:46986:S 09 Sep 18:57:50.120 * FAIL message received from 5ab4f85306da6d633e4834b4d3327f45af02171b about bedab2c537fe94f8c0363ac4ae97d56832316e65步4:主节点8370/8375授权8373升级为主节点转移:42645:M 09 Sep 18:57:51.055 # Failover auth granted to 792020fe66c00ae56e27cd7a048ba6bb2b67adb6 for epoch 16步5:原主节点8371修改⾃⼰的配置,成为8373的从节点:46590:M 09 Sep 18:57:51.488 # Configuration change detected. Reconfiguring myself as a replica of792020fe66c00ae56e27cd7a048ba6bb2b67adb6步6:主节点8370/8375/8373明确8371失败状态:42645:M 09 Sep 18:57:51.522 * Clear FAIL state for node bedab2c537fe94f8c0363ac4ae97d56832316e65: master without slots is reachable again.步7:新从节点8371开始从新主节点8373,第⼀次全量同步数据:8373⽇志::4255:M 09 Sep 18:57:51.906 * Full resync requested by slave xx.x.xxx.200:83714255:M 09 Sep 18:57:51.906 * Starting BGSAVE for SYNC with target: disk4255:M 09 Sep 18:57:51.941 * Background saving started by pid 52308371⽇志::46590:S 09 Sep 18:57:51.948 * Full resync from master: d7751c4ebf1e63d3baebea1ed409e0e7243a4423:440721826993步8:主节点8370/8375判定8373(新主)失联:42645:M 09 Sep 18:58:00.320 * Marking node 792020fe66c00ae56e27cd7a048ba6bb2b67adb6 as failing (quorum reached).步9:主节点8370/8375判定8373(新主)恢复:60295:M 09 Sep 18:58:18.181 * Clear FAIL state for node 792020fe66c00ae56e27cd7a048ba6bb2b67adb6: is reachable again and nobody is serving its slots after some time.步10:主节点8373完成全量同步所需要的BGSAVE操作:5230:C 09 Sep 18:59:01.474 * DB saved on disk5230:C 09 Sep 18:59:01.491 * RDB: 7112 MB of memory used by copy-on-write4255:M 09 Sep 18:59:01.877 * Background saving terminated with success步11:从节点8371开始从主节点8373接收到数据:46590:S 09 Sep 18:59:02.263 * MASTER <-> SLAVE sync: receiving 2657606930 bytes from master步12:主节点8373发现从节点8371对output buffer作了限制:4255:M 09 Sep 19:00:19.014 # Client id=14259015 addr=xx.x.xxx.200:21772 fd=844 name= age=148 idle=148 flags=S db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=16349 oll=4103 omem=95944066 events=rw cmd=psync scheduled to be closed ASAP for overcoming of output buffer limits.4255:M 09 Sep 19:00:19.015 # Connection with slave xx.x.xxx.200:8371 lost.步13:从节点8371从主节点8373同步数据失败,连接断了,第⼀次全量同步失败:46590:S 09 Sep 19:00:19.018 # I/O error trying to sync with MASTER: connection lost46590:S 09 Sep 19:00:20.102 * Connecting to MASTER xx.x.xxx.199:837346590:S 09 Sep 19:00:20.102 * MASTER <-> SLAVE sync started步14:从节点8371重新开始同步,连接失败,主节点8373的连接数满了:46590:S 09 Sep 19:00:21.103 * Connecting to MASTER xx.x.xxx.199:837346590:S 09 Sep 19:00:21.103 * MASTER <-> SLAVE sync started46590:S 09 Sep 19:00:21.104 * Non blocking connect for SYNC fired the event.46590:S 09 Sep 19:00:21.104 # Error reply to PING from master: '-ERR max number of clients reached'步15:从节点8371重新连上主节点8373,第⼆次开始全量同步:8371⽇志:46590:S 09 Sep 19:00:49.175 * Connecting to MASTER xx.x.xxx.199:837346590:S 09 Sep 19:00:49.175 * MASTER <-> SLAVE sync started46590:S 09 Sep 19:00:49.175 * Non blocking connect for SYNC fired the event.46590:S 09 Sep 19:00:49.176 * Master replied to PING, replication can continue...46590:S 09 Sep 19:00:49.179 * Partial resynchronization not possible (no cached master)46590:S 09 Sep 19:00:49.501 * Full resync from master: d7751c4ebf1e63d3baebea1ed409e0e7243a4423:440780763454 8373⽇志:4255:M 09 Sep 19:00:49.176 * Slave xx.x.xxx.200:8371 asks for synchronization4255:M 09 Sep 19:00:49.176 * Full resync requested by slave xx.x.xxx.200:83714255:M 09 Sep 19:00:49.176 * Starting BGSAVE for SYNC with target: disk4255:M 09 Sep 19:00:49.498 * Background saving started by pid 1841318413:C 09 Sep 19:01:52.466 * DB saved on disk18413:C 09 Sep 19:01:52.620 * RDB: 2124 MB of memory used by copy-on-write4255:M 09 Sep 19:01:53.186 * Background saving terminated with success步16:从节点8371同步数据成功,开始加载经内存:46590:S 09 Sep 19:01:53.190 * MASTER <-> SLAVE sync: receiving 2637183250 bytes from master46590:S 09 Sep 19:04:51.485 * MASTER <-> SLAVE sync: Flushing old data46590:S 09 Sep 19:05:58.695 * MASTER <-> SLAVE sync: Loading DB in memory步17:集群恢复正常:42645:M 09 Sep 19:05:58.786 * Clear FAIL state for node bedab2c537fe94f8c0363ac4ae97d56832316e65: slave is reachable again.步18:从节点8371同步数据成功,耗时7分钟:46590:S 09 Sep 19:08:19.303 * MASTER <-> SLAVE sync: Finished with success8371失联原因分析:由于⼏台机器在同⼀个机架,不太可能发⽣⽹络中断的情况,于是通过SLOWLOG GET命令查看了慢查询⽇志,发现有⼀个KEYS命令被执⾏了,耗时8.3秒,再查看集群节点超时设置,发现是5s(cluster-node-timeout 5000)出现节点失联的原因:客户端执⾏了耗时1条8.3s的命令,2016/9/9 18:57:43 开始执⾏KEYS命令2016/9/9 18:57:50 8371被判断失联(redis⽇志)2016/9/9 18:57:51 执⾏完KEYS命令总结来说,有以下⼏个问题:1.由于cluster-node-timeout设置⽐较短,慢查询KEYS导致了集群判断节点8371失联2.由于8371失联,导致8373升级为主,开始主从同步3.由于配置client-output-buffer-limit的限制,导致第⼀次全量同步失败了4.⼜由于PHP客户端的连接池有问题,疯狂连接服务器,产⽣了类似SYN攻击的效果5.第⼀次全量同步失败后,从节点重连主节点花了30秒(超过了最⼤连接数1w)关于client-output-buffer-limit参数:# The syntax of every client-output-buffer-limit directive is the following:## client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds>## A client is immediately disconnected once the hard limit is reached, or if# the soft limit is reached and remains reached for the specified number of# seconds (continuously).# So for instance if the hard limit is 32 megabytes and the soft limit is# 16 megabytes / 10 seconds, the client will get disconnected immediately# if the size of the output buffers reach 32 megabytes, but will also get# disconnected if the client reaches 16 megabytes and continuously overcomes# the limit for 10 seconds.## By default normal clients are not limited because they don't receive data# without asking (in a push way), but just after a request, so only# asynchronous clients may create a scenario where data is requested faster# than it can read.## Instead there is a default limit for pubsub and slave clients, since# subscribers and slaves receive data in a push fashion.## Both the hard or the soft limit can be disabled by setting them to zero.client-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60采取措施:1.单实例的切割到4G以下,否则发⽣主从切换会耗时很长2.调整client-output-buffer-limit参数,防⽌同步进⾏到⼀半失败3.调整cluster-node-timeout,不能少于15s4.禁⽌任何耗时超过cluster-node-timeout的慢查询,因为会导致主从切换5.修复客户端类似SYN攻击的疯狂连接⽅式总结以上就是本⽂关于详细分析Redis集群故障的全部内容,希望对⼤家有所帮助。

RedisCluster报错:ToomanyClusterredirections

RedisCluster报错:ToomanyClusterredirections

RedisCluster报错:ToomanyClusterredirections报错信息Spring Boot项⽬中访问redis cluster报错:redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Cluster redirections?(集群重定向过多)原因分析redis 绑定了多个ipeg:1. redis.conf ⽂件中bind 127.0.0.1 192.168.186.12. 在创建集群时host使⽤127.0.0.1 eg: redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1问题修复bind配置为主机ip(不要配置多个ip,也不要配置为回环地址127.0.0.1,配置为回环地址只有本机能访问,其他主机⽆法访问),重建集群eg:redis-cli --cluster create 192.168.186.1:7000 192.168.186.1:7001 192.168.186.1:7002 192.168.186.1:7003 192.168.186.1:7004 192.168.186.1:7005 --cluster-replicas 11. 所有节点redi.conf⽂件中bind配置为主机ip2. 删除所有节点中的nodes.conf 、rdb⽂件3. kill所有redis进程4. 重新启动所有redis实例5. 重建集群 redis-cli --cluster create 192.168.186.1:7000 192.168.186.1:7001 192.168.186.1:7002 192.168.186.1:7003192.168.186.1:7004 192.168.186.1:7005 --cluster-replicas 16. 重启项⽬即可正常访问如果重建集群时报错:ERR] Node 192.168.186.1:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.可参考。

【搭建rediscluster集群,JedisCluster带密码访问解决当中各种坑!】

【搭建rediscluster集群,JedisCluster带密码访问解决当中各种坑!】

【搭建rediscluster集群,JedisCluster带密码访问解决当中各种坑!】⼀.搭建redis单机本⽂搭建redis3.0版本,3.0主要增加了redis cluster集群功能。

2.解压源码:tar -zxvf redis-3.0.0.tar.gz3.编译源码:cd /usr/local/redis-3.0.0make4.安装到指定⽬录: cd /usr/local/redis-3.0.0make PREFIX=/usr/local/redis install5.进⼊源码⽬录,将redis.conf拷贝到安装路径:cd /usr/local/redismkdir confcp /usr/local/redis-3.0.0/redis.conf /usr/local/redis/bin6.修改redis.conf配置⽂件,以后端模式启动:daemonize yes7.启动redis:cd /usr/local/redis ./bin/redis-server ./redis.conf //在何处启动的server,⼀些配置⽂件就默认在该处⽣成(如果配置的相对路径)8.redis.conf配置⽂件主要配置:port 7001 //监听的端⼝# bind 127.0.0.1 //绑定ip,只允许该ip访问,不填默认为*,表⽰允许所有ip访问requirepass "你的密码" //开启密码loglevel debug //⽇志级别,开发模式尽量选⽤debuglogfile "redis.log" //⽇志⽂件路径,此处使⽤相对路径,将⽣成到/usr/local/redis下maxmemory 100000000 //允许最⼤内存占⽤100mappendonly yes //启⽤aofauto-aof-rewrite-percentage 80 //部署在同⼀机器的多个redis实例,建议把auto-aof-rewrite错开(可分别写80-100不等),防⽌瞬间fork,所有redis进程做rewrite,占⽤⼤量内存9.jedis连接redis单机:1<dependency>2<groupId>redis.clients</groupId>3<artifactId>jedis</artifactId>4<version>2.7.0</version>5</dependency>连接池整合spring:<!-- redis连接池(单例) --><bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close"><constructor-arg name="poolConfig" ref="jedisPoolConfig"/><constructor-arg name="host" value="${redis.host}"/><constructor-arg name="port" value="${redis.port}"/><constructor-arg name="timeout" value="${redis.timeout}"/><constructor-arg name="password" value="${redis.pass}"/></bean><!-- 连接池配置 --><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><!-- 最⼤连接数 --><property name="maxTotal" value="150"/><!-- 最⼤空闲连接数 --><property name="maxIdle" value="30"/><!-- 最⼩空闲连接数 --><property name="minIdle" value="10"/><!-- 获取连接时的最⼤等待毫秒数,⼩于零:阻塞不确定的时间,默认-1 --><property name="maxWaitMillis" value="3000"/><!-- 每次释放连接的最⼤数⽬ --><property name="numTestsPerEvictionRun" value="100"/><!-- 释放连接的扫描间隔(毫秒) --><property name="timeBetweenEvictionRunsMillis" value="3000"/><!-- 连接最⼩空闲时间 --><property name="minEvictableIdleTimeMillis" value="1800000"/><!-- 连接空闲多久后释放, 当空闲时间>该值且空闲连接>最⼤空闲连接数时直接释放 --><property name="softMinEvictableIdleTimeMillis" value="10000"/><!-- 在获取连接的时候检查有效性, 默认false --><property name="testOnBorrow" value="true"/><!-- 在空闲时检查有效性, 默认false --><property name="testWhileIdle" value="true"/><!-- 在归还给pool时,是否提前进⾏validate操作 --><property name="testOnReturn" value="true"/><!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --><property name="blockWhenExhausted" value="false"/></bean>1 @RunWith(SpringJUnit4ClassRunner.class) // 指定测试⽤例的运⾏器这⾥是指定了Junit42 @ContextConfiguration("classpath:spring/application*.xml")3public class RedisTest {4 @Autowired5private JedisPool pool;6 @Test7public void testJedisPool() {8 Jedis jedis = null;9 String name = null;10try {11 jedis = pool.getResource();12 jedis.set("testName", "RCL");13 name = jedis.get("testName");14 } catch (Exception ex) {15 ex.printStackTrace();16 } finally {17if (jedis != null) {18// 返回给池19 jedis.close();20 }21 Assert.assertEquals("RCL", name);22 }23 }10.如果连接不上,可查看是否防⽕墙没有将redis端⼝开放:/etc/sysconfig/iptables添加:-A INPUT -p tcp -m state --state NEW -m tcp --dport 7001 -j ACCEPT //7001即redis端⼝重启防⽕墙⼆、搭建redis集群1.安装ruby环境集群管理⼯具redis-trib.rb依赖ruby环境(1)安装ruby:yum install ruby yum install rubygems(2)安装ruby和redis的接⼝程序:拷贝redis-3.0.0.gem⾄/usr/local。

解决springboot2.x集成redis节点故障redisTemplate报错redi。。。

解决springboot2.x集成redis节点故障redisTemplate报错redi。。。

解决springboot2.x集成redis节点故障redisTemplate报错redi。

springboot2.x集成redis。

redis节点故障,集群状态ok的情况下,程序使⽤redisTemplate操作redis⼀直报错:Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out after 1 minute(s)解决⽅案:捕获redisTemplate操作的异常,然后重新初始化redisTemplate的连接⼯⼚connectionFactory代码如下:RedisService.javapackage com.harara;import com.harara.RedisConfig;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Primary;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import ponent;import java.io.Serializable;/*** @author: harara* @date: 2020-9-07 18:23* @description:* @version: 1.0*/@Slf4j@Componentpublic class RedisService {@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate RedisConfig redisConfig;/*** 更新redisTemplate :处理集群宕机恢复后程序不恢复问题* 重新初始化redisTemplate的连接⼯⼚connectionFactory*/private RedisTemplate refreshRedisTemplate(){LettuceConnectionFactory connectionFactory = redisConfig.connectionFactory();connectionFactory.afterPropertiesSet();redisTemplate.setConnectionFactory(connectionFactory);return redisTemplate;}/*** @param key* @return*/public Object get(final String key){ValueOperations<Serializable, Object> operations = null;try {operations= redisTemplate.opsForValue();return operations.get(key);}catch (Exception e){log.error("redis操作string get出现异常:{}",e.getMessage());operations = refreshRedisTemplate().opsForValue();return operations.get(key);}}}RedisConfig.javapackage com.harara;import cn.hutool.core.util.StrUtil;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Configuration;import org.springframework.core.annotation.Order;import org.springframework.core.env.MapPropertySource;import org.springframework.data.redis.connection.RedisClusterConfiguration;import org.springframework.data.redis.connection.RedisStandaloneConfiguration;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import java.util.HashMap;import java.util.Map;/*** redis 配置类* @author harara* @date 2020/9/7 11:34**/@Configuration@Order(value = 1)public class RedisConfig {//redis连接的database@Value("${spring.redis.database:0}")private int database;//集群节点配置@Value("${spring.redis.cluster.nodes:#{null}}")private String nodes;//集群最⼤连接转移数@Value("${spring.redis.cluster.max-redirects:3}")private int maxRedirects;//单节点情况下redis的ip@Value("${spring.redis.host:#{null}}")private String host;//单节点情况下redis的端⼝@Value("${spring.redis.port:#{null}}")private Integer port;//redis的连接密码@Value("${spring.redis.password:#{null}}")private String password;/*** 创建连接⼯⼚LettuceConnectionFactory* @return*/public LettuceConnectionFactory connectionFactory() {Map<String, Object> source = new HashMap<String, Object>();RedisClusterConfiguration redisClusterConfiguration;RedisStandaloneConfiguration redisStandaloneConfiguration;//集群模式if(nodes !=null){source.put("spring.redis.cluster.nodes", nodes);source.put("spring.redis.cluster.max-redirects", maxRedirects);redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));if(!StrUtil.isEmpty(password)){redisClusterConfiguration.setPassword(password);}//创建连接⼯⼚LettuceConnectionFactory lettuceConnectionFactory = newLettuceConnectionFactory(redisClusterConfiguration);return lettuceConnectionFactory;}else{//单机模式redisStandaloneConfiguration = new RedisStandaloneConfiguration(host,port);redisStandaloneConfiguration.setDatabase(database);if(!StrUtil.isEmpty(password)){redisStandaloneConfiguration.setPassword(password);}//创建连接⼯⼚LettuceConnectionFactory lettuceConnectionFactory = newLettuceConnectionFactory(redisStandaloneConfiguration);return lettuceConnectionFactory;}}}。

K8S线上集群排查,实测排查Node节点NotReady异常状态

K8S线上集群排查,实测排查Node节点NotReady异常状态

K8S线上集群排查,实测排查Node节点NotReady异常状态⼀,⽂章简述⼤家好,本篇是个⼈的第 2 篇⽂章。

是关于在之前项⽬中,k8s 线上集群中 Node 节点状态变成 NotReady 状态,导致整个 Node 节点中容器停⽌服务后的问题排查。

⽂章中所描述的是本⼈在项⽬中线上环境实际解决的,那除了如何解决该问题,更重要的是如何去排查这个问题的起因。

关于 Node 节点不可⽤的 NotReady 状态,当时也是花了挺久的时间去排查的。

⼆,Pod 状态在分析 NotReady 状态之前,我们⾸先需要了解在 k8s 中 Pod 的状态都有哪些。

并且每个状态都表⽰什么含义,不同状态是很直观的显⽰出当前 Pod 所处的创建信息。

为了避免⼤家对 Node 和 Pod 的概念混淆,先简单描述下两者之间的关系(引⽤⼀张 K8S 官⽅图)。

从图中很直观的显⽰出最外⾯就是 Node 节点,⽽⼀个 Node 节点中是可以运⾏多个 Pod 容器,再深⼊⼀层就是每个 Pod 容器可以运⾏多个实例 App 容器。

因此关于本篇⽂章所阐述的 Node 节点不可⽤,就会直接导致 Node 节点中所有的容器不可⽤。

毫⽆疑问,Node 节点是否健康,直接影响该节点下所有的实例容器的健康状态,直⾄影响整个 K8S 集群。

那么如何解决并排查 Node 节点的健康状态?不急,我们先来聊聊关于关于 Pod 的⽣命周期状态。

Pending:该阶段表⽰已经被 Kubernetes 所接受,但是容器还没有被创建,正在被 kube 进⾏资源调度。

1:图中数字 1 是表⽰在被 kube 资源调度成功后,开始进⾏容器的创建,但是在这个阶段是会出现容器创建失败的现象Waiting或ContainerCreating:这两个原因就在于容器创建过程中镜像拉取失败,或者⽹络错误容器的状态就会发⽣转变。

Running:该阶段表⽰容器已经正常运⾏。

Failed:Pod 中的容器是以⾮ 0 状态(⾮正常)状态退出的。

停止redis集群后清除数据重启,Redisson无法自动重连问题解决方法

停止redis集群后清除数据重启,Redisson无法自动重连问题解决方法

停⽌redis集群后清除数据重启,Redisson⽆法⾃动重连问题解决⽅法停⽌redis集群清除数据后重启⽆法⾃动重连问题解决⽅法问题重现步骤1、停⽌redis集群中的每个节点⽤命令停⽌逐个节点太⿇烦了,写了个shell脚本,shutdown.sh (-a 123456 是redis集群的连接密码)redis-cli -p 7001 -a 123456 shutdownredis-cli -p 7002 -a 123456 shutdownredis-cli -p 7003 -a 123456 shutdownredis-cli -p 7004 -a 123456 shutdownredis-cli -p 7005 -a 123456 shutdownredis-cli -p 7006 -a 123456 shutdown执⾏命令 ./shutdown.sh 停⽌redis服务2、清除每个节点产⽣的数据⽂件(nodes.conf appendonly.aof dump.rdb ) 执⾏脚本./moveRedis.sh 清除数据⽂件moveRedis.sh内容为cd redis7001rm -rf nodes.conf appendonly.aof dump.rdbcd ..cd redis7002rm -rf nodes.conf appendonly.aof dump.rdbcd ..cd redis7003rm -rf nodes.conf appendonly.aof dump.rdbcd ..cd redis7004rm -rf nodes.conf appendonly.aof dump.rdbcd ..cd redis7005rm -rf nodes.conf appendonly.aof dump.rdbcd ..cd redis7006rm -rf nodes.conf appendonly.aof dump.rdb3、启动redis服务 执⾏ ./startall.sh 逐个启动redis服务 startall.sh脚本内容为cd redis7001./redis-server redis.confcd ..cd redis7002./redis-server redis.confcd ..cd redis7003./redis-server redis.confcd ..cd redis7004./redis-server redis.confcd ..cd redis7005./redis-server redis.confcd ..cd redis7006./redis-server redis.conf4、创建集群进⼊redis-trib.rb所在⽬录执⾏如下命令创建集群./redis-trib.rb create --replicas 1 192.169.1.71:7001 192.169.1.71:7002 192.169.1.71:7003 192.169.1.71:7004 192.169.1.71:7005 192.169.1.71:7006程序报错以上重现过程程序都是启动着的,redis集群服务起来后redisson并没有⾃动重连,查看⽇志发现有如下报错org.redisson.client.RedisException: MOVED redirection loop detected. Node //192.169.2.238:9511 has further redirect to //192.169.2.238:9511at mandAsyncService.checkAttemptFuture(CommandAsyncService.java:865)at mandAsyncService$10.operationComplete(CommandAsyncService.java:673)at ty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:511)at ty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:504)at ty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:483)at ty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:424)at ty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:121)at org.redisson.misc.RedissonPromise.tryFailure(RedissonPromise.java:108)at mandData.tryFailure(CommandData.java:78)at mandDecoder.decode(CommandDecoder.java:313)at mandDecoder.decodeCommand(CommandDecoder.java:128)at mandDecoder.decode(CommandDecoder.java:108)at ty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:489) at ty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366)at ty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265)at ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)at ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)at ty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)at ty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)at ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)at ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)at ty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)at ty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)at ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)at ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)at ty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)at ty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)at ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)at ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)at ty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)at ty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434)at ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)at ty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)at ty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965)at ty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)at ty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:647)at ty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:582)at ty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:499)at ty.channel.nio.NioEventLoop.run(NioEventLoop.java:461)at ty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)at ty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)at ng.Thread.run(Thread.java:748)解决⽅案最开始怀疑是redisson版本的原因,原来是redisson-3.8.2,将版本提⾼⾄3.11.1依旧有这个问题然后猜测是不是和redis版本有关,将redis版本由3.2.12升⾄4.0.14后,问题依然存在既然和外部⽆关,那就只能从程序上优化了在使⽤到RedissonClient的⽅法中捕获异常,⼀旦出现异常,重新获取⼀次RedissonClient需要注意的是⼿动调⽤加了@Bean注解的⽅法⽆效,需要再写⼀个⽤于⼿动调⽤的获取RedissonClient的⽅法完整代码如下package com.xiaonian.middleware.redis;import com.xiaonian.util.StrUtil;import org.redisson.Redisson;import org.redisson.api.RedissonClient;import org.redisson.config.Config;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import ponent;/*** Redisson管理类* 连接redis2(缓存redis)* @author xiaonian* @date 2019/6/26 10:41* @version v2.0.0*/@Configuration@Componentpublic class RedissonManager {/**redis集群节点*/@Value(value = "${spring.redis.cluster.nodes:#{null}}")private String cluster;/**redis密码*/@Value("${spring.redis.password:#{null}}")private String password;@Value("${spring.redis.host:#{null}}")/**redis单机节点主机*/private String host;/**redis单机节点端⼝*/@Value("${spring.redis.host:#{null}}")private String port;/**最⼤连接数*/@Value("${redisson.pool.max.active:30}")private int MaxPoolSize;@Beanpublic RedissonClient getRedisson() {return loadRedisson();}public RedissonClient loadRedisson(){RedissonClient redisson = null;Config config = new Config();//单节点if(!StrUtil.isEmpty(host)){eSingleServer().setAddress("redis://"+host+":"+port).setPassword(password).setConnectionPoolSize(MaxPoolSize)//最⼩空闲连接.setConnectionMinimumIdleSize(0);redisson = Redisson.create(config);}else{//集群节点String[] nodes = cluster.split(",");//redisson版本是3.5,集群的ip前⾯要加上“redis://”,不然会报错,3.2版本可不加for(int i=0;i<nodes.length;i++){nodes[i] = "redis://"+nodes[i];}//这是⽤的集群servereClusterServers()//设置集群状态扫描时间2000.setScanInterval(2000).addNodeAddress(nodes).setPassword(password).setMasterConnectionPoolSize(MaxPoolSize)//最⼩空闲连接.setMasterConnectionMinimumIdleSize(0);redisson = Redisson.create(config);// System.out.println(config.);//可通过打印redisson.getConfig().toJSON().toString()来检测是否配置成功}return redisson;}public RedissonClient retryGetRedisson() {return loadRedisson();}}。

Redis学习笔记五:缓存常见问题和解决方案

Redis学习笔记五:缓存常见问题和解决方案

Redis学习笔记五:缓存常见问题和解决⽅案作者:原⽂地址:缓存击穿key的过期造成⾼并发访问数据库。

缓存击穿解决⽅案使⽤Redis set nx⽅法,加⼀把锁,只有这个⼈可以访问数据库,并把数据存⼊缓存,其他的流量就可以从缓存读取数据了。

get keyset key nx3-1 ok -> DB拿数据3-2 false sleep -> 1会导致的问题是:死锁⽐如去DB拿数据的那个请求挂了,会导致死锁。

解决办法:设置锁的过期时间但是如果没挂,锁到期了发⽣超时了。

可以通过多线程来解决,⼀个线程取DB,另外⼀个线程监控是否取回来,更新锁时间。

以上问题会导致客户端的复杂度增加。

引⼊zk。

缓存穿透查询⼀个根本不存在的key缓存穿透解决⽅案使⽤布隆过滤器,有两种实现⽅案:客户端包含布隆过滤器客户端只实现算法Redis集成布隆过滤器有缺点,只能增加,不能删除可以改成缓存雪崩设置缓存时采⽤了相同的过期时间,导致缓存在某⼀时刻同时失效,请求全部转发到DB,DB瞬时压⼒过重雪崩。

缓存雪崩解决⽅案如果对于过期时间要求不⾼,可以把原有的失效时间基础上增加⼀个随机值,⽐如1-5分钟随机。

如果对过期时间要求⾮常⾼,必须在某个时间过期,这个时候就必须要强依赖击穿⽅案,或者在业务层做⼀些延迟处理。

更多的说明见:主从DB与cache⼀致性Redis如何淘汰过期的keys参考:Redis keys过期有两种⽅式:被动和主动⽅式。

当⼀些客户端尝试访问它时,key会被发现并主动的过期。

当然,这样是不够的,因为有些过期的keys,永远不会访问他们。

⽆论如何,这些keys应该过期,所以定时随机测试设置keys的过期时间。

所有这些过期的keys将会从密钥空间删除。

具体就是Redis每秒10次做的事情:测试随机的20个keys进⾏相关过期检测。

删除所有已经过期的keys。

如果有多于25%的keys过期,重复步奏1.这是⼀个平凡的概率算法,基本上的假设是,我们的样本是这个密钥控件,并且我们不断重复过期检测,直到过期的keys的百分百低于25%,这意味着,在任何给定的时刻,最多会清除1/4的过期keys。

isempty用法 -回复

isempty用法 -回复

isempty用法-回复“isempty”是一个在编程语言中广泛使用的函数或方法。

根据不同的编程语言中的实现,isempty可以用来检查某个数据结构或变量是否为空。

在本文中,我们将以此为主题,逐步回答相关问题。

1. isempty的定义和作用是什么?isempty是一个函数或方法,用于检查某个特定的数据结构或变量是否为空。

具体而言,它可以判断一个字符串、数组、列表、集合、字典、队列等类型的数据是否为空。

2. 为什么需要使用isempty?在编程中,我们经常需要检查一个变量是否为空,这样可以避免在处理该变量时出现错误。

例如,在处理用户输入时,我们需要首先检查输入字符串是否为空,以免出现无效的业务逻辑或错误的处理。

3. isempty的用法和语法是什么?实际操作中,isempty的具体用法和语法因编程语言而异。

下面以几种常用编程语言为例进行说明。

- 在Python中,我们可以使用len函数来判断字符串、列表、元组、字典等数据类型的长度是否为0。

例如:my_list = [1, 2, 3]if len(my_list) == 0:print("List is empty")- 在Java中,我们可以使用判断条件来检查数组、字符串等对象是否为空。

例如:String myString = "Hello World";if (myString.isEmpty()) {System.out.println("String is empty");}- 在JavaScript中,我们可以使用判断条件来检查字符串、数组等对象是否为空。

例如:let myArray = [];if (myArray.length === 0) {console.log("Array is empty");}在这些例子中,我们使用了不同语法和方法,但都能正确判断相关对象是否为空。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

redis学习之集群报错Node is not empty 遇到的问题及解决办法
在redis.conf里bind 真机ip后,接着重新执行每个redis.conf,最后再创建集群,但报错,如下图所示:
图中报的错即:
[ERR] Node 192.168.161.131:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
这就奇怪了,于是我又去检查了一下redis.conf,ip我确实改过来了
想了一会发现这三个文件appendonly.aof dump.rdb nodes.conf是之前执行ip127.0.0.1时生成的,在我改为真机ip后在执行并没有生效。

这里解释一下dump.rdb文件:
dump.rdb是由Redis服务器自动生成的默认情况下每隔一段时间redis 服务器程序会自动对数据库做一次遍历,把内存快照写在一个叫做“dump.rdb”
的文件里,这个持久化机制叫做SNAPSHOT。

有了SNAPSHOT后,如果服务器宕机,重新启动redis服务器程序时redis会自动加载dump.rdb,将数据库状态恢复到上一次做SNAPSHOT时的状态。

知道原因后就好办了,解决办法:
1)将每个节点下aof、rdb、nodes.conf本地备份文件删除;
2)172.168.63.201:7001> flushdb #清空当前数据库(可省略)
3)之后再执行脚本,成功执行;
问题解决了之后就可以成功从java客户端测试了:
ps:
这里大家不要这样测试,可以将其写在配置文件里,我这里是为了方便。

相关文档
最新文档