resque故障转移redis的解决方案

发布于 2024-12-10 05:08:17 字数 69 浏览 0 评论 0 原文

由于集群 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难如初 2024-12-17 05:08:17

我不这么认为。但是,您可以使用 自己实现主选举机制 .apache.org/" rel="nofollow">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 命令来处理“成为 master”、“master 改变”和“不再 master”场景。

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:

  1. Use IP/port of the redis server instead of PID of the process
  2. Use redis-cli along with the SLAVEOF command to handle "became master", "master changed" and "no longer master" scenarions.
梦里人 2024-12-17 05:08:17

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

https://github.com/ryanlecompte/redis_failover

I've released a redis_failover gem that provides Redis failover for Ruby with ZooKeeper:

https://github.com/ryanlecompte/redis_failover

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文