resque故障转移redis的解决方案 [英] Solutions for resque failover redis
本文介绍了resque故障转移redis的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于集群 Redis 仍在开发中,Resque 中是否有机制可以在主节点宕机时自动故障转移到 Redis 从节点?
Since clustered Redis is still in the works, are there mechanisms in Resque that automatically will failover to a Redis slave should the master ever go down?
推荐答案
我不这么认为.但是,您可以使用 自己实现主选举机制.apache.org/" rel="nofollow">Apache Zookeeper:
I don't think so. However, you can implement the master election mechanism yourself quite easily using 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
您可以将上面的脚本修改为:
You could modify the script above to:
- 使用redis服务器的IP/端口代替进程的PID
- 使用 redis-cli 和 SLAVEOF 命令来处理成为主人"、主人改变"和不再掌握"场景.
- Use IP/port of the redis server instead of PID of the process
- Use redis-cli along with the SLAVEOF command to handle "became master", "master changed" and "no longer master" scenarions.
这篇关于resque故障转移redis的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文