2017-03-16 100 views
0

这可能是duplicate是否有使用“Akkling.Cluster.Sharding”在演员之间发送消息的示例?

因此,我已经取得了一些进展。但是,我认为将reference documentation from the C# API解释为期望的Akka.FSharp API具有挑战性。

有没有使用“Akkling.Cluster.Sharding”在actor之间发送消息的例子?

截至目前,我只能从我的客户端程序发送消息,而不是演员。

let consumer (actor:Actor<_>) msg = 
    printfn "\n%A received %A" (actor.Self.Path.ToStringWithAddress()) (box msg) |> string |> ignored 

let system1 = System.create "cluster-system" (configurePort 2551) 
let shardRegion1 = spawnSharded id system1 "printer" <| props (actorOf2 consumer) 

shardRegion1 <! ("shard-1", "entity-1", "hello world 1") 

上面的代码工作。但是,它仅适用于字符串作为消息。我仍然在努力让演员通过各种类型的消息向对方发送消息。

注:

我得到了Akka.Persistence.SqlServer插件工作。

不过,我不是如何改造Akkling.Cluster.Sharding内following setup明确:

open Akka.FSharp 
let clusterHostActor = 
    spawn system1 nodeName <| fun (inbox: Actor<_>) -> 
     let cluster = Cluster.Get system1 
     cluster.Subscribe(inbox.Self, [| typeof<ClusterEvent.IClusterDomainEvent> |]) 
     inbox.Defer(fun() -> cluster.Unsubscribe(inbox.Self)) 
     let rec messageLoop() = 
      actor { 
       let! message = inbox.Receive()       
       match box message with 
       | :? ClusterEvent.MemberUp  as event -> printfn "Member %s Joined the Cluster at %O" event.Member.Address.Host DateTime.Now 
                  let sref = select (event.Member.Address.ToString() + "/user/listener") inbox 
                  sref <! "Hello from clusterHostActor" 
       | :? ClusterEvent.MemberRemoved as event -> printfn "Member %s Left the Cluster at %O" event.Member.Address.Host DateTime.Now 
       | other ->         printfn "Cluster Received event %O at %O" other DateTime.Now 

       return! messageLoop() 
      } 
     messageLoop() 

具体来说,我的印象是一个碎片区域是在分片集群系统中所需的下为了在演员之间来回发送消息。

作为这个范例的新手,我正努力在使用分片功能的两位演员之间创建一个简单的“hello world”类型的消息程序。

有什么建议吗?

回答

0

如果您希望您的分片节点成为分片演员的有效主机/容器,则它必须正在运行与该演员类型关联的分片区域。发送给分片演员的所有消息都通过shardRegion引用发送。

在第一个示例代码片段中,您已经表明,字符串消息是唯一有效的消息类型,可能是因为您的行为将字符串作为唯一有效的消息类型。

正如您在spawnShardeddefinition中看到的那样,它需要4个参数。这里最重要的是第一个,这是一个函数,用于解析分片插件所需的所有信息,以便将消息路由到有效的actor /实体。此方法返回的元组,其中:

  • 第一个元素是碎片的标识符,目标实体住在
  • 第二个元素是实体本身在其碎片的范围的标识符。因此,为了唯一识别集群内所有其他实体之间的实体,它必须提供唯一的分片ID /实体ID对。
  • 第三个参数是一个实际的消息,它将被发送到一个实体。它的类型应该匹配用作演员行为的递归循环函数输入的类型 - 如果你已经显示,它可能是一个普通的对象。

由于在这个例子中,这个消息解析器函数是id(identity),我们直接发送一个元组到分片区域。您可以将该功能更改为您想要指定自定义的其他任何内容。

PS:如果您还有其他问题,可以通过Akka.NET gitter channel找到帮助。

相关问题