redis分片集群的部署和使用教程
使用docker在虛擬機(jī)部署
腳本是個(gè)好東西。使用腳本部署那是相當(dāng)?shù)目焖?。下面介紹一下我使用腳本+docker部署的步驟吧。
- 首先拉取鏡像,至少是可以分片的版本。
pull images redis:5.0.9-alpine3.11
- 選取一個(gè)工作目錄創(chuàng)建一個(gè)redis配置文件的模板文件(redis-cluster.tmpl )
port ${PORT}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 你的虛擬機(jī)IP
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes- 使用腳本批量創(chuàng)建目錄和配置文件 init.sh
#!/bin/bash
for port in $(seq 8001 8006);
do
mkdir -p ./${port}/conf
PORT=${port} envsubst < ./redis/redis-cluster.tmpl > ./${port}/conf/redis.conf
mkdir -p ./${port}/data;
done- 給腳本權(quán)限并運(yùn)行腳本
chmod +x init.sh ./init.sh
- 創(chuàng)建docker-compose.yml文件
這里redis對應(yīng)的版本按照自己拉取的改一下。
version: '3.8'
services:
redis-8001:
image: redis:latest
container_name: redis-8001
ports:
- "8001:8001"
- "18001:18001"
volumes:
- ./8001/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./8001/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
networks:
- redis-cluster-net
redis-8002:
image: redis:latest
container_name: redis-8002
ports:
- "8002:8002"
- "18002:18002"
volumes:
- ./8002/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./8002/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
networks:
- redis-cluster-net
redis-8003:
image: redis:latest
container_name: redis-8003
ports:
- "8003:8003"
- "18003:18003"
volumes:
- ./8003/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./8003/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
networks:
- redis-cluster-net
redis-8004:
image: redis:latest
container_name: redis-8004
ports:
- "8004:8004"
- "18004:18004"
volumes:
- ./8004/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./8004/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
networks:
- redis-cluster-net
redis-8005:
image: redis:latest
container_name: redis-8005
ports:
- "8005:8005"
- "18005:18005"
volumes:
- ./8005/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./8005/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
networks:
- redis-cluster-net
redis-8006:
image: redis:latest
container_name: redis-8006
ports:
- "8006:8006"
- "18006:18006"
volumes:
- ./8006/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./8006/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
networks:
- redis-cluster-net
networks:
redis-cluster-net:
driver: bridge- 在創(chuàng)建dcoker-compose.yml對應(yīng)的目錄下執(zhí)行該文件
docker compose up -d
- 隨便進(jìn)入一個(gè)容器內(nèi)部搭建分片集群
docker exec -it reids-8001 redis-cli --cluster create \
192.168.59.128:8001 \
192.168.59.128:8002 \
192.168.59.128:8003\
192.168.59.128:8004 \
192.168.59.128:8005 \
192.168.59.128:8006 \
--cluster-replicas 1 // 副本數(shù)量·1 前三個(gè)節(jié)點(diǎn)成為主節(jié)點(diǎn),后面三個(gè)節(jié)點(diǎn)成為從節(jié)點(diǎn)至此大功告成。
SpringBoot 整合redis集群
- 導(dǎo)入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>- 配置yml
spring:
data:
redis:
# 集群節(jié)點(diǎn)配置:填寫你的虛擬機(jī)IP和端口
cluster:
nodes:
- 192.168.59.128:8001
- 192.168.59.128:8002
- 192.168.59.128:8003
- 192.168.59.128:8004
- 192.168.59.128:8005
# 最大重定向次數(shù)
max-redirects: 3
# 連接超時(shí)時(shí)間
timeout: 3000ms
# Lettuce連接池配置 (可選但推薦)
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: -1ms- 配置讀寫分離
@Configuration
public class RedisConfig {
@Bean
public LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer() {
return clientConfigurationBuilder -> clientConfigurationBuilder
.readFrom(ReadFrom.REPLICA_PREFERRED); // 優(yōu)先從從節(jié)點(diǎn)讀取
}
}可選取的讀寫分離政策:
- ReadFrom.MASTER // 只從主節(jié)點(diǎn)讀?。◤?qiáng)一致性)
- ReadFrom.MASTER_PREFERRED // 優(yōu)先從主節(jié)點(diǎn)讀取
- ReadFrom.REPLICA // 只從從節(jié)點(diǎn)讀取
- ReadFrom.REPLICA_PREFERRED // 優(yōu)先從從節(jié)點(diǎn)讀取(推薦)
- ReadFrom.NEAREST // 從最近的節(jié)點(diǎn)讀取
- ReadFrom.ANY // 從任意節(jié)點(diǎn)讀取
redis分片集群一些須知
哈希槽
- 分片集群一共有為0-16383 slot,存儲數(shù)據(jù)和讀取數(shù)據(jù)時(shí),在哪一個(gè)節(jié)點(diǎn)進(jìn)行操作,是根據(jù)key的有效值,根據(jù)CRC16 算法算取哈希值,然后對 16384 進(jìn)行取模,得到的值就是slot值。
- key的有效值分為兩種情況
a.key中沒有{},整個(gè)key都是有效值
b. key中有{},只有{}中的字符才是有效值。
3.根據(jù)key的有效值的特性,我們可以把同一類型的數(shù)據(jù)存儲在相同的slot下,減少切換節(jié)點(diǎn)帶來的不必要的性能消耗。比如存儲用戶的數(shù)據(jù) ,在設(shè)計(jì)key時(shí),我們可以統(tǒng)一在和用戶相關(guān)的key前加上:{user}。
redis的拓展和壓縮
- 添加一個(gè)新的節(jié)點(diǎn)
redis-cli --cluster add-node <新節(jié)點(diǎn)IP:端口> <集群中任意已知節(jié)點(diǎn)IP:端口>
默認(rèn)時(shí)主節(jié)點(diǎn),此時(shí)新節(jié)點(diǎn)還沒有分配任何的slot,需要重新分配slot。
redis-cli --cluster reshard <集群中任意已知節(jié)點(diǎn)IP:端口>
如果要設(shè)置成從節(jié)點(diǎn)
# 連接新節(jié)點(diǎn) redis-cli -h <新從節(jié)點(diǎn)IP> -p <新從節(jié)點(diǎn)端口> # 設(shè)置主節(jié)點(diǎn) CLUSTER REPLICATE <目標(biāo)主節(jié)點(diǎn)ID>
- 刪除節(jié)點(diǎn)
如果刪除的從節(jié)點(diǎn),因?yàn)閺墓?jié)點(diǎn)沒有任何的slot,所以可以直接刪除
redis-cli --cluster del-node <集群中任意已知節(jié)點(diǎn)IP:端口> <要?jiǎng)h除的從節(jié)點(diǎn)ID>
刪除主節(jié)點(diǎn),把主節(jié)點(diǎn)的slot遷移到其他主節(jié)點(diǎn)在進(jìn)行刪除
redis-cli --cluster reshard <集群中任意已知節(jié)點(diǎn)IP:端口>
到此這篇關(guān)于redis分片集群的部署和使用的文章就介紹到這了,更多相關(guān)redis分片集群部署內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用?redis?消息隊(duì)列完成秒殺過期訂單處理操作(二)
這篇文章主要介紹了如何使用?redis?消息隊(duì)列完成秒殺過期訂單處理操作,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-07-07
Redis 中spark參數(shù)executor-cores引起的異常解決辦法
這篇文章主要介紹了Redis 中spark參數(shù)executor-cores引起的異常解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03
如何基于Session實(shí)現(xiàn)短信登錄功能
對比起Cookie,Session是存儲在服務(wù)器端的會話,相對安全,并且不像Cookie那樣有存儲長度限制,下面這篇文章主要給大家介紹了關(guān)于如何基于Session實(shí)現(xiàn)短信登錄功能的相關(guān)資料,需要的朋友可以參考下2022-10-10
Redis分布式限流生產(chǎn)環(huán)境落地方案
本文主要介紹了Redis分布式限流生產(chǎn)環(huán)境落地方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2026-02-02
Unable?to?connect?to?Redis無法連接到Redis解決的全過程
這篇文章主要給大家介紹了關(guān)于Unable?to?connect?to?Redis無法連接到Redis解決的相關(guān)資料,文中通過圖文以及實(shí)例代碼將解決的過程介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
Redis Cluster Pipeline導(dǎo)致的死鎖問題解決
本文主要介紹了Redis Cluster Pipeline導(dǎo)致的死鎖問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10
Jedis操作Redis實(shí)現(xiàn)模擬驗(yàn)證碼發(fā)送功能
Redis是一個(gè)著名的key-value存儲系統(tǒng),也是nosql中的最常見的一種,這篇文章主要給大家介紹Jedis操作Redis實(shí)現(xiàn)模擬驗(yàn)證碼發(fā)送功能,感興趣的朋友一起看看吧2021-09-09

