TOP

elixir 高可用系列(五) Supervisor(二)
2017-10-09 13:35:00 】 浏览:3830
Tags:elixir 可用 系列 Supervisor

'ServerA PID: <0.146.0>' iex(3)> GenServer.call(:server_b, :display) 'ServerB PID: <0.147.0>' iex(4)> GenServer.call(:server_c, :display) 'ServerC PID: <0.148.0>' # 通过消息 :err 让 serverA 出错 iex(5)> GenServer.cast(:server_a, :err) :ok iex(6)> 14:55:16.183 [error] GenServer :server_a terminating ** (stop) "stop ServerA" Last message: {:"$gen_cast", :err} State: [] nil # serverA 出错后,再次查看3个process的PID,发现 supervisor 重启了所有 process,符合策略 :one_for_all iex(7)> GenServer.call(:server_a, :display) 'ServerA PID: <0.153.0>' iex(8)> GenServer.call(:server_b, :display) 'ServerB PID: <0.154.0>' iex(9)> GenServer.call(:server_c, :display) 'ServerC PID: <0.156.0>'

监督树

监督者并不是一维的,监督者也可以监督其它监督者,从而形成树状的监督关系。

修改上面的测试代码如下:(只修改了 Supervisor 的部分)

defmodule PseudoServerA do
  use GenServer

  def start_link(state, opts \\ []) do
    GenServer.start_link(__MODULE__, state, opts)
  end

  def handle_call(:display, _from, []) do
    {:reply, 'ServerA PID: ' ++ :erlang.pid_to_list(self()), []}
  end

  def handle_cast(:err, []) do
    {:stop, "stop ServerA", []}
  end
end

defmodule PseudoServerB do
  use GenServer

  def start_link(state, opts \\ []) do
    GenServer.start_link(__MODULE__, state, opts)
  end

  def handle_call(:display, _from, []) do
    {:reply, 'ServerB PID: ' ++ :erlang.pid_to_list(self()), []}
  end

  def handle_cast(:err, []) do
    {:stop, "stop ServerB", []}
  end
end

defmodule PseudoServerC do
  use GenServer

  def start_link(state, opts \\ []) do
    GenServer.start_link(__MODULE__, state, opts)
  end

  def handle_call(:display, _from, []) do
    {:reply, 'ServerC PID: ' ++ :erlang.pid_to_list(self()), []}
  end

  def handle_cast(:err, []) do
    {:stop, "stop ServerC", []}
  end
end

defmodule SupervisorBranch do
  import Supervisor.Spec

  def start_link(state) do
    children = [
      worker(PseudoServerA, [[], [name: :server_a]]),
      worker(PseudoServerB, [[], [name: :server_b]]),
    ]

    Supervisor.start_link(children, strategy: :one_for_one)
  end

end

defmodule SupervisorRoot do
  import Supervisor.Spec

  def init() do
    children = [
      supervisor(SupervisorBranch, [[name: :supervisor_branch]]),
      worker(PseudoServerC, [[], [name: :server_c]])
    ]

    # Start the supervisor with children
    Supervisor.start_link(children, strategy: :one_for_all)
  end

end

测试流程如下:

# 启动 根 监督者 
iex(1)> SupervisorRoot.init
{:ok, #PID<0.149.0>}

# 启动后,查看 3 个process 的PID
iex(2)> GenServer.call(:server_a, :display)
'ServerA PID: <0.151.0>'
iex(3)> GenServer.call(:server_b, :display)
'ServerB PID: <0.152.0>'
iex(4)> GenServer.call(:server_c, :display)
'ServerC PID: <0.153.0>'

# 通过消息 :err 让 serverA 出错
iex(5)> GenServer.cast(:server_a, :err)
:ok
iex(6)>
15:31:15.846 [error] GenServer :server_a terminating
 ** (stop) "stop ServerA"
 Last message: {:"$gen_cast", :err}
 State: []

 nil

 # serverA 出错后,因为它的监督者 SupervisorBranch 的策略是 :one_for_one,所以只重启了 serverA
 iex(7)> GenServer.call(:server_a, :display)
 'ServerA PID: <0.158.0>'
 iex(8)> GenServer.call(:server_b, :display)
 'ServerB PID: <0.152.0>'
 iex(9)> GenServer.call(:server_c, :display)
 'ServerC PID: <0.153.0>'

 # 通过消息 :err 让 serverC 出错
 iex(10)> GenServer.cast  
		

请关注公众号获取更多资料


elixir 高可用系列(五) Supervisor(二) https://www.cppentry.com/bencandy.php?fid=88&id=124427

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇elixir 高可用系列(四) Task 下一篇elixir 高可用系列 - 目录