Redis實現(xiàn)集群搭建+集群讀寫的示例
問題
容量不夠,redis 如何進行擴容?并發(fā)寫操作, redis 如何分攤?另外,主從模式,薪火相傳模式,主機宕機,導致 ip 地址發(fā)生變化,應(yīng)用程序中配置需要修改對應(yīng)的主機地址、端口等信息。
之前通過代理主機來解決,但是 redis7.0 中提供了解決方案。就是 無中心化集群 配置:即每個節(jié)點都可以和其他節(jié)點進行聯(lián)系。如 A、B、C 節(jié)點。想訪問 B 節(jié)點,可以先訪問 A 節(jié)點,A 節(jié)點會去聯(lián)系 B 節(jié)點。無須代理服務(wù)器或者負載均衡去找對應(yīng)的節(jié)點
什么是集群
Redis 集群實現(xiàn)了對 Redis 的水平擴容,即啟動 N 個 redis 節(jié)點,將整個數(shù)據(jù)庫分布存儲在這 N 個節(jié)點中,每個節(jié)點存儲總數(shù)據(jù)的 1/N。
Redis 集群通過分區(qū)(partition)來提供一定程度的可用性(availability):即使集群中有一部分節(jié)點失效或者無法進行通訊,集群也可以繼續(xù)處理命令請求。
環(huán)境準備
- 將 rdb、aof 文件都刪除掉
- 三臺虛擬機,新建目錄/myredis/cluster
- 制作 6 個實例,三主三從,三主機端口分別為 6381,6383,6385。三從機端口分別為6382,6384,6386(具體端口視情況)
- 每一個配置文件的基本信息修改(和 主從復制 - 哨兵模式 一樣)
- 開啟 daemonize yes
- 指定 6 個端口,不能重復
- 6 個 Pid 文件名字配置,不能重復,盡量以加上端口進行識別
- 6 個 Log 文件名字,不能重復,盡量以加上端口進行識別
- 6 個 dump.rdb 名字,不能重復,盡量以加上端口進行識別
- Appendonly 關(guān)掉或者換名字
- 每一個配置文件的集群信息修改
- cluster-enabled yes:打開集群模式
- cluster-config-file nodes-6379.conf:設(shè)定節(jié)點配置文件名
- cluster-node-timeout 15000:設(shè)定節(jié)點失聯(lián)時間,超過該時間(毫秒),集群自動進行主從切換
配置文件模板(替換端口號):
[root@redis-cluster2 cluster]# cat redisCluster6383.conf bind 0.0.0.0 daemonize yes protected-mode no logfile "/myredis/cluster/cluster6383.log" pidfile /myredis/cluster6383.pid dir /myredis/cluster dbfilename dump6383.rdb appendonly yes appendfilename "appendonly6383.aof" requirepass 111111 masterauth 111111 port 6383 cluster-enabled yes cluster-config-file nodes-6383.conf cluster-node-timeout 5000
將以上配置文件分別寫入到三臺虛擬機中,每個虛擬機其兩個redis服務(wù),使用redis-server指定配置文件啟動
[root@redis-cluster2 cluster]# redis-server /myredis/cluster/redisCluster6383.conf [root@redis-cluster2 cluster]# redis-server /myredis/cluster/redisCluster6384.conf [root@redis-cluster2 cluster]# ps -ef | grep redis root 1950 1 0 15:38 ? 00:00:00 redis-server 0.0.0.0:6383 [cluster] root 1956 1 0 15:38 ? 00:00:00 redis-server 0.0.0.0:6384 [cluster] root 1962 1569 0 15:38 pts/0 00:00:00 grep --color=auto redis
通過命令redis-cli為集群構(gòu)建主從關(guān)系
[root@k8s-master01 cluster]# redis-cli -a 111111 --cluster create --cluster-replicas 1 192.168.58.129:6381 192.168.58.129:6382 192.168.58.130:6383 192.168.58.130:6384 192.168.58.212:6385 192.168.58.212:6386 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 192.168.58.130:6384 to 192.168.58.129:6381 Adding replica 192.168.58.212:6386 to 192.168.58.130:6383 Adding replica 192.168.58.129:6382 to 192.168.58.212:6385 M: 772165af2d8a6b4600833ff36f33d1fe02acfa96 192.168.58.129:6381 slots:[0-5460] (5461 slots) master S: 5edb851c0aa5de08924f1d079c21b668e74e14ab 192.168.58.129:6382 replicates c1e8aaf10e92cd3aebffe1247d8c99a9d857e916 M: e722f20399055c4543aa5a60697a1c6cebfe8e06 192.168.58.130:6383 slots:[5461-10922] (5462 slots) master S: afc75f35d9856b15040460f3373fd658907e1cc8 192.168.58.130:6384 replicates 772165af2d8a6b4600833ff36f33d1fe02acfa96 M: c1e8aaf10e92cd3aebffe1247d8c99a9d857e916 192.168.58.212:6385 slots:[10923-16383] (5461 slots) master S: a256bdaeafd97ff59a4e59d994046ff102e7509b 192.168.58.212:6386 replicates e722f20399055c4543aa5a60697a1c6cebfe8e06 Can 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 cluster Waiting for the cluster to join . >>> Performing Cluster Check (using node 192.168.58.129:6381) M: 772165af2d8a6b4600833ff36f33d1fe02acfa96 192.168.58.129:6381 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: afc75f35d9856b15040460f3373fd658907e1cc8 192.168.58.130:6384 slots: (0 slots) slave replicates 772165af2d8a6b4600833ff36f33d1fe02acfa96 S: 5edb851c0aa5de08924f1d079c21b668e74e14ab 192.168.58.129:6382 slots: (0 slots) slave replicates c1e8aaf10e92cd3aebffe1247d8c99a9d857e916 M: e722f20399055c4543aa5a60697a1c6cebfe8e06 192.168.58.130:6383 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: a256bdaeafd97ff59a4e59d994046ff102e7509b 192.168.58.212:6386 slots: (0 slots) slave replicates e722f20399055c4543aa5a60697a1c6cebfe8e06 M: c1e8aaf10e92cd3aebffe1247d8c99a9d857e916 192.168.58.212:6385 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. [root@k8s-master01 cluster]# ll total 52 drwxr-xr-x. 2 root root 4096 Dec 1 15:30 appendonlydir -rw-r--r--. 1 root root 10718 Dec 1 15:50 cluster6381.log -rw-r--r--. 1 root root 15205 Dec 1 15:50 cluster6382.log -rw-r--r--. 1 root root 171 Dec 1 15:49 dump6382.rdb -rw-r--r--. 1 root root 799 Dec 1 15:49 nodes-6381.conf -rw-r--r--. 1 root root 811 Dec 1 15:49 nodes-6382.conf -rw-r--r--. 1 root root 347 Dec 1 15:32 redisCluster6381.conf -rw-r--r--. 1 root root 346 Dec 1 15:32 redisCluster6382.conf
以6381為切入點查看集群狀態(tài)
[root@k8s-master01 cluster]# redis-cli -a 111111 -p 6381 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6381> 127.0.0.1:6381> info replication # Replication role:master connected_slaves:1 slave0:ip=192.168.58.130,port=6384,state=online,offset=224,lag=0 master_failover_state:no-failover master_replid:6955624b397904832e2911972f1c9b871e8b86fc master_replid2:0000000000000000000000000000000000000000 master_repl_offset:224 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:224 127.0.0.1:6381>
集群指令
查看集群信息
127.0.0.1:6381> cluster nodes afc75f35d9856b15040460f3373fd658907e1cc8 192.168.58.130:6384@16384 slave 772165af2d8a6b4600833ff36f33d1fe02acfa96 0 1733039642782 1 connected 5edb851c0aa5de08924f1d079c21b668e74e14ab 192.168.58.129:6382@16382 slave c1e8aaf10e92cd3aebffe1247d8c99a9d857e916 0 1733039641297 5 connected e722f20399055c4543aa5a60697a1c6cebfe8e06 192.168.58.130:6383@16383 master - 0 1733039641617 3 connected 5461-10922 a256bdaeafd97ff59a4e59d994046ff102e7509b 192.168.58.212:6386@16386 slave e722f20399055c4543aa5a60697a1c6cebfe8e06 0 1733039641067 3 connected c1e8aaf10e92cd3aebffe1247d8c99a9d857e916 192.168.58.212:6385@16385 master - 0 1733039640849 5 connected 10923-16383 772165af2d8a6b4600833ff36f33d1fe02acfa96 192.168.58.129:6381@16381 myself,master - 0 1733039639000 1 connected 0-5460
查看鍵的插槽值
cluster keyslot k1 192.168.58.129:6381> cluster keyslot k1 (integer) 12706 192.168.58.129:6381> cluster keyslot k2 (integer) 449
查看插槽值里有幾個key
cluster countkeysinslot 4847 #只能看自己插槽的值
查詢集群中的值
cluster getkeysinslot <slot> <count> #返回 count 個 slot 槽中的鍵
查看單節(jié)點
127.0.0.1:6381> cluster info cluster_state: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:1 cluster_stats_messages_ping_sent:1226 cluster_stats_messages_pong_sent:1199 cluster_stats_messages_sent:2425 cluster_stats_messages_ping_received:1194 cluster_stats_messages_pong_received:1226 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:2425 total_cluster_links_buffer_limit_exceeded:0
集群讀寫
在6381進行寫操作時,set k1出現(xiàn)報錯,顯示到6385進行寫,操作,set k2 卻可以執(zhí)行成功,這是為什么呢?
127.0.0.1:6381> set k1 v1 (error) MOVED 12706 192.168.58.212:6385 127.0.0.1:6381> set k2 v2 OK 127.0.0.1:6381> keys * 1) "k2" 127.0.0.1:6381>
是因為集群在創(chuàng)建時,分配了三個槽位,必須在各自的槽位進行寫操作
如何解決?
防止路由失效,添加參數(shù)-c并新增兩個key
127.0.0.1:6381> FLUSHALL OK 127.0.0.1:6381> keys * (empty array) 127.0.0.1:6381> quit [root@k8s-master01 cluster]# redis-cli -a 111111 -p 6381 -c Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6381> 127.0.0.1:6381> 127.0.0.1:6381> keys * (empty array) 127.0.0.1:6381> set k1 v1 -> Redirected to slot [12706] located at 192.168.58.212:6385 OK 192.168.58.212:6385> 192.168.58.212:6385> set k2 v2 -> Redirected to slot [449] located at 192.168.58.129:6381 OK
到此這篇關(guān)于Redis實現(xiàn)集群搭建+集群讀寫的示例的文章就介紹到這了,更多相關(guān)Redis 集群搭建+集群讀寫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis increment 函數(shù)處理并發(fā)序列號案例
這篇文章主要介紹了Redis increment 函數(shù)處理并發(fā)序列號案例,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-08-08深入了解Redis連接數(shù)問題的現(xiàn)象和解法
一般情況?Redis?連接數(shù)問題并不常見,但是當你業(yè)務(wù)服務(wù)增加、對?Redis?的依賴持續(xù)增強的過程中,可能會遇到很多?Redis?的問題,這個時候,Redis?連接數(shù)可能就成了一個常見的問題,在本章節(jié),希望能夠帶大家了解Redis連接數(shù)問題的現(xiàn)象和解法,需要的朋友可以參考下2023-12-12redis實現(xiàn)計數(shù)器-防止刷單方法介紹
本文主要向大家介紹了redis實現(xiàn)計數(shù)器防止刷單的方法和有關(guān)代碼,具有一定參考價值,需要的朋友可以了解下。2017-11-11關(guān)于redis可視化工具讀取數(shù)據(jù)亂碼問題
大家來聊一聊在日常操作redis時用的是什么工具,redis提供的一些命令你都了解了嗎,今天通過本文給大家介紹redis可視化工具讀取數(shù)據(jù)亂碼問題,感興趣的朋友跟隨小編一起看看吧2021-07-07使用lua+redis解決發(fā)多張券的并發(fā)問題
這篇文章主要介紹了使用lua+redis解決發(fā)多張券的并發(fā)問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01