5.3. 配置替代 Marshaller 实施
Data Grid 提供 Marshaller 实现,您可以使用它而不是 ProtoStream。您还可以将 Data Grid 配置为使用自定义 marshaller 实现。
5.3.1. 使用 JBoss Marshalling 复制链接链接已复制到粘贴板!
JBoss Marshalling 是一个基于序列化的 marshalling 库,是之前的 Data Grid 版本中的默认 marshaller。
- 您不应该在 Data Grid 中使用基于序列化的 marshalling。反之,您应该使用 Protostream,它是一个高性能的二进制有线格式,以确保向后兼容。
-
JBoss Marshalling 和
AdvancedExternalizer接口已弃用,并将在以后的发行版本中删除。但是,除非您使用 JBoss Marshalling,否则数据网格会忽略高级外部工具实现。
流程
-
将
infinispan-jboss-marshalling依赖项添加到您的 classpath。 -
将 Data Grid 配置为使用
GenericJBossMarshaller。 将您的 Java 类添加到反序列化白名单中。
以编程方式:
GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder(); builder.serialization() .marshaller(new GenericJBossMarshaller()) .whiteList() .addRegexps("org.infinispan.example.", "org.infinispan.concrete.SomeClass");GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder(); builder.serialization() .marshaller(new GenericJBossMarshaller()) .whiteList() .addRegexps("org.infinispan.example.", "org.infinispan.concrete.SomeClass");Copy to Clipboard Copied! Toggle word wrap Toggle overflow 声明:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
5.3.2. 使用 Java Serialization 复制链接链接已复制到粘贴板!
您可以将 Java 序列化与 Data Grid 搭配使用,以汇总您的对象,但只有 Java 对象实现 Java Serializable 接口。
流程
-
将 Data Grid 配置为使用
JavaSerializationMarshaller作为 marshaller。 将您的 Java 类添加到反序列化白名单中。
以编程方式:
GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder(); builder.serialization() .marshaller(new JavaSerializationMarshaller()) .whiteList() .addRegexps("org.infinispan.example.", "org.infinispan.concrete.SomeClass");GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder(); builder.serialization() .marshaller(new JavaSerializationMarshaller()) .whiteList() .addRegexps("org.infinispan.example.", "org.infinispan.concrete.SomeClass");Copy to Clipboard Copied! Toggle word wrap Toggle overflow 声明:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
5.3.3. 使用 Kryo Marshaller 复制链接链接已复制到粘贴板!
Data Grid 提供了一个使用 Kryo 库的 marshalling 实现。
Data Grid Servers 的先决条件
要将 Kryo marshalling 与 Data Grid 服务器搭配使用,请添加 JAR,其中包含 Kryo marshalling 实现的运行时类文件,如下所示:
-
从 Data Grid Maven 存储库复制
infinispan-marshaller-kryo-bundle.jar。 -
将 JAR 文件添加到 Data Grid 服务器安装目录中的
server/lib目录中。
Data Grid Library Mode 的先决条件
要将 Kryo marshalling 与 Data Grid 搭配使用,作为应用程序中的嵌入式库,请执行以下操作:
将
infinispan-marshaller-kryo依赖项添加到您的pom.xml。<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-marshaller-kryo</artifactId> <version>${version.infinispan}</version> </dependency><dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-marshaller-kryo</artifactId> <version>${version.infinispan}</version> </dependency>Copy to Clipboard Copied! Toggle word wrap Toggle overflow 将
org.infinispan.marshaller.kryo.KryoMarshaller类指定为 marshaller。GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder(); builder.serialization() .marshaller(new org.infinispan.marshaller.kryo.KryoMarshaller());GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder(); builder.serialization() .marshaller(new org.infinispan.marshaller.kryo.KryoMarshaller());Copy to Clipboard Copied! Toggle word wrap Toggle overflow
流程
-
为
SerializerRegistryService.java接口实施服务提供商。 将所有序列化器注册放在
寄存器(Kryo)方法中;其中 serializers 使用KryoAPI 向提供的 Kryo 对象注册,例如:kryo.register(ExampleObject.class, new ExampleObjectSerializer())
kryo.register(ExampleObject.class, new ExampleObjectSerializer())Copy to Clipboard Copied! Toggle word wrap Toggle overflow 指定在部署 JAR 文件中实施类的完整路径:
META-INF/services/org/infinispan/marshaller/kryo/SerializerRegistryService
META-INF/services/org/infinispan/marshaller/kryo/SerializerRegistryServiceCopy to Clipboard Copied! Toggle word wrap Toggle overflow
5.3.4. 使用 Protostuff Marshaller 复制链接链接已复制到粘贴板!
Data Grid 提供了一个 marshalling 实现,它使用 Protostuff 库。
Data Grid Servers 的先决条件
要将 Protostuff marshalling 与 Data Grid 服务器一起使用,请添加 JAR,其中包含 Protostuff marshalling 实现的运行时类文件,如下所示:
-
从 Data Grid Maven 存储库复制
infinispan-marshaller-protostuff-bundle.jar。 -
将 JAR 文件添加到 Data Grid 服务器安装目录中的
server/lib目录中。
Data Grid Library Mode 的先决条件
要将 Protostuff marshalling 与 Data Grid 一起用作应用程序中嵌入的库,请执行以下操作:
将
infinispan-marshaller-protostuff依赖项添加到pom.xml。<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-marshaller-protostuff</artifactId> <version>${version.infinispan}</version> </dependency><dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-marshaller-protostuff</artifactId> <version>${version.infinispan}</version> </dependency>Copy to Clipboard Copied! Toggle word wrap Toggle overflow 将
org.infinispan.marshaller.protostuff.ProtostuffMarshaller类指定为 marshaller。GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder(); builder.serialization() .marshaller(new org.infinispan.marshaller.protostuff.ProtostuffMarshaller());GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder(); builder.serialization() .marshaller(new org.infinispan.marshaller.protostuff.ProtostuffMarshaller());Copy to Clipboard Copied! Toggle word wrap Toggle overflow
流程
执行以下操作之一为对象 marshalling 注册自定义 Protostuff 模式:
调用
register ()方法。RuntimeSchema.register(ExampleObject.class, new ExampleObjectSchema());
RuntimeSchema.register(ExampleObject.class, new ExampleObjectSchema());Copy to Clipboard Copied! Toggle word wrap Toggle overflow 为
SerializerRegistryService.java接口实施服务供应商,该接口将所有模式注册放置在register ()方法中。然后,您应该指定在部署 JAR 文件中实施类的完整路径:
META-INF/services/org/infinispan/marshaller/protostuff/SchemaRegistryService
META-INF/services/org/infinispan/marshaller/protostuff/SchemaRegistryServiceCopy to Clipboard Copied! Toggle word wrap Toggle overflow
5.3.5. 使用自定义 Marshallers 复制链接链接已复制到粘贴板!
Data Grid 提供了一个 Marshaller 接口,您可以为自定义 marshallers 实施。
流程
-
实施
Marshaller接口。 - 将 Data Grid 配置为使用您的 marshaller。
将您的 Java 类添加到反序列化白名单中。
以编程方式:
GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder(); builder.serialization() .marshaller(new org.infinispan.example.marshall.CustomMarshaller()) .whiteList().addRegexp("org.infinispan.example.*");GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder(); builder.serialization() .marshaller(new org.infinispan.example.marshall.CustomMarshaller()) .whiteList().addRegexp("org.infinispan.example.*");Copy to Clipboard Copied! Toggle word wrap Toggle overflow 声明:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
自定义 marshaller 实现可以通过 initialize () 方法访问配置的白板列表,该方法在启动时调用。
5.3.6. 将 Java 类添加到 Deserialization White 列表中 复制链接链接已复制到粘贴板!
出于安全原因,数据网格不允许对 arbritrary Java 类进行反序列化处理,这适用于 JSON、XML 和 marshalled byte[] 内容。
您必须将 Java 类添加到反序列化白名单中,可以使用系统属性或在 Data Grid 配置中指定它们。
系统属性
// Specify a comma-separated list of fully qualified class names -Dinfinispan.deserialization.whitelist.classes=java.time.Instant,com.myclass.Entity // Specify a regular expression to match classes -Dinfinispan.deserialization.whitelist.regexps=.*
// Specify a comma-separated list of fully qualified class names
-Dinfinispan.deserialization.whitelist.classes=java.time.Instant,com.myclass.Entity
// Specify a regular expression to match classes
-Dinfinispan.deserialization.whitelist.regexps=.*
声明
您添加到反序列化白名单中的 Java 类适用于 Data Grid CacheContainer,并可以被 CacheContainer 控制的所有缓存反序列化。