resque故障转移redis的解决方案

时间:2011-10-13 19:42:21

标签: ruby redis resque

由于群集Redis仍然在工作,Resque中是否有机制会在主服务器出现故障时自动故障转移到Redis从服务器?

2 个答案:

答案 0 :(得分:1)

我不这么认为。但是,您可以implement the master election mechanism yourself轻松使用Apache Zookeeper

require "rubygems"
require "zookeeper"

def log(msg)
  puts "[#{Process.pid}] #{msg}"
end

def debug(obj)
  log(obj.inspect)
end

def 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
  end
end

@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 = false

log "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}"
  end
end

您可以将上面的脚本修改为:

  1. 使用redis服务器的IP /端口而不是进程的PID
  2. 使用redis-cli和SLAVEOF命令来处理“成为主人”,“主人改变”和“不再掌握”的情况。

答案 1 :(得分:0)

我发布了一个redis_failover gem,它使用ZooKeeper为Ruby提供Redis故障转移:

https://github.com/ryanlecompte/redis_failover