我不这么认为。但是,您可以使用Apache
Zookeeper轻松轻松地实现主选举机制:
require "rubygems"require "zookeeper"def log(msg) puts "[#{Process.pid}] #{msg}"enddef debug(obj) log(obj.inspect)enddef on_master_changed(&block) loop do wcb = Zookeeper::WatcherCallback.new resp = @zookeeper.get_children(:path => @base_path, :watcher => wcb, :watcher_context => @base_path) children = resp[:children].map{|name| "#{@base_path}/#{name}"} new_master = children.sort.first block.call(new_master) while !wcb.completed? sleep(0.1) end endend@zookeeper = Zookeeper.new("localhost:2181")if @zookeeper.state != Zookeeper::ZOO_CONNECTED_STATE log 'Unable to connect to Zookeeper!' exit(1)end@base_path = "/nodes"@zookeeper.create(:path => @base_path)resp = @zookeeper.create(:path => "#{@base_path}/node-", :ephemeral => true, :sequence => true, :data => Process.pid.to_s)my_node = resp[:path]is_master = falselog "My node is: #{my_node}"on_master_changed do |new_master| if new_master == my_node if is_master log "I am still the master. Bow before me or die!" else log "I am the new master. Behold!" end is_master = true else pid = @zookeeper.get(:path => new_master)[:data] log "New master is process #{pid}" endend您可以将上面的脚本修改为:
- 使用Redis服务器的IP /端口而不是进程的PID
- 将redis-cli与SLAVEOF命令一起使用可处理“成为主服务器”,“已更改主服务器”和“不再主服务器”方案。



