Redis主從和集群

主從

主從概念

  • ?個master可以擁有多個slave,?個slave?可以擁有多個slave,如此下去,形成了強?的多級服務器集群架構(gòu)
  • master用來寫數(shù)據(jù),slave用來讀數(shù)據(jù),經(jīng)統(tǒng)計:網(wǎng)站的讀寫比率是10:1
  • 通過主從配置可以實現(xiàn)讀寫分離
  • master和slave都是一個redis實例


    image.png

主從配置

配置主

  • 查看當前主機的ip地址

Ifconfig


image.png
  • 修改etc/redis/redis.conf文件
sudo vi redis.conf
bind 192.168.110.36
  • 重啟redis服務
    ps aux | grep redis


    image.png
Sudo kill -9 12916
Sudo redis-server /etc/redis/redis.conf

配置從

  • 復制etc/redis/redis.conf文件

sudo cp redis.conf ./slave.conf

  • 修改redis/slave.conf文件

sudo vi slave.conf

  • 編輯內(nèi)容
bind 192.168.110.36
slaveof 192.168.110.36 6379
port 6378
  • 開啟redis服務

sudo redis-server slave.conf

  • 查看主從關(guān)系

redis-cli -h 192.168.110.36 info Replication


image.png

操作數(shù)據(jù)

  • 進入主客戶端

redis-cli -h 192.168.110.36 -p 6379

  • 進入從客戶端

redis-cli -h 192.168.110.36 -p 6378

  • 在master上寫數(shù)據(jù)

mset a1 11 a2 22 a3 33


image.png
  • 在slave上讀數(shù)據(jù)


    image.png

搭建集群

介紹

為什么要有集群

  • 服務器可能因為代碼原因,人為原因,或者自然災害等造成服務器損壞。數(shù)據(jù)服務就掛掉了
  • 大公司都會有很多的服務器(華東地區(qū)、華南地區(qū)、華中地區(qū)、華北地區(qū)、西北地區(qū)、西南地區(qū)、東北地區(qū)、臺港澳地區(qū)機房)

集群的概念

集群是一組相互獨立的、通過高速網(wǎng)絡互聯(lián)的計算機,它們構(gòu)成了一個組,并以單一系統(tǒng)的模式加以管理。一個客戶與集群相互作用時,集群像是一個獨立的服務器。集群配置是用于提高可用性和可縮放性。


image.png

當請求到來首先由負載均衡服務器處理,把請求轉(zhuǎn)發(fā)到另外的一臺服務器上。
百度的ip地址 119.75.217.109/
61.135.169.121/

Redis集群

分類

  • 軟件層面
  • 硬件層面
    軟件層面:只有一臺電腦,在這臺電腦上啟動了多臺redis服務


    image.png

    硬件層面:存在多臺實體電腦,每臺電腦都啟動了一個redis或者多個redis服務


    image.png

配置機器1

  • 在演示中,192.168.110.37為當前ubuntu機器的ip
  • 在192.168.110.37上進?Desktop?錄,創(chuàng)建conf?錄
  • 在conf?錄下創(chuàng)建?件7000.conf,編輯內(nèi)容如下
port 7000
bind 192.168.110.37
daemonize yes
pidfile 7000.pid
cluster-enabled yes
cluster-config-file 7000_node.conf
cluster-node-timeout 15000
appendonly yese
  • 在conf?錄下創(chuàng)建?件7001.conf,編輯內(nèi)容如下
port 7001
bind 192.168.110.37
daemonize yes
pidfile 7001.pid
cluster-enabled yes
cluster-config-file 7001_node.conf
cluster-node-timeout 15000
appendonly yes
  • 在conf?錄下創(chuàng)建?件7002.conf,編輯內(nèi)容如下
port 7002
bind 192.168.110.37
daemonize yes
pidfile 7002.pid
cluster-enabled yes
cluster-config-file 7002_node.conf
cluster-node-timeout 15000
appendonly yes

總結(jié):這三個文件的配置區(qū)別只有port、pidfile、cluster-config-file三項
使用配置文件啟動redis服務

redis-server 7000.conf
redis-server 7001.conf
redis-server 7002.conf

查看進程如下圖


image.png

配置機器2

  • 在演示中,192.168.110.38為當前ubuntu機器的ip
  • 在192.168.110.38上進?Desktop?錄,創(chuàng)建conf?錄
  • 在conf?錄下創(chuàng)建?件7003.conf,編輯內(nèi)容如下
    ·```shell
    port 7003
    bind 192.168.110.38
    daemonize yes
    pidfile 7003.pid
    cluster-enabled yes
    cluster-config-file 7003_node.conf
    cluster-node-timeout 15000
    appendonly yes
* 在conf?錄下創(chuàng)建?件7004.conf,編輯內(nèi)容如下
```shell
port 7004
bind 192.168.110.38
daemonize yes
pidfile 7004.pid
cluster-enabled yes
cluster-config-file 7004_node.conf
cluster-node-timeout 15000
appendonly yes
  • 在conf?錄下創(chuàng)建?件7005.conf,編輯內(nèi)容如下
port 7005
bind 192.168.110.38
daemonize yes
pidfile 7005.pid
cluster-enabled yes
cluster-config-file 7005_node.conf
cluster-node-timeout 15000
appendonly yes

總結(jié):這三個文件的配置區(qū)別只有port、pidfile、cluster-config-file三項
使用配置文件啟動redis服務

redis-server 7003.conf
redis-server 7004.conf
redis-server 7005.conf

查看進程如下圖


image.png

創(chuàng)建集群

  • redis的安裝包中包含了redis-trib.rb,?于創(chuàng)建集群 //ruby
  • 接下來的操作在192.168.110.37機器上進?
  • 將命令復制,這樣可以在任何?錄下調(diào)?此命令

sudo cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/local/bin/

  • 安裝ruby環(huán)境,因為redis-trib.rb是?ruby開發(fā)的

sudo apt-get install ruby

  • 在提示信息處輸?y,然后回?繼續(xù)安裝


    image.png
  • 運?如下命令創(chuàng)建集群

redis-trib.rb create --replicas 1 192.168.110.37:7000 192.168.110.37:7001 192.168.110.37:7002 192.168.110.38:7003 192.168.110.38:7004 192.168.110.38:7005

執(zhí)?上?這個指令在某些機器上可能會報錯,主要原因是由于安裝的 ruby 不是最 新版本


image.png

天朝的防?墻導致?法下載最新版本,所以需要設置 gem 的源
解決辦法如下:

//先查看??的 gem 源是什么地址

gem source -l // 如果是https://rubygems.org/ 就需要更換

// 更換指令為

gem sources --add [https://gems.ruby-china.com](https://gems.ruby-china.com/) --remove https://rubygems.org/

// 通過 gem 安裝 redis 的相關(guān)依賴

sudo gem install redis
// 然后重新執(zhí)?指令
redis-trib.rb create --replicas 1 192.168.110.37:7000 192.168.110.37:7001 192.168.110.37:7002 192.168.110.38:7003 192.168.110.38:7004 192.168.110.38:7005
image.png

(提示信息輸入yes即)


image.png

提示完成,集群搭建成功

數(shù)據(jù)驗證

  • 根據(jù)上圖可以看出,當前搭建的主服務器為7000、7001、7003,對應的從服務器是7005、7004、7002
  • 在192.168.110.37機器上連接7002,加參數(shù)-c表示連接到集群

redis-cli -h 192.168.110.37 -c -p 7002

  • ?動跳到了7003服務器,并寫?數(shù)據(jù)成功


    image.png
  • 在7003可以獲取數(shù)據(jù),如果寫入數(shù)據(jù)又重定向到7001(負載均衡)


    image.png

    注意點

  • Redis 集群會把數(shù)據(jù)存在?個 master 節(jié)點,然后在這個 master 和其對應的salve 之間進?數(shù)據(jù)同步。當讀取數(shù)據(jù)時,也根據(jù)?致性哈希算法到對應的 master 節(jié) 點獲取數(shù)據(jù)。只有當?個master 掛掉之后,才會啟動?個對應的 salve 節(jié)點,充 當 master
  • 需要注意的是:必須要3個或以上的主節(jié)點,否則在創(chuàng)建集群時會失敗,并且當存 活的主節(jié)點數(shù)?于總節(jié)點數(shù)的?半時,整個集群就?法提供服務了

go語言redis-cluster開源客戶端

go get github.com/gitstliu/go-redis-cluster

代碼

func (this*ClusterController)Get(){
   cluster, _ := redis.NewCluster(
       &redis.Options{
           StartNodes: []string{"192.168.110.37:7000", "192.168.110.37:7001", "192.168.110.37:7002","192.168.110.38:7003","192.168.110.38:7004","192.168.110.38:7005"},
           ConnTimeout: 50 * time.Millisecond,
           ReadTimeout: 50 * time.Millisecond,
           WriteTimeout: 50 * time.Millisecond,
KeepAlive: 16,
           AliveTime: 60 * time.Second,
       })
   cluster.Do("set","name","itheima")

   name,_ := redis.String(cluster.Do("get","name"))
   beego.Info(name)
   this.Ctx.WriteString("集群創(chuàng)建成功")
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. redis主從 主從概念(master--slave) a) ?個master可以擁有多個slave,?個...
    husky_1閱讀 1,889評論 0 1
  • 前期準備 使用的操作系統(tǒng)是os x系統(tǒng), 已經(jīng)安裝了redis(我這里是使用brew進行安裝). redis主從環(huán)...
    CoderMonkey閱讀 789評論 0 3
  • 標簽: redis 緩存 主從 哨兵 集群 本文簡單的介紹redis三種模式在linux的安裝部署和數(shù)據(jù)存儲的總結(jié)...
    luhanlin閱讀 4,495評論 0 5
  • 由于redis集群需要使用ruby命令,所以我們需要安裝ruby(所有的操作都是在單機部署的偽集群模式下進行) (...
    蝸牛docom閱讀 517評論 0 2
  • 忘記俗世的一切,我只想和你互相浪費,短的沉默,長的無意義。 《拉比的海》 大海,沒去過,因為還不知道跟誰去;或許還...
    何語婳閱讀 435評論 2 4

友情鏈接更多精彩內(nèi)容