简体   繁体   English

如何更改Akka应用程序的默认Serializer?

[英]How to change default Serializer for an Akka application?

I read the akka serialization page , where they talk about serialization-bindings as 我读了akka序列化页面 ,他们在那里谈论serialization-bindings

serialization-bindings {
      "java.lang.String" = java
      "docs.serialization.Customer" = java
      "com.google.protobuf.Message" = proto
      "docs.serialization.MyOwnSerializable" = myown
      "java.lang.Boolean" = myown
    }

I am considering use of kyro serialization and wondering if there is a way to turn on kyro serialization by default for complete application? 我正在考虑使用kyro序列化,并想知道是否有一种方法可以打开kyro序列化默认为完整的应用程序? rather than giving every class or package name? 而不是给每个类或包名?

Something like 就像是

serialization-bindings {
     default = kyro
}

This is the way, I use kryo: 这是方式,我使用kryo:

serializers {
  kryo = "com.romix.akka.serialization.kryo.KryoSerializer"
}

serialization-bindings {
  "com.ex.es.Msg" = kryo
  "java.io.Serializable" = none
}

Similar to Kunal all my messages, that I send over the wire / store / serialize in any way extend that Msg class. 与Kunal类似,我通过wire / store / serialize以任何方式发送的所有消息都扩展了Msg类。 That's not only because I'm not sure wether there is a way to set a default serializer, but also to make it explicit for me, what is serialized in my application. 这不仅仅是因为我不确定是否有办法设置默认的序列化程序,而且还要让我明确,我的应用程序中序列化了什么。

And now to give your actual question a try: 现在试试你的实际问题

I also had to set "java.io.Serializable" = none otherwise I got a warning, that it wasn't sure, which serializer to pick for my classes. 我还必须设置"java.io.Serializable" = none否则我收到一个警告,它不确定,哪个序列化程序选择我的类。

So this could imply, that case classes by default extend Serializable and make something like this work as a default setting: 所以这可能意味着,默认情况下,case类扩展Serializable并使这样的工作作为默认设置:

serializers {
  kryo = "com.romix.akka.serialization.kryo.KryoSerializer"
}

serialization-bindings {
  "java.io.Serializable" = kryo
}

But that is only an educated guess and at the moment I have no possibility to test. 但这只是一个有根据的猜测,目前我没有可能进行测试。

The way I do that is to make all my serializable classes extend a SerializableTrait and then in my bindings I just specify my trait to be serialized using the custom serializer. 我这样做的方法是让我的所有可序列化类扩展SerializableTrait ,然后在我的绑定中,我只是指定我的特性使用自定义序列化器进行序列化。

akka.actor {
  serializers {
    json-serializer = "io.example.MySerializerClass"
 }
  serialization-bindings {
   "io.example.SerializableTrait" = json-serializer
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM