2016-09-03 28 views
1

如果consitent-hashing-pool路由器的HOCON如下,我該如何指定hashMapping。如何在使用Hocon時指定HashMapping

akka { 
     actor { 
     serializers { 
      wire = "Akka.Serialization.WireSerializer, Akka.Serialization.Wire" 
     } 
     serialization-bindings { 
      "System.Object" = wire 
     } 
     deployment { 
      /data-collector { 
      router = consistent-hashing-pool 
      nr-of-instances = 10 
      } 
     } 
     } 
     loggers = ["Akka.Logger.NLog.NLogLogger,Akka.Logger.NLog"] 
    } 

鑑於

let config = Configuration.load() 
let systemName = config.GetString("app-config.actorsystem", "my-system") 
let system = System.create systemName config 
let collectorRouter = 
    let hashMapping (msg:obj) = 
    match msg with 
    | :? Message as msg -> 
     match msg with 
     | Parse (_, req) -> req.uniqueId |> box 
    | _ -> "-" |> box 
    ConsistentHashingPool (10, ConsistentHashMapping hashMapping) 
let dataCollectorProps = 
    { props (dataCollector settings.collector) with 
     Router = Some (collectorRouter :> RouterConfig)} //(FromConfig.Instance.WithFallback collectorRouter) 
let test = spawn system "data-collector" <| dataCollectorProps 

Router = Some (collectorRouter :> RouterConfig)工作

Router = Some (FromConfig.Instance.WithFallback collectorRouter)

什麼是指定hashMapping功能的正確方法是什麼?

編輯1 從控制檯的警告

Akka.Routing.ConsistentHashingRoutingLogic|Message [Message] must be handled by hashMapping, or implement [IConsistentHashable] or be wrapped in [ConsistentHashableEnvelope]

回答

0

three ways of specifying the hash key。我最喜歡的是執行IConsistentHashable並從ConsistentHashKey屬性返回密鑰(基於消息的某些屬性)。

例摘自my Consistent Hashing article(在C#;對不起,我不知道F#):

public class CurrencyPriceChangeMessage : IConsistentHashable 
{ 
    public string CurrencyPair { get; } 
    public decimal Price { get; } 

    public object ConsistentHashKey 
    { 
     get 
     { 
      return this.CurrencyPair; 
     } 
    } 

散列密鑰將被用於路由消息與始終相同的密鑰來同一routee。

+0

我在hocon中將'nr-of-instances'改爲3,路由器有三條路由。哈希也起作用。在代碼和hocon中定義路由器的時候,它是從hocon中選擇一個並用代碼配置來代替路由器嗎? – ritcoder

+0

據我所知,當在代碼和HOCON中定義一個設置時,HOCON優先。 – Gigi

+0

已注意。那麼工作得很好。 – ritcoder

相關問題