gpt4 book ai didi

ruby - resque故障转移redis解决方案

转载 作者:IT王子 更新时间:2023-10-29 06:07:08 36 4
gpt4 key购买 nike

由于集群式 Redis 仍在开发中,Resque 中是否有机制可以在主服务器出现故障时自动故障转移到 Redis 从服务器?

最佳答案

我不这么认为。但是,您可以 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处理“成为主人”、“主人改变”和“不再是主人”场景的命令。

关于ruby - resque故障转移redis解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7759572/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com