window下创建redis出现问题小结

一.准备工作

1.准备一个redis,删除目录下的,dump.rdb文件,并修改他的配置文件:redis.windows.conf

1.修改端口:我设置为 port 7001
2.放开配置:
 cluster-enabled yes
 cluster-config-file nodes-7001.conf //名称可自改
 cluster-node-timeout 15000

 

window下创建redis出现问题小结

window下创建redis出现问题小结

将这个redis复制5份,并修改相应配置,将端口设置为:7001,7002,7003,7004,7005,7006

2.配置Ruby环境、Redis的Ruby驱动redis.gem以及创建Redis集群的工具redis-trib.rb。

(1)下载Redis安装文件:http://download.redis.io/releases/,我们下载zip格式Redis-x64-3.2.1版本。

window下创建redis出现问题小结

(2)下载Ruby安装文件:http://dl.bintray.com/oneclick/rubyinstaller/rubyinstaller-2.2.4-x64.exe

(3)下载Ruby环境下Redis的驱动:https://rubygems.org/gems/redis/versions/3.2.2,考虑到兼容性,这里下载的是3.2.2版本。

下载:https://rubygems.org/downloads/redis-3.2.2.gem

window下创建redis出现问题小结

(4)下载Redis官方提供的创建Redis集群的ruby脚本文件redis-trib.rb;(我自己下载的,在文章最后,自用)

3.将所有redis文件和redis-trib.rb放在一个redis目录下:

window下创建redis出现问题小结

二.启动redis群

1.在redis-trib.rb目录下输入:后面会出现选项,选yes即可;

redis-trib.rb create --replicas 0 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

三.出现问题

1.缺少redis库:      

 解决方法:gem install redis (安装redis库)

window下创建redis出现问题小结

2.插槽15被占用,解决方法:打开每一个服务器,执行flushall、flushdb、cluster reset指令:到每个redis目录下修改:

window下创建redis出现问题小结

window下创建redis出现问题小结

四.资源

redis-trib.rb:将他复制到文本上并修改文本名称即可:

#!/usr/bin/env ruby
 
# TODO (temporary here, we'll move this into the Github issues
 when "[ER"
 color="31;1"
 when "[WA"
 color="31;1"
 when "[OK"
 color="32"
 when "[FA","***"
 color="33"
 else
 color=nil
 end
 
 color = nil if ENV['TERM'] != "xterm"
 print "\033[#{color}m" if color
 print s
 print "\033[0m" if color
 print "\n"
end
 
class ClusterNode
 def initialize(addr)
 s = addr.split(":")
 if s.length < 2
  puts "Invalid IP or Port (given as #{addr}) - use IP:Port format"
  exit 1
 end
 port = s.pop # removes port from split array
 ip = s.join(":") # if s.length > 1 here, it's IPv6, so restore address
 @r = nil
 @info = {}
 @info[:host] = ip
 @info[:port] = port
 @info[:slots] = {}
 @info[:migrating] = {}
 @info[:importing] = {}
 @info[:replicate] = false
 @dirty = false # True if we need to flush slots info into node.
 @friends = []
 end
 
 def friends
 @friends
 end
 
 def slots
 @info[:slots]
 end
 
 def has_flag?(flag)
 @info[:flags].index(flag)
 end
 
 def to_s
 "#{@info[:host]}:#{@info[:port]}"
 end
 
 def connect(o={})
 return if @r
 print "Connecting to node #{self}: " if $verbose
 STDOUT.flush
 begin
  @r = Redis.new(:host => @info[:host], :port => @info[:port], :timeout => 60)
  @r.ping
 rescue
  xputs "[ERR] Sorry, can't connect to node #{self}"
  exit 1 if o[:abort]
  @r = nil
 end
 xputs "OK" if $verbose
 end
 
 def assert_cluster
 info = @r.info
 if !info["cluster_enabled"] || info["cluster_enabled"].to_i == 0
  xputs "[ERR] Node #{self} is not configured as a cluster node."
  exit 1
 end
 end
 
 def assert_empty
 if !(@r.cluster("info").split("\r\n").index("cluster_known_nodes:1")) ||
  (@r.info['db0'])
  xputs "[ERR] Node #{self} is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0."
  exit 1
 end
 end
 
 def load_info(o={})
 self.connect
 nodes = @r.cluster("nodes").split("\n")
 nodes.each{|n|
  # name addr flags role ping_sent ping_recv link_status slots
  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] = false if master_id == "-"
 
  if info[:flags].index("myself")
  @info = @info.merge(info)
  @info[:slots] = {}
  slots.each{|s|
   if s[0..0] == '['
   if s.index("->-") # Migrating
    slot,dst = s[1..-1].split("->-")
    @info[:migrating][slot.to_i] = dst
   elsif s.index("-<-") # Importing
    slot,src = http://www.cppcns.com/shujuku/redis/s[1..-1].split("-<-")
    @info[:importing][slot.to_i] = src
   end
   elsif s.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
  } if slots
  @dirty = false
  @r.cluster("info").split("\n").each{|e|
   k,v=e.split(":")
   k = k.to_sym
   v.chop!
   if k != :cluster_state
   @info[k] = v.to_i
   else
   @info[k] = v
   end
  }
  elsif o[:getfriends]
  @friends << info
  end
 }
 end
 
 def add_slots(slots)
 slots.each{|s|
  @info[:slots][s] = :new
 }
 @dirty = true
 end
 
 def set_as_replica(node_id)
 @info[:replicate] = node_id
 @dirty = true
 end
 
 def flush_node_config
 return if !@dirty
 if @info[:replicate]
  begin
  @r.cluster("replicate",@info[:replicate])
  rescue
  # If the cluster did not already joined it is possible that
  # the slave does not know the master node yet. Somaster")
  puts "*** No such master node #{fields[0]}"
  exit 1
  end
  weights[node.info[:name]] = fields[1].to_f
 } if opt['weight']
 useempty = opt['use-empty-masters']
 
 # Assign a weight to each node, and compute the total cluster weight.
 total_weight = 0
 nodes_involved = 0
 @nodes.each{|n|
  if n.has_flag?("master")
  next if !useempty && n.slots.length == 0
  n.info[:w] = weights[n.info[:name]] ? weights[n.info[:name]] : 1
  total_weight += n.info[:w]
  nodes_involved += 1
  end
 }
 
 # Check cluster,>

window下创建redis出现问题小结

扫一扫手机访问