2

我正在创建一个Akka.Cluster包含三个节点 A、B 和 C 的示例,其中 A 是灯塔。到目前为止,从日志来看,当没有演员或演员是本地的(使用spawnand创建spawnOpt)时,集群可以正常工作。我想从 B 创建一个演员并从 C 访问它。

let _ = spawne system "r-actor" <@ actorOf (fun msg -> printfn "Received: %s" msg) @> []

我明白了

2016-08-31 01:59:00.1185|INFO|Akka.Actor.EmptyLocalActorRef|Message String from akka://calculatorSystem/deadLetters to akka://calculatorSystem/user/r-actor was not delivered. 1 dead letters encountered.

使用

let r = FromConfig.Instance :> RouterConfig |> SpawnOption.Router
let _ = spawne system "r-actor" <@ actorOf (fun msg -> printfn "Received: %s" msg) @> [r]

抛出异常

Akka.Configuration.ConfigurationExceptionAkka.dll 中发生未处理的类型异常
附加信息:Configuration problem while creating [akka://calculatorSystem/user/r-actor] with router dispatcher [akka.actor.default-dispatcher] and mailbox and routee dispatcher [akka.actor.default-dispatcher] and mailbox [].

节点 C 上的测试函数为

let rec calculate content =
  printfn "Processing %s" content
  let actor = select "/user/r-actor" system
  actor <! content
  let text = Console.ReadLine()
  if text <> "quit" then
    calculate text
calculate "sample1"

HOCON(节点 B)

    akka {
      actor {
        provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"
        serializers {
          wire = "Akka.Serialization.WireSerializer, Akka.Serialization.Wire"
        }
        serialization-bindings {
          "System.Object" = wire
        }
        deployment {
          /user/add {
            router = round-robin-pool
            nr-of-instances = 10
            cluster {
              enabled = on
              max-nr-of-instances-per-node = 10
              allow-local-routees = off
            }
          }
          /user/r-actor {
            router = round-robin-pool
            nr-of-instances = 10
            cluster {
              enabled = on
              max-nr-of-instances-per-node = 10
              allow-local-routees = off
            }
          }
        }
      }
      remote {
        log-remote-lifecycle-events = DEBUG
        log-received-messages = on
        helios.tcp {
          transport-class = "Akka.Remote.Transport.Helios.HeliosTcpTransport, Akka.Remote"
          applied-adapters = []
          transport-protocol = tcp
          hostname = "127.0.0.1"
          port = 0
        }
      }
      loggers = ["Akka.Logger.NLog.NLogLogger,Akka.Logger.NLog"]
      cluster {
        seed-nodes = [
          "akka.tcp://calculatorSystem@127.0.0.1:7001"
        ]
        roles = ["add-service"]
        auto-down-unreachable-after = 10s
      }
    }

如何创建可由集群中的另一个节点调用的参与者?

4

1 回答 1

2

定义路由器配置时,不要使用/user前缀 - 它是自动添加的。

此外,如果您想选择驻留在另一个节点上的演员,您需要使用完整的演员路径(带有节点地址) - 这是因为事实上,可能有不同的演员生活在具有相同路径的不同节点上。你必须准确。

通常在编译时您不会知道节点的地址。有两种方法可以提取它:

1.从集群状态获取地址

这更容易,但您失去了对加入/离开节点做出反应的能力。你不得不每次都检查它,这很慢。此示例用于ActorSelection.ResolveOne检索实际IActorRef实例。

async {
    let members = Cluster.Get(system).State.Members
    let actorRefs =
        members 
        |> Seq.filter (fun m -> m.Roles.Contains("expected")) // use roles to filter out nodes that shouldn't be checked
        |> Seq.map (fun m -> 
            let selection = select (m.Address.ToString() + "user/r-actor") system
            let actorRef = selection.ResolveOne(timeout) |> Async.AwaitTask)
        |> Async.Parallel }

2. 订阅集群事件并对加入/离开节点做出反应

在这里,您可以在节点加入/离开时对其做出反应。它还使用Identify/ActorIdentity接收实际IActorRef,这是更快的选择。

let aref =  
    spawn system "listener"
    <| fun mailbox ->
        let cluster = Cluster.Get (mailbox.Context.System)
        cluster.Subscribe (mailbox.Self, [| typeof<ClusterEvent.IMemberEvent> |])
        mailbox.Defer <| fun () -> cluster.Unsubscribe (mailbox.Self)
        let rec loop () = 
            actor {
                let! (msg: obj) = mailbox.Receive ()
                match msg with
                | :? ClusterEvent.MemberUp as up -> 
                    // new node joined the cluster
                    let selection = select (up.Member.Address.ToString() + "user/r-actor") mailbox
                    selection <! Identify(null) // request actor under selection to identify itself
                | :? ActorIdentity as id when id.Subject <> null ->
                    // actor has identified itself
                    id.Subject <! "hello"
                | :? ClusterEvent.MemberRemoved as rem -> 
                    //  node leaved the cluster, invalidate all actors from that node
                | _ -> ()
                return! loop () }
        loop ()

如有疑问,我写了一篇关于从 F# 创建 Akka.NET 集群的博客文章。也许你会发现它很有用。

于 2016-08-31T08:21:07.633 回答