第 5 章 将应用程序迁移到 Data Grid 8
5.1. Data Grid 8 中的 Marshalling 复制链接链接已复制到粘贴板!
Marshalling 功能在 Data Grid 8 中被显著重构,以隔离内部对象和用户对象。
因为 Data Grid 现在处理内部类的划分,所以在使用嵌入式或远程缓存配置 marshallers 时,不再需要处理这些内部类。
5.1.1. ProtoStream marshalling 复制链接链接已复制到粘贴板!
默认情况下,Data Grid 8 使用 ProtoStream API 将所有数据作为协议缓冲区(一种语言中立且向后兼容的格式)。
protobuf 编码是一种模式定义格式,现在是许多应用程序的默认标准,与 JBoss Marshalling 相比,转码数据时具有更大的灵活性,这是 Data Grid 7 中的默认设置。
因为 ProtoStream marshaller 基于 Protobuf 格式,所以数据网格可以在不先转换为 Java 对象的情况下转换为其他编码。使用 JBoss Marshalling 时,需要在转换为任何其他格式前将键和值转换为 Java 对象。
作为迁移到 Data Grid 8 的一部分,您应该为您的 Java 类使用 ProtoStream marshalling。
在高级别中,要使用 ProtoStream marshaller,您可以使用 ProtoStream 处理器生成 SerializationContextInitializer
实现。首先,您可以在 Java 类中添加 @Proto
注解,然后使用 Data Grid 提供的 ProtoStream 处理器来生成包含以下内容的序列化上下文:
-
.proto
模式,提供 Java 对象的结构化表示,作为 Protobuf 消息类型。 - Marshaller 实现将 Java 对象编码为 Protobuf 格式。
根据您使用的嵌入式还是远程缓存,Data Grid 可以自动注册 SerializationContextInitializer
实现。
Data Grid Server 的 Marshalling
您应该只对远程缓存使用 Protobuf 编码,并将 ProtoStream marshaller 用于任何自定义类型。
其他 marshaller 实现(如 JBoss marshalling)需要您使用与 Data Grid CLI、Data Grid Console 或 Ickle 查询不兼容的不同缓存编码。
缓存存储和 ProtoStream
在 Data Grid 7.x 中,您保留至缓存存储的数据与 Data Grid 8 中的 ProtoStream marshaller 不兼容。您必须使用 StoreMigrator
工具将数据从任何 Data Grid 7.x 缓存存储迁移到 Data Grid 8 缓存存储。
5.1.2. 备用 marshaller 实现 复制链接链接已复制到粘贴板!
Data Grid 提供替代的 marshaller 实现,到 ProtoStream 有助于简化从旧版本进行迁移。在迁移到 ProtoStream marshalling 时,您应该只使用这些替代的 marshallers 作为中间解决方案。
对于新项目,红帽强烈建议您只使用 ProtoStream marshalling 以避免将来升级或迁移出现任何问题。
JBoss marshalling
在 Data Grid 7 中,JBoss Marshalling 是默认的 marshaller。在 Data Grid 8 中,ProtoStream marshalling 是默认值。
如果您有客户端需要使用 Java 序列化要求,您应该使用 JavaSerializationMarshaller
而不是 JBoss Marshalling。
如果需要在迁移到 Data Grid 8 的过程中使用 JBoss Marshalling 作为临时解决方案,请执行以下操作:
嵌入式缓存
-
将
infinispan-jboss-marshalling
依赖项添加到您的 classpath。 将 Data Grid 配置为使用
JBossUserMarshaller
,例如:<serialization marshaller="org.infinispan.jboss.marshalling.core.JBossUserMarshaller"/>
<serialization marshaller="org.infinispan.jboss.marshalling.core.JBossUserMarshaller"/>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 将您的类添加到 Data Grid 允许进行序列化的类列表中。
远程缓存
如果 infinispan-jboss-marshalling
模块位于 classpath 上,则数据网格服务器不支持 JBoss Marshalling,并且 GenericJBossMarshaller
不再被自动配置。
您必须将 Hot Rod Java 客户端配置为使用 JBoss Marshalling,如下所示:
RemoteCacheManager
.marshaller("org.infinispan.jboss.marshalling.commons.GenericJBossMarshaller");
.marshaller("org.infinispan.jboss.marshalling.commons.GenericJBossMarshaller");
Copy to Clipboard Copied! Toggle word wrap Toggle overflow hotrod-client.properties
infinispan.client.hotrod.marshaller = GenericJBossMarshaller
infinispan.client.hotrod.marshaller = GenericJBossMarshaller
Copy to Clipboard Copied! Toggle word wrap Toggle overflow