首页 > 解决方案 > 集群中的全局动态主管

问题描述

我有一个独特的问题,我不需要在 elxir 中解决。

我需要使用动态主管在集群环境中动态启动 (n) 个子级。我正在使用 libcluster 来管理集群并使用全局进程注册表来查找动态主管 pid。这是正在发生的事情:

global: Name conflict terminating {:packer_supervisor, #PID<31555.1430.0>}

这是主管的代码:

defmodule EcompackingCore.PackerSupervisor do
  use DynamicSupervisor
  require Logger

  def start_link() do
    DynamicSupervisor.start_link(__MODULE__, :ok, name: {:global, :packer_supervisor})
  end

  def init(:ok) do
    Logger.info("Starting Packer Supervisor")
    DynamicSupervisor.init(strategy: :one_for_one)
  end

  def add_packer(badge_id, packer_name) do
    child_spec = {EcompackingCore.Packer, {badge_id, packer_name}}
    DynamicSupervisor.start_child(:global.whereis_name(:packer_supervisor), child_spec)
  end

  def remove_packer(packer_pid) do
    DynamicSupervisor.terminate_child(:global.whereis_name(:packer_supervisor), packer_pid)
  end

  def children do
    DynamicSupervisor.which_children(:global.whereis_name(:packer_supervisor))
  end

  def count_children do
    DynamicSupervisor.count_children(:global.whereis_name(:packer_supervisor))
  end

end

问题似乎是在两个节点上都启动了主管。处理这个问题的最佳方法是什么?我真的需要主管是动态的,这样我才能有效地管理工作模块。可能是不同的注册表?

谢谢你的帮助。

标签: elixircluster-analysis

解决方案


经过一番研究,我找到了一个解决方案:

我现在使用https://github.com/bitwalker/swarm来处理 pid 注册。这允许跨集群设置进程,并在其中一个节点出现故障时提供切换支持。


推荐阅读