Redis集群
使用redis做緩存工具
實(shí)現(xiàn)系統(tǒng)高可用,redis需要做主備。使用redis做分片集群。
向業(yè)務(wù)邏輯中添加緩存。
1使用redis做緩存
1.1安裝redis
版本說明
????使用redis4.0.9版本。redis4.0.9版本主要增加了redis集群功能。
安裝的前提條件:
需要安裝gcc:yum
????install gcc-c++
下載redis的源碼包。
wget命令下載:
????yum -y? install wget
????wget?http://download.redis.io/releases/redis-4.0.9.tar.gz
或去官網(wǎng)下載
把源碼包上傳到linux服務(wù)器
解壓源碼包
????tar -zxvf? redis-4.0.9.tar.gz
????cp -r redis-4.0.9 /usr/local/
Make編譯 ? ? ? //在redis-4.0.9源碼文件夾下執(zhí)行
make
Make install 安裝?
//指定安裝的路徑
[
root@localhost? redis-4.0.9]# make install PREFIX=/usr/local/redis
1.2啟動(dòng)redis
前端啟動(dòng)模式
????/usr/local/redis/bin/redis-server
默認(rèn)是前端啟動(dòng)模式,端口是6379
后端啟動(dòng)
????從redis的源碼文件夾redis.4.0.9中復(fù)制redis.conf到redis的安裝目錄。
????cp -r? /usr/local/redis-4.0.9/redis.conf? ../redis/bin
修改配置文件
????vi? ?/usr/local/redis/bin/redis.conf? ? ? 修改下面兩項(xiàng)
(在命令模式下使用 “/ 關(guān)鍵字” 來查找daemonize, 字母 n來查看下一個(gè)匹配項(xiàng))
bind 127.0.01 表示外部不能訪問。所以需要進(jìn)行處理。
*注釋掉bind 127.0.0.1 或者將127.0.0.1改成這個(gè)服務(wù)器的IP地址
[root@localhost bin]# ./redis-server redis.conf? ? //指定redis配置文件啟動(dòng)
[root@localhost bin]# ./redis-cli? //來啟動(dòng)redis客戶端
127.0.0.1:6379>? shutdown? ? ? ? // 關(guān)閉 redis客戶端
not connected> exit? ? ? ? ? ? ? ? ? ? //退出
1.3Redis常用命令
測(cè)試安裝是否成功:
127.0.0.1:6379>set redis? hello
OK
127.0.0.1:6379> get redis
"hello"
集群結(jié)構(gòu)
????集群中有三個(gè)節(jié)點(diǎn)的集群,每個(gè)節(jié)點(diǎn)有一主一備。需要6臺(tái)虛擬機(jī)。
????搭建一個(gè)偽分布式的集群,使用6個(gè)redis實(shí)例來模擬。
2.3搭建集群需要的環(huán)境
????搭建集群需要使用到官方提供的ruby腳本。
????需要安裝ruby的環(huán)境。
????安裝ruby
????????1.
????????????????yum install ruby
[root@localhost local]# yum install ruby
????????2.
????????????????yum install rubygems
[root@localhost local]# yum install rubygems
????redis集群管理工具redis-trib.rb在redis-4.0.9 的源碼文件夾下面的
[root@localhost~]# cd? redis-4.0.9
[root@localhost redis-4.0.9]# cd src
[root@localhost src]# ll *.rb?
? -rwxrwxr-x.1 root root 48141 Apr? 1 07:01redis-trib.rb
[root@localhost src]#
腳本需要的ruby包:
????????需要將redis-4.0.2.gem上傳到linux服務(wù)。
安裝ruby的包:
????????????然后再在這個(gè)redis-4.0.2.gem文件所在路徑下執(zhí)行g(shù)em命令:
[root@localhost redis]# gem install redis-4.0.2.gem
發(fā)生錯(cuò)誤:
ERROR:
Error installing redis-4.0.2.gem:
redis requires
Ruby version >= 2.2.2.? //Ruby version? 版本必須要大于等于2.2.2。當(dāng)前Ruby version 太低,Centos7 的yum源的ruby版本只支持2.0
查看當(dāng)前ruby
-v? 版本
[root@localhost redis]# ruby -v? ? ?
ruby官網(wǎng)推薦rvm
方式更新ruby版本:
1.
獲取
[root@localhost local]# gpg --keyserver hkp://keys.gnupg.net --recv-keys
409B6B1796C275462A1703113804BB82D39DC0E3
7D2BAF1CF37B13E2069D6956105BD0E739499BDB
2.
[root@localhost local]# \curl -sSL https://get.rvm.io | bash -s stable
[root@localhost local]# find / -name rvm -print
[rooocalhost local]# source /usr/local/rvm/scripts/rvm
[root@localhost local]# rvm install 2.4.1? ? ? ? ? //安裝一個(gè)ruby版本
[root@localhost local]# rvm use 2.4.1? ? ? ? ? ? ? // 使用指定版本的ruby
[root@localhost local]#? rvm use 2.4.1–default //設(shè)置默認(rèn)版本
[root@localhost local]# rvm remove 2.0.0? ? ? ? ? ? // 刪除2.0.0版本的
[root@localhost local]# ruby–version? ? ? ? ? ? ? ? //查看ruby版本,安裝
[root@localhost redis]# gem install redis-4.0.2.gem?
Successfully
installed redis-4.0.2
1
gem installed
Installing
ri documentation for redis-4.0.2...
InstallingRDoc documentation for redis-4.0.2.…
2.4集群的搭建
第一步:創(chuàng)建6個(gè)redis實(shí)例,復(fù)制六次redis文件夾,修改端口號(hào)從7001~7006
[root@localhost redis-cluster]# cp -r ../redis/bin? ./redis01
[root@localhost redis-cluster]# cp -r ../redis/bin? ./redis02
[root@localhost redis-cluster]# cp -r ../redis/bin? ./redis03
[root@localhost redis-cluster]# cp -r ../redis/bin? ./redis04
[root@localhost redis-cluster]# cp -r ../redis/bin? ./redis05
[root@localhost redis-cluster]# cp -r ../redis/bin? ./redis06
第二步:刪除dump.rdb
文件
[root@localhost redis01]# rm -rf dump.rdb
第三步:修改redis的配置文件? ? ?
1、修改端口號(hào)
2、打開cluster-enable前面的注釋。
第四步:把創(chuàng)建集群的ruby腳本redis-4.0.9文件夾下的redis-trib.rb復(fù)制到redis-cluster目錄下。
[root@localhost local]# cp redis-4.0.9/src/redis-trib.rb? ./redis-cluster/
第五步:?jiǎn)?dòng)6個(gè)redis實(shí)例
創(chuàng)建sh腳本:
[root@localhost redis-cluster]# vi startall.sh? ? ? ? ? ? ? // 在redis-cluster文件夾下面創(chuàng)建
//編寫了個(gè)sh腳本,將linux命令放腳本文件中順序執(zhí)行
腳本命令內(nèi)容如下:
cd redis01/
./redis-server ./redis.conf
cd ../
cd redis02/
./redis-server ./redis.conf
cd ../
cd redis03/
./redis-server ./redis.conf
cd ../
cd redis04/
./redis-server ./redis.conf
cd ../
cd redis05/
./redis-server ./redis.conf
cd ../
cd redis06/
./redis-server ./redis.conf
給startall.sh的執(zhí)行權(quán)限:
[root@localhost redis-cluster]# chmod 777 ./startall.sh?
第六步:創(chuàng)建集群。//
1 代表一個(gè)備份
./redis-trib.rb
create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003
127.0.0.1:7004 127.0.0.1:7005? 127.0.0.1:7006
[root@bogon redis-cluster]# ./redis-trib.rb create --replicas 1
192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003
192.168.25.153:7004 192.168.25.153:7005? 192.168.25.153:7006
>>>
Creating cluster
>>>
Performing hash slots allocation on 6 nodes...
Using
3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding
replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding
replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding
replica 127.0.0.1:7004 to 127.0.0.1:7003
>>>
Trying to optimize slaves allocation for anti-affinity
[WARNING]
Some slaves are in the same host as their master
M:
019dde9da0dfd6b7ce5091c0670dcd5514a66c45 127.0.0.1:7001
slots:0-5460
(5461 slots) master
M:
f65c3fd8019d85bddb526c5ac05514c40dec0ff3 127.0.0.1:7002
slots:5461-10922
(5462 slots) master
M:
ccbc6d6fd6d7c901065728d19e487a34451b8ab3 127.0.0.1:7003
slots:10923-16383
(5461 slots) master
S:
d92bd81cb4a35a01f0bb3643f704df2c1301c4d3 127.0.0.1:7004
replicates
ccbc6d6fd6d7c901065728d19e487a34451b8ab3
S:
ceb866951d9d8c1da3099370be1f9d9f3f4556f2 127.0.0.1:7005
replicates
019dde9da0dfd6b7ce5091c0670dcd5514a66c45
S:
85be972bf84b2cfb6bba07d148a7a6c2a4cafba1 127.0.0.1:7006
replicates
f65c3fd8019d85bddb526c5ac05514c40dec0ff3
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 127.0.0.1:7001)
M:
019dde9da0dfd6b7ce5091c0670dcd5514a66c45 127.0.0.1:7001
slots:0-5460
(5461 slots) master
1
additional replica(s)
S:
85be972bf84b2cfb6bba07d148a7a6c2a4cafba1 127.0.0.1:7006
slots: (0
slots) slave
replicates
f65c3fd8019d85bddb526c5ac05514c40dec0ff3
M:
f65c3fd8019d85bddb526c5ac05514c40dec0ff3 127.0.0.1:7002
slots:5461-10922
(5462 slots) master
1
additional replica(s)
S:
ceb866951d9d8c1da3099370be1f9d9f3f4556f2 127.0.0.1:7005
slots: (0
slots) slave
replicates
019dde9da0dfd6b7ce5091c0670dcd5514a66c45
M:
ccbc6d6fd6d7c901065728d19e487a34451b8ab3 127.0.0.1:7003
slots:10923-16383
(5461 slots) master
1
additional replica(s)
S:
d92bd81cb4a35a01f0bb3643f704df2c1301c4d3 127.0.0.1:7004
slots: (0
slots) slave
replicates
ccbc6d6fd6d7c901065728d19e487a34451b8ab3
[OK]
All nodes agree about slots configuration.
>>>
Check for open slots...
>>>
Check slots coverage...
[OK]
All 16384 slots covered.
[root@bogon redis-cluster]#
2.5測(cè)試集群
在任何一個(gè)實(shí)例都可以登錄任何一個(gè)端口的數(shù)據(jù)庫中
[root@bogonredis-cluster]# redis01/redis-cli -h 192.168.25.153 -p 7002-c
? ? // -c 是不可缺少的
[root@bogon redis-cluster]# redis01/redis-cli -h 192.168.25.153 -p 7002
192.168.25.153:7002>
set a 100
(error)
MOVED 15495 192.168.25.153:7003
192.168.25.153:7002>
[root@bogon redis-cluster]# redis01/redis-cli -h 192.168.25.153 -p 7002 -c
192.168.25.153:7002>set a 100
->
Redirected to slot [15495] located at 192.168.25.153:7003
OK
192.168.25.153:7003>
2.6關(guān)閉redis
//單機(jī)客戶端關(guān)閉方式
[root@localhost redis-cluster]# redis01/redis-cli -p 7001 shutdown
//編寫了個(gè)sh腳本,將linux命令放腳本文件中順序執(zhí)行,依次關(guān)閉redis客戶端
[root@localhost redis-cluster]# vi shutdown.sh
[root@localhost redis-cluster]# chmod +x ./shutdown.sh
[root@localhost redis-cluster]# ./shutdown.sh
內(nèi)容:
查看redis客戶端信息
127.0.0.1:6379> info
或者
127.0.0.1:6379>info replication
或者
info clients