我正在使用Akka Cluster实现一些冗余并删除我的java应用程序中的单点故障。但是,在将消息从一台计算机发送到另一台计算机时,我遇到了一些消息序列化问题。
我发送的是不实施Serializable
的java对象。因此,我需要使用自己的序列化程序,幸运的是,该对象提供了使用Kryo的序列化方法。所以,遵循这个guide我创建了自己的akka序列化程序,扩展了JSerializer
:
import akka.serialization.JSerializer;
public class MyObjectClusterSerializer extends JSerializer {
public int identifier() {
return 66666;
}
public boolean includeManifest() {
return false;
}
public byte[] toBinary(Object obj) {
MyObject myObject = (MyObject) obj;
byte[] bytes = null;
try {
bytes = MyObjectHelper.serialize(myObject);
} catch (SerializationException e) {
}
return bytes;
}
@Override
public Object fromBinaryJava(byte[] arg0, Class<?> arg1) {
MyObject myObject = null;
try {
myObject = MyObjectHelper.deserialize(arg0);
} catch (DeserializationException e) {
}
return myObject;
}
}
代码很简单,我不相信这就是问题所在。然后我将序列化程序和序列化程序绑定添加到我的配置中:
serializers {
myobject = "com.my.awesome.project.MyObjectClusterSerializer"
}
serialization-bindings {
"com.my.awesome.project.MyObject" = myobject
}
当我运行单个节点时,我没有任何问题(显然是因为我没有序列化任何东西......)。但是在集群模式下,我不断获得这些例外:
2014-08-04 01:45:27,498 ERROR [MyApp-akka.actor.default-dispatcher-16] - Slf4jLogger$$anonfun$receive$1$$anonfun$applyOrElse$1.apply$mcV$sp (Slf4jLogger.scala:66) - Transient association error (association remains live)
java.io.NotSerializableException: com.my.awesome.project.MyObject
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183) ~[?:1.7.0_60]
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547) ~[?:1.7.0_60]
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508) ~[?:1.7.0_60]
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431) ~[?:1.7.0_60]
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177) ~[?:1.7.0_60]
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347) ~[?:1.7.0_60]
at akka.serialization.JavaSerializer$$anonfun$toBinary$1.apply$mcV$sp(Serializer.scala:129) ~[akka-actor_2.11-2.3.4.jar:?]
at akka.serialization.JavaSerializer$$anonfun$toBinary$1.apply(Serializer.scala:129) ~[akka-actor_2.11-2.3.4.jar:?]
at akka.serialization.JavaSerializer$$anonfun$toBinary$1.apply(Serializer.scala:129) ~[akka-actor_2.11-2.3.4.jar:?]
at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58) ~[scala-library-2.11.1.jar:?]
at akka.serialization.JavaSerializer.toBinary(Serializer.scala:129) ~[akka-actor_2.11-2.3.4.jar:?]
at akka.remote.serialization.MessageContainerSerializer.serializeSelection(MessageContainerSerializer.scala:36) ~[akka-remote_2.11-2.3.4.jar:?]
at akka.remote.serialization.MessageContainerSerializer.toBinary(MessageContainerSerializer.scala:25) ~[akka-remote_2.11-2.3.4.jar:?]
at akka.remote.MessageSerializer$.serialize(MessageSerializer.scala:36) ~[akka-remote_2.11-2.3.4.jar:?]
at akka.remote.EndpointWriter$$anonfun$serializeMessage$1.apply(Endpoint.scala:845) ~[akka-remote_2.11-2.3.4.jar:?]
at akka.remote.EndpointWriter$$anonfun$serializeMessage$1.apply(Endpoint.scala:845) ~[akka-remote_2.11-2.3.4.jar:?]
at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58) ~[scala-library-2.11.1.jar:?]
at akka.remote.EndpointWriter.serializeMessage(Endpoint.scala:844) ~[akka-remote_2.11-2.3.4.jar:?]
at akka.remote.EndpointWriter.writeSend(Endpoint.scala:747) ~[akka-remote_2.11-2.3.4.jar:?]
at akka.remote.EndpointWriter$$anonfun$4.applyOrElse(Endpoint.scala:722) ~[akka-remote_2.11-2.3.4.jar:?]
at akka.actor.Actor$class.aroundReceive(Actor.scala:465) ~[akka-actor_2.11-2.3.4.jar:?]
at akka.remote.EndpointActor.aroundReceive(Endpoint.scala:415) ~[akka-remote_2.11-2.3.4.jar:?]
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516) [akka-actor_2.11-2.3.4.jar:?]
at akka.actor.ActorCell.invoke(ActorCell.scala:487) [akka-actor_2.11-2.3.4.jar:?]
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:238) [akka-actor_2.11-2.3.4.jar:?]
at akka.dispatch.Mailbox.run(Mailbox.scala:220) [akka-actor_2.11-2.3.4.jar:?]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:393) [akka-actor_2.11-2.3.4.jar:?]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.1.jar:?]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.1.jar:?]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.1.jar:?]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.1.jar:?]
对我而言,这意味着它无法弄清楚要使用的串行器,它实际上使用的是通用的Java序列化器。此外,我不知道它是否相关,但我使用ConsistentHashableEnvelope包装我的消息,因为我将其发送到支持群集的一致性散列组。
任何有助于使这件事有用的帮助?我错过了什么吗?
答案 0 :(得分:3)
Akka通过消息类选择一次串行器(请参阅堆栈跟踪)。在您的情况下,akka正确使用 JavaSerializer ,因为基本消息具有ConsistentHashableEnvelope类型,并将此序列化程序应用于所有字段(消息和 hashKey )。
要解决您的任务,只需为 ConsistentHashableEnvelope 创建serlializer,并仅在消息字段具有 MyObject 类型和 JavaSerializer 时使用 MyObjectClusterSerializer em>在其他情况下