Docker環境下秒建Redis集羣,連SpringBoot也整上了!

SpringBoot實戰電商項目mall(30k+star)地址:github.com/macrozheng/…node

摘要

爲了提升Redis的存儲容量和響應速度,有時候咱們須要搭建Redis集羣。本文主要講述Redis集羣環境的搭建步驟以及如何在SpringBoot中整合使用Redis集羣。git

Redis集羣搭建

這裏咱們使用最方便的搭建方式,使用Docker Compose來搭建,對Docker Compose不瞭解的朋友能夠參考下《使用Docker Compose部署SpringBoot應用》。咱們將搭建一個6節點的Redis集羣,包括3個主節點和3個從節點。github

  • 在搭建Redis集羣以前,咱們須要修改下Redis的配置文件redis.conf,該文件的下載地址:github.com/antirez/red…redis

  • 須要修改的屬性以下,主要是修改了一些集羣配置和運行端口,端口號須要按需修改成6391~6396:spring

# 開啓集羣功能
cluster-enabled yes
# 設置運行端口
port 6391
# 設置節點超時時間,單位毫秒
cluster-node-timeout 15000
# 集羣內部配置文件
cluster-config-file "nodes-6391.conf"
複製代碼
  • 而後咱們須要編寫docker-compose.yml文件用於編排6個Redis容器,具體屬性的做用能夠參考下面的註釋;
version: "3"
services:
 redis-master1:
 image: redis:5.0 # 基礎鏡像
 container_name: redis-master1 # 容器名稱
 working_dir: /config # 切換工做目錄
 environment: # 環境變量
 - PORT=6391 # 會使用config/nodes-${PORT}.conf這個配置文件
 ports: # 映射端口,對外提供服務
 - 6391:6391 # redis的服務端口
 - 16391:16391 # redis集羣監控端口
 stdin_open: true # 標準輸入打開
 tty: true # 後臺運行不退出
 network_mode: host # 使用host模式
 privileged: true # 擁有容器內命令執行的權限
 volumes:
 - /mydata/redis-cluster/config:/config #配置文件目錄映射到宿主機
 entrypoint: # 設置服務默認的啓動程序
 - /bin/bash
 - redis.sh
 redis-master2:
 image: redis:5.0
 working_dir: /config
 container_name: redis-master2
 environment:
 - PORT=6392
 ports:
 - 6392:6392
 - 16392:16392
 stdin_open: true
 network_mode: host
 tty: true
 privileged: true
 volumes:
 - /mydata/redis-cluster/config:/config
 entrypoint:
 - /bin/bash
 - redis.sh
 redis-master3:
 image: redis:5.0
 container_name: redis-master3
 working_dir: /config
 environment:
 - PORT=6393
 ports:
 - 6393:6393
 - 16393:16393
 stdin_open: true
 network_mode: host
 tty: true
 privileged: true
 volumes:
 - /mydata/redis-cluster/config:/config
 entrypoint:
 - /bin/bash
 - redis.sh
 redis-slave1:
 image: redis:5.0
 container_name: redis-slave1
 working_dir: /config
 environment:
 - PORT=6394
 ports:
 - 6394:6394
 - 16394:16394
 stdin_open: true
 network_mode: host
 tty: true
 privileged: true
 volumes:
 - /mydata/redis-cluster/config:/config
 entrypoint:
 - /bin/bash
 - redis.sh
 redis-slave2:
 image: redis:5.0
 working_dir: /config
 container_name: redis-slave2
 environment:
 - PORT=6395
 ports:
 - 6395:6395
 - 16395:16395
 stdin_open: true
 network_mode: host
 tty: true
 privileged: true
 volumes:
 - /mydata/redis-cluster/config:/config
 entrypoint:
 - /bin/bash
 - redis.sh
 redis-slave3:
 image: redis:5.0
 container_name: redis-slave3
 working_dir: /config
 environment:
 - PORT=6396
 ports:
 - 6396:6396
 - 16396:16396
 stdin_open: true
 network_mode: host
 tty: true
 privileged: true
 volumes:
 - /mydata/redis-cluster/config:/config
 entrypoint:
 - /bin/bash
 - redis.sh
複製代碼
  • 從docker-compose.yml文件中咱們能夠看到,咱們的Redis容器分別運行在6391~6396這6個端口之上, 將容器中的/config配置目錄映射到了宿主機的/mydata/redis-cluster/config目錄,同時還以redis.sh腳本做爲該容器的啓動腳本;docker

  • redis.sh腳本的做用是根據environment環境變量中的PORT屬性,以指定配置文件來啓動Redis容器;數據庫

redis-server  /config/nodes-${PORT}.conf
複製代碼
  • 接下來咱們須要把Redis的配置文件和redis.sh上傳到Linux服務器的/mydata/redis-cluster/config目錄下;

  • 接下來上傳咱們的docker-compose.yml文件到Linux服務器,並使用docker-compose命令來啓動全部容器;
docker-compose up -d
複製代碼
  • 啓動過程當中會輸出以下信息;

  • 此時進入其中一個Redis容器之中,初始化Redis集羣;
# 進入Redis容器
docker exec -it redis-master1 /bin/bash
# 初始化Redis集羣命令
redis-cli --cluster create \
192.168.6.139:6391 192.168.6.139:6392 192.168.6.139:6393 \
192.168.6.139:6394 192.168.6.139:6395 192.168.6.139:6396 \
--cluster-replicas 1
複製代碼
  • 集羣建立過程當中會讓你確認配置,輸入yes確認便可;

  • Redis集羣建立成功後會輸出以下信息;

  • 建立成功後咱們可使用redis-cli命令鏈接到其中一個Redis服務;
# 單機模式啓動
redis-cli -h 127.0.0.1 -p 6391
# 集羣模式啓動
redis-cli -c -h 127.0.0.1 -p 6391
複製代碼
  • 以後經過cluster nodes命令能夠查看節點信息,發現符合原來3主3從的預期。

SpringBoot中使用Redis集羣

咱們在《Spring Data Redis 最佳實踐!》中講到了在SpringBoot中如何使用Redis,用的是單節點的Redis服務,此次咱們講下如何使用Redis集羣服務。緩存

  • 咱們在原來代碼的基礎上進行改造,修改application.yml配置文件,添加Redis集羣配置;
spring:
 redis:
# host: 192.168.6.139 # Redis服務器地址
# database: 0 # Redis數據庫索引(默認爲0)
# port: 6379 # Redis服務器鏈接端口
 password: # Redis服務器鏈接密碼(默認爲空)
 timeout: 3000ms # 鏈接超時時間
 lettuce:
 pool:
 max-active: 8 # 鏈接池最大鏈接數
 max-idle: 8 # 鏈接池最大空閒鏈接數
 min-idle: 0 # 鏈接池最小空閒鏈接數
 max-wait: -1ms # 鏈接池最大阻塞等待時間,負值表示沒有限制
 cluster:
 nodes:
 - 192.168.6.139:6391
 - 192.168.6.139:6392
 - 192.168.6.139:6393
 - 192.168.6.139:6394
 - 192.168.6.139:6395
 - 192.168.6.139:6396
複製代碼
  • 此時咱們再次調用獲取品牌詳情的接口,就會把品牌信息緩存到Redis集羣中去了;bash

  • 因爲Redis容器redis-master1redis-slave2互爲主從,因此裏面都緩存了相同的品牌詳情信息。服務器

配置文件地址

github.com/macrozheng/…

項目源碼地址

github.com/macrozheng/…

公衆號

mall項目全套學習教程連載中,關注公衆號第一時間獲取。

公衆號圖片
相關文章
相關標籤/搜索