redis集群安装部署.docx
《redis集群安装部署.docx》由会员分享,可在线阅读,更多相关《redis集群安装部署.docx(48页珍藏版)》请在冰豆网上搜索。
![redis集群安装部署.docx](https://file1.bdocx.com/fileroot1/2023-1/21/1acba2d8-80c9-453d-b063-71d9d2dc2032/1acba2d8-80c9-453d-b063-71d9d2dc20321.gif)
redis集群安装部署
1.安装软件
Redis集群的创建需要两个必备软件
Redis
Ruby
2.Reids安装
官方网站:
http:
//redis.io/
windows版下载:
下载3.0版本
下载后解压,可以看到多个版本,我们这里使用redis-64.3.0.503版本,将这个版本解压后,拷贝到C盘目录下
看到如下内容
redis-benchmark.exe:
性能测试,用以模拟同时由多个个客户端发送多个SETs/GETs查询
redis-check-aof.exe:
更新日志检查
redis-check-dump.exe:
本地数据库检查
redis-cli.exe:
客户端
redis-server.exe:
服务端
这里我们创建一个6个节点的服务器集群,将redis.windows-service.conf复制6份,分别命名为
redis.7001.conf,redis.7002.conf,redis.7003.conf,redis.7004.conf,redis.7005.conf,redis.7006.conf
修改文件内容如下
redis.7001.conf
port7001
appendonlyyes
appendfilename"appendonly.7001.aof"
cluster-enabledyes
cluster-config-filenodes-7001.conf
cluster-node-timeout15000
cluster-slave-validity-factor10
cluster-migration-barrier1
cluster-require-full-coverageyes
redis.7002.conf内容
port7002
appendonlyyes
appendfilename"appendonly.7002.aof"
cluster-enabledyes
cluster-config-filenodes-7002.conf
cluster-node-timeout15000
cluster-slave-validity-factor10
cluster-migration-barrier1
cluster-require-full-coverageyes
redis.7003.conf内容
port7003
appendonlyyes
appendfilename"appendonly.7003.aof"
cluster-enabledyes
cluster-config-filenodes-7003.conf
cluster-node-timeout15000
cluster-slave-validity-factor10
cluster-migration-barrier1
cluster-require-full-coverageyes
redis.7004.conf内容
port7004
appendonlyyes
appendfilename"appendonly.7004.aof"
cluster-enabledyes
cluster-config-filenodes-7004.conf
cluster-node-timeout15000
cluster-slave-validity-factor10
cluster-migration-barrier1
cluster-require-full-coverageyes
redis.7005.conf内容
port7005
appendonlyyes
appendfilename"appendonly.7005.aof"
cluster-enabledyes
cluster-config-filenodes-7005.conf
cluster-node-timeout15000
cluster-slave-validity-factor10
cluster-migration-barrier1
cluster-require-full-coverageyes
redis.7006.conf内容
port7006
appendonlyyes
appendfilename"appendonly.7006.aof"
cluster-enabledyes
cluster-config-filenodes-7006.conf
cluster-node-timeout15000
cluster-slave-validity-factor10
cluster-migration-barrier1
cluster-require-full-coverageyes
启动服务
打开6个CMD命令行工具,切换到C:
\redis-64.3.0.503目录下,分别输入
Redis-server.exeredis.7001.conf
Redis-server.exeredis.7002.conf
Redis-server.exeredis.7003.conf
Redis-server.exeredis.7004.conf
Redis-server.exeredis.7005.conf
Redis-server.exeredis.7006.conf
启动成功后界面如下
3.Ruby安装
下载地址:
安装界面如下所示
安装完成后,打开命令输入ruby–v命令检查是否安装成功
4.安装redis的ruby运行库
打开命令行输入geminstallredis命令
5.下载redis-trib.rb脚本文件
redis-trib.rb文件是运行redis集群的脚本库,文件内容如下,可将下面的文件内容拷贝到记事本中另存为redis-trib.rb文件
然后将redis-trib.rb拷贝到C:
\redis-64.3.0.503目录下
#!
/usr/bin/envruby
#TODO(temporaryhere,we'llmovethisintotheGithubissuesonce
#redis-tribinitialimplementationiscompleted).
#
#-Makesurethatiftherehashingfailsinthemiddleredis-tribwilltry
#torecover.
#-Whenredis-tribperformsaclustercheck,ifitdetectsaslotmovein
#progressitshouldprompttheusertocontinuethemovefromwhereit
#stopped.
#-GracefullyhandleCtrl+Cinmove_slottoprompttheuserifreallystop
#whilerehashing,andperformingthebestcleanuppossibleiftheuser
#forcesthequit.
#-Whendoing"fix"setaglobalFixtotrue,andprompttheuserto
#fixtheproblemifautomaticallyfixableeverytimethereissomething
#tofix.Forinstance:
#1)Ifthereisanodethatpretendtoreceiveaslot,ortomigratea
#slot,buthasnoentriesinthatslot,fixit.
#2)Ifthereisanodehavingkeysinslotsthatarenotownedbyit
#fixthisconditionmovingtheentriesinthesamenode.
#3)Performmorepossiblyslowtestsaboutthestateofthecluster.
#4)Whenabortedslotmigrationisdetected,fixit.
require'rubygems'
require'redis'
ClusterHashSlots=16384
defxputs(s)
cases[0..2]
when">>>"
color="29;1"
when"[ER"
color="31;1"
when"[OK"
color="32"
when"[FA","***"
color="33"
else
color=nil
end
color=nilifENV['TERM']!
="xterm"
print"\033[#{color}m"ifcolor
prints
print"\033[0m"ifcolor
print"\n"
end
classClusterNode
definitialize(addr)
s=addr.split(":
")
ifs.length<2
puts"InvalidIPorPort(givenas#{addr})-useIP:
Portformat"
exit1
end
port=s.pop#removesportfromsplitarray
ip=s.join(":
")#ifs.length>1here,it'sIPv6,sorestoreaddress
@r=nil
@info={}
@info[:
host]=ip
@info[:
port]=port
@info[:
slots]={}
@info[:
migrating]={}
@info[:
importing]={}
@info[:
replicate]=false
@dirty=false#Trueifweneedtoflushslotsinfointonode.
@friends=[]
end
deffriends
@friends
end
defslots
@info[:
slots]
end
defhas_flag?
(flag)
@info[:
flags].index(flag)
end
defto_s
"#{@info[:
host]}:
#{@info[:
port]}"
end
defconnect(o={})
returnif@r
print"Connectingtonode#{self}:
"
STDOUT.flush
begin
@r=Redis.new(:
host=>@info[:
host],:
port=>@info[:
port],:
timeout=>60)
@r.ping
rescue
xputs"[ERR]Sorry,can'tconnecttonode#{self}"
exit1ifo[:
abort]
@r=nil
end
xputs"OK"
end
defassert_cluster
info=@r.info
if!
info["cluster_enabled"]||info["cluster_enabled"].to_i==0
xputs"[ERR]Node#{self}isnotconfiguredasaclusternode."
exit1
end
end
defassert_empty
if!
(@r.cluster("info").split("\r\n").index("cluster_known_nodes:
1"))||
(@r.info['db0'])
xputs"[ERR]Node#{self}isnotempty.Eitherthenodealreadyknowsothernodes(checkwithCLUSTERNODES)orcontainssomekeyindatabase0."
exit1
end
end
defload_info(o={})
self.connect
nodes=@r.cluster("nodes").split("\n")
nodes.each{|n|
#nameaddrflagsroleping_sentping_recvlink_statusslots
split=n.split
name,addr,flags,master_id,ping_sent,ping_recv,config_epoch,link_status=split[0..6]
slots=split[8..-1]
info={
:
name=>name,
:
addr=>addr,
:
flags=>flags.split(","),
:
replicate=>master_id,
:
ping_sent=>ping_sent.to_i,
:
ping_recv=>ping_recv.to_i,
:
link_status=>link_status
}
info[:
replicate]=falseifmaster_id=="-"
ifinfo[:
flags].index("myself")
@info=@info.merge(info)
@info[:
slots]={}
slots.each{|s|
ifs[0..0]=='['
ifs.index("->-")#Migrating
slot,dst=s[1..-1].split("->-")
@info[:
migrating][slot.to_i]=dst
elsifs.index("-<-")#Importing
slot,src=s[1..-1].split("-<-")
@info[:
importing][slot.to_i]=src
end
elsifs.index("-")
start,stop=s.split("-")
self.add_slots((start.to_i)..(stop.to_i))
else
self.add_slots((s.to_i)..(s.to_i))
end
}ifslots
@dirty=false
@r.cluster("info").split("\n").each{|e|
k,v=e.split(":
")
k=k.to_sym
v.chop!
ifk!
=:
cluster_state
@info[k]=v.to_i
else
@info[k]=v
end
}
elsifo[:
getfriends]
@friends<end
}
end
defadd_slots(slots)
slots.each{|s|
@info[:
slots][s]=:
new
}
@dirty=true
end
defset_as_replica(node_id)
@info[:
replicate]=node_id
@dirty=true
end
defflush_node_config
returnif!
@dirty
if@info[:
replicate]
begin
@r.cluster("replicate",@info[:
replicate])
rescue
#Iftheclusterdidnotalreadyjoineditispossiblethat
#theslavedoesnotknowthemasternodeyet.Soonerrors
#wereturnASAPleavingthedirtyflagset,toflushthe
#configlater.
return
end
else
new=[]
@info[:
slots].each{|s,val|
ifval==:
new
new<
@info[:
slots][s]=true
end
}
@r.cluster("addslots",*new)
end
@dirty=false
end
definfo_string
#Wewanttodisplaythehashslotsassignedtothisnode
#asranges,likein:
"1-5,8-9,20-25,30"
#
#Note:
thiscouldbeeasilywrittenwithoutsideeffects,
#weuse'slots'justtosplitthecomputationintosteps.
#Firststep:
wewantanincreasingarrayofintegers
#forinstance:
[1,2,3,4,5,8,9,20,21,22,23,24,25,30]
slots=@info[:
slots].keys.sort
#Aswewanttoaggregateadjacentslotsweconvertallthe
#slotintegersintoranges(withjustoneelement)
#Sowehavesomethinglike[1..1,2..2,...andsoforth.
slots.map!
{|x|x..x}
#Finallywegrouprangeswithadjacentelements.
slots=slots.reduce([]){|a,b|
if!
a.empty?
&&b.first==(a[-1].last)+1
a[0..-2]+[(a[-1].first)..(b.last)]
else
a+[b]
end
}
#Nowourtaskiseasy,wejustconvertrangeswithjustone
#elementintoanumber,andarealrangeintoastart-endformat.
#Finallywejointhearrayusingthecommaasseparator.
slots=slots.map{|x|
x.count==1?
x.first.to_s:
"#{x.first}-#{x.last}"
}.join(",")
role=self.has_flag?
("master")?
"M":
"S"
ifself.info[:
replicate]and@dirty
is="S:
#{self.info[:
name]}#{self.to_s}"
else
is="#{role}:
#{self.info[:
name]}#{self.to_s}\n"+
"slots:
#{slots}(#{self.slots.length}slots)"+
"#{(self.info[:
flags]-["myself"]).join(",")}"
end
ifself.info[:
replicate]
is+="\nreplicates#{info[:
replicate]}"
elsifself.has_flag?
("master")&&self.info[:
replicas]
is+="\n#{info[:
replicas].length}additionalreplica(s)"
end
is
end
#Returnasinglestringrepresentingnodesandassociatedslots.
#TODO:
removeslavesfromconfigwhenslaveswillbehandled
#byRedisCluster.
defget_config_signature
config=[]
@r.cluster("nodes").each_line{|l|
s=l.split
slots=s[8..-1].select{|x|x[0..0]!
="["}
nextifslots.length==0
config<
"+(slots.sort.join(","))
}
config.sort.join("|")
end
definfo
@info
end
defis_dirty?
@dirty
end
defr
@r
end
end
classRedisTrib
definitialize
@nodes=[]
@fix=false
@errors=[]
end
defcheck_arity(req_args,num_args)
if((req_args>0andnum_args!
=req_args)||
(req_args<0andnum_argsxputs"[ERR]Wrongnumberofargumentsforspecifiedsubcommand"
exit1
end
end
defadd_node(node)
@nodes<end
defcluste