2024年2月--Mac电脑安装RedisCluster集群(亲验成功)(非Docker模式,Docker模式的缺陷在于Springboot连不上Redis集群节点跳转时的Docker内网IP)
前提:按照本文的指令照做之前,先在Mac电脑安装一个默认端口为6379的单节点redis server,保证后面的redis-server和redis-cli指令能够正常执行。
为什么没有在Mac的Docker里安装Redis集群?我确实在Docker里安装成功了Redis集群:《2024年2月笔记:Redis7.2.4版本在Mac电脑的Docker里安装Redis集群,但是SprintBoot连接不到Docker内部网络IP地址的Redis集群》,但是!当SpringBoot连接到Redis集群时,集群内部为了跳转到正确的槽位对应的节点用的是Docker内部网络IP比如172.18.0.2,Mac宿主机访问不了172.18.0.2因为是Docker内网IP,所以Springboot访问redis失败。目前暂时未解决该问题,有懂的小伙伴麻烦留言告诉我,不胜感激。
第1步,新建redis.conf
redis.conf内容如下:
port 7021
cluster-enabled yes // 启用集群模式
cluster-config-file nodes.conf // 集群的配置文件
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0 // 任何IP都可访问
masterauth myredis // 指定集群密码
requirepass myredis // 指定集群密码
第2步,新建6个以不同端口号为命名的文件夹,用于存储6个redis节点的新文件生成内容
新建6个文件夹,以redis集群的6个端口命名,分别是: 7021, 7022, 7023, 7024, 7025, 7026
复制redis.conf到以上新建的六个文件夹下,把port端口号改成相应的redis 端口号,我这里就是:7021, 7022, 7023, 7024, 7025, 7026
分别启动6个节点:
zhu@DW61 7021 % cd /7021
zhu@DW61 7022 % redis-server redis.conf
24907:C 10 Feb 2024 11:57:39.240 # WARNING: Changing databases number from 16 to 1 since we are in cluster mode
zhu@DW61 7021 % cd ../7022
zhu@DW61 7022 % redis-server redis.conf
24907:C 10 Feb 2024 11:57:39.240 # WARNING: Changing databases number from 16 to 1 since we are in cluster mode
zhu@DW61 7022 % cd ../7023
zhu@DW61 7023 % redis-server redis.conf
24990:C 10 Feb 2024 11:57:43.729 # WARNING: Changing databases number from 16 to 1 since we are in cluster mode
zhu@DW61 7023 % cd ../7024
zhu@DW61 7024 % redis-server redis.conf
25022:C 10 Feb 2024 11:57:46.544 # WARNING: Changing databases number from 16 to 1 since we are in cluster mode
zhu@DW61 7024 % cd ../7025
zhu@DW61 7025 % redis-server redis.conf
25044:C 10 Feb 2024 11:57:50.082 # WARNING: Changing databases number from 16 to 1 since we are in cluster mode
zhu@DW61 7025 % cd ../7026
zhu@DW61 7026 % redis-server redis.conf
25084:C 10 Feb 2024 11:57:54.540 # WARNING: Changing databases number from 16 to 1 since we are in cluster mode
查看redis所有的启动节点,此时每个节点都是独立节点,还不是集群:
zhu@DW61 7026 % ps -ef | grep redis
504 24802 1 0 11:57AM ?? 0:00.45 redis-server 0.0.0.0:7021 [cluster]
504 24908 1 0 11:57AM ?? 0:00.18 redis-server 0.0.0.0:7022 [cluster]
504 24991 1 0 11:57AM ?? 0:00.15 redis-server 0.0.0.0:7023 [cluster]
504 25023 1 0 11:57AM ?? 0:00.13 redis-server 0.0.0.0:7024 [cluster]
504 25045 1 0 11:57AM ?? 0:00.10 redis-server 0.0.0.0:7025 [cluster]
504 25085 1 0 11:57AM ?? 0:00.07 redis-server 0.0.0.0:7026 [cluster]
504 25130 1404 0 11:58AM ttys001 0:00.00 grep redis
启动6个节点成功后,文件夹目录是这样的:
第3步,执行创建Redis Cluster 集群指令
执行创建Redis集群指令:
redis-cli --cluster create 127.0.0.1:7021 127.0.0.1:7022 127.0.0.1:7023 127.0.0.1:7024 127.0.0.1:7025 127.0.0.1:7026 --cluster-replicas 1 -a myredis
创建集群的整个过程:
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 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7025 to 127.0.0.1:7021
Adding replica 127.0.0.1:7026 to 127.0.0.1:7022
Adding replica 127.0.0.1:7024 to 127.0.0.1:7023
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 256e2f22ebff898c19c0e943caf3dfefc2e75eb8 127.0.0.1:7021
slots:[0-5460] (5461 slots) master
M: 6af6f789151e825aab57998b9d5efc2da0abf6db 127.0.0.1:7022
slots:[5461-10922] (5462 slots) master
M: 719bc79c5d4144e82596b1083592223771735da8 127.0.0.1:7023
slots:[10923-16383] (5461 slots) master
S: fb576ff3f4fd2918c7207a50583df62c00f6e747 127.0.0.1:7024
replicates 719bc79c5d4144e82596b1083592223771735da8
S: 01200cf6560b1913c43affa73ac0aaaaffff75d2 127.0.0.1:7025
replicates 256e2f22ebff898c19c0e943caf3dfefc2e75eb8
S: 23bd2778ec4ebebd45cd0f5d0c393566a33bc47f 127.0.0.1:7026
replicates 6af6f789151e825aab57998b9d5efc2da0abf6db
Can I set the above configuration? (type 'yes' to accept): yes // 这里的yes需要手动输入
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 127.0.0.1:7021)
M: 256e2f22ebff898c19c0e943caf3dfefc2e75eb8 127.0.0.1:7021
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 23bd2778ec4ebebd45cd0f5d0c393566a33bc47f 127.0.0.1:7026
slots: (0 slots) slave
replicates 6af6f789151e825aab57998b9d5efc2da0abf6db
S: 01200cf6560b1913c43affa73ac0aaaaffff75d2 127.0.0.1:7025
slots: (0 slots) slave
replicates 256e2f22ebff898c19c0e943caf3dfefc2e75eb8
S: fb576ff3f4fd2918c7207a50583df62c00f6e747 127.0.0.1:7024
slots: (0 slots) slave
replicates 719bc79c5d4144e82596b1083592223771735da8
M: 6af6f789151e825aab57998b9d5efc2da0abf6db 127.0.0.1:7022
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 719bc79c5d4144e82596b1083592223771735da8 127.0.0.1:7023
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. // 所有16384个槽位全部分配成功,集群创建成功
验证redis集群指令:
redis-cli -c -p 7021 -a myredis // 进入redis集群时,要加入-c表示连接到集群
127.0.0.1:7023> cluster nodes // 查看集群节点,三主三从
fb576ff3f4fd2918c7207a50583df62c00f6e747 127.0.0.1:7024@17024 slave 719bc79c5d4144e82596b1083592223771735da8 0 1707540515000 3 connected
256e2f22ebff898c19c0e943caf3dfefc2e75eb8 127.0.0.1:7021@17021 master - 0 1707540515503 1 connected 0-5460
23bd2778ec4ebebd45cd0f5d0c393566a33bc47f 127.0.0.1:7026@17026 slave 6af6f789151e825aab57998b9d5efc2da0abf6db 0 1707540515503 2 connected
719bc79c5d4144e82596b1083592223771735da8 127.0.0.1:7023@17023 myself,master - 0 1707540515000 3 connected 10923-16383
01200cf6560b1913c43affa73ac0aaaaffff75d2 127.0.0.1:7025@17025 slave 256e2f22ebff898c19c0e943caf3dfefc2e75eb8 0 1707540515503 1 connected
6af6f789151e825aab57998b9d5efc2da0abf6db 127.0.0.1:7022@17022 master - 0 1707540514489 2 connected 5461-10922
127.0.0.1:7023> cluster info // 查看集群信息
cluster_state:ok // 集群状态:ok 表示集群很健康
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:3
cluster_stats_messages_ping_sent:1757
cluster_stats_messages_pong_sent:1739
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:3497
cluster_stats_messages_ping_received:1739
cluster_stats_messages_pong_received:1758
cluster_stats_messages_received:3497
total_cluster_links_buffer_limit_exceeded:0
127.0.0.1:7023>
第4步,SpringBoot集成Redis集群
将redis配置写到application.properties文件里:
spring.redis.cluster.max-redirects=3
spring.redis.client-type=lettuce
spring.redis.cluster.nodes=127.0.0.1:7021,127.0.0.1:7022,127.0.0.1:7023,127.0.0.1:7024,127.0.0.1:7025,127.0.0.1:7026
spring.redis.password=myredis
spring.redis.connect-timeout=3000
#spring.redis.timeout=3000
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
配置RedisTemplate:
@Slf4j
@Configuration
@Component
@RequiredArgsConstructor
public class ConfigRedisCluster {
final ObjectMapper objectMapper;
@Primary
@Bean("redisTemplate")
public RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory factory) { // factory对象就是application.properties里配置的redis集群
StringRedisSerializer stringSerializer = new StringRedisSerializer();
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
Springboot启动类:
@Slf4j
@SpringBootApplication
public class EcouponApplication
extends SpringBootServletInitializer
{
public static void main(String[] args) {
SpringApplication.run(EcouponApplication.class, args);
log.info("service is started successfully.");
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(EcouponApplication.class);
}
}
第5步,Redis集群重启指令
当电脑重启后,redis节点全部需要重新启动。
关闭指令:
redis-cli -p 7021 -a myredis shutdown // 每一个节点执行一遍,7022是redis端口,替换成你自己的redis端口就行,shutdown是关闭指令。
redis-cli -p 7022 -a myredis shutdown
redis-cli -p 7023 -a myredis shutdown
redis-cli -p 7024 -a myredis shutdown
redis-cli -p 7025 -a myredis shutdown
redis-cli -p 7026 -a myredis shutdown
再次启动集群时,一定要进入集群所在的节点目录下去执行redis-server redis.conf指令,不要随便在任意目录下就去执行该指令,因为第一次集群启动时创建出来的rdb文件和aof文件全部都在节点的目录下,槽位分配记录也是在节点的目录下:
具体执行每个节点的指令:
zhuw118@DWN4T9CF61 redis-cluster % cd 7021 // 进入第1个节点目录下
zhuw118@DWN4T9CF61 7021 % redis-server redis.conf // 启动第1个节点
19968:C 11 Feb 2024 14:38:15.526 # WARNING: Changing databases number from 16 to 1 since we are in cluster mode
zhuw118@DWN4T9CF61 7021 % cd ../7022 // 进入第2个节点目录下
zhuw118@DWN4T9CF61 7022 % redis-server redis.conf // 启动第2个节点
20181:C 11 Feb 2024 14:38:35.727 # WARNING: Changing databases number from 16 to 1 since we are in cluster mode
zhuw118@DWN4T9CF61 7022 % cd ../7023 // 进入第3个节点目录下
zhuw118@DWN4T9CF61 7023 % redis-server redis.conf // 启动第3个节点
20238:C 11 Feb 2024 14:38:42.990 # WARNING: Changing databases number from 16 to 1 since we are in cluster mode
zhuw118@DWN4T9CF61 7023 % cd ../7024 // 进入第4个节点目录下
zhuw118@DWN4T9CF61 7024 % redis-server redis.conf // 启动第4个节点
20261:C 11 Feb 2024 14:38:46.228 # WARNING: Changing databases number from 16 to 1 since we are in cluster mode
zhuw118@DWN4T9CF61 7024 % cd ../7025 // 进入第5个节点目录下
zhuw118@DWN4T9CF61 7025 % redis-server redis.conf // 启动第5个节点
20281:C 11 Feb 2024 14:38:49.373 # WARNING: Changing databases number from 16 to 1 since we are in cluster mode
zhuw118@DWN4T9CF61 7025 % cd ../7026 // 进入第6个节点目录下
zhuw118@DWN4T9CF61 7026 % redis-server redis.conf // 启动第6个节点
20308:C 11 Feb 2024 14:38:52.705 # WARNING: Changing databases number from 16 to 1 since we are in cluster mode
重新启动后要重新验证集群状态:
zhuw118@DWN4T9CF61 7026 % ps -ef |grep redis // 查看所有redis节点的启动状态
504 19969 1 0 2:38PM ?? 0:01.09 redis-server 0.0.0.0:7021 [cluster]
504 20182 1 0 2:38PM ?? 0:00.57 redis-server 0.0.0.0:7022 [cluster]
504 20239 1 0 2:38PM ?? 0:00.38 redis-server 0.0.0.0:7023 [cluster]
504 20262 1 0 2:38PM ?? 0:00.33 redis-server 0.0.0.0:7024 [cluster]
504 20282 1 0 2:38PM ?? 0:00.16 redis-server 0.0.0.0:7025 [cluster]
504 20309 1 0 2:38PM ?? 0:00.10 redis-server 0.0.0.0:7026 [cluster]
504 20334 76345 0 2:38PM ttys000 0:00.00 grep redis
zhuw118@DWN4T9CF61 7026 % redis-cli -c -p 7022 -a myredis // 用-c指令进入集群模式的-p端口为7022的节点,-a指令表示集群密码
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7022> cluster nodes
23bd2778ec4ebebd45cd0f5d0c393566a33bc47f 127.0.0.1:7026@17026 slave 6af6f789151e825aab57998b9d5efc2da0abf6db 0 1707633558000 2 connected
01200cf6560b1913c43affa73ac0aaaaffff75d2 127.0.0.1:7025@17025 slave 256e2f22ebff898c19c0e943caf3dfefc2e75eb8 0 1707633558000 1 connected
fb576ff3f4fd2918c7207a50583df62c00f6e747 127.0.0.1:7024@17024 slave 719bc79c5d4144e82596b1083592223771735da8 0 1707633558632 3 connected
719bc79c5d4144e82596b1083592223771735da8 127.0.0.1:7023@17023 master - 0 1707633558000 3 connected 10923-16383
256e2f22ebff898c19c0e943caf3dfefc2e75eb8 127.0.0.1:7021@17021 master - 0 1707633558530 1 connected 0-5460
6af6f789151e825aab57998b9d5efc2da0abf6db 127.0.0.1:7022@17022 myself,master - 0 1707633557000 2 connected 5461-10922
127.0.0.1:7022> cluster info
cluster_state:ok // 集群状态ok,表示集群重新启动成功
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_ping_sent:89
cluster_stats_messages_pong_sent:80
cluster_stats_messages_fail_sent:6
cluster_stats_messages_sent:175
cluster_stats_messages_ping_received:80
cluster_stats_messages_pong_received:85
cluster_stats_messages_fail_received:4
cluster_stats_messages_received:169
total_cluster_links_buffer_limit_exceeded:0
end.
![](https://images.cnblogs.com/cnblogs_com/zhuwenjoyce/945554/o_201113020411my-zhifubao-shouqianma.jpeg)