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,否则数据网格会忽略 高级外部工具 实现。

流程

  1. infinispan-jboss-marshalling 依赖项添加到您的 classpath。
  2. 将 Data Grid 配置为使用 GenericJBossMarshaller
  3. 将您的 Java 类添加到反序列化白名单中。

    • 以编程方式:

      GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder();
      builder.serialization()
             .marshaller(new GenericJBossMarshaller())
             .whiteList()
             .addRegexps("org.infinispan.example.", "org.infinispan.concrete.SomeClass");
      Copy to Clipboard Toggle word wrap
    • 声明:

      <serialization marshaller="org.infinispan.jboss.marshalling.commons.GenericJBossMarshaller">
        <white-list>
            <class>org.infinispan.concrete.SomeClass</class>
            <regex>org.infinispan.example.*</regex>
        <white-list>
      </serialization>
      Copy to Clipboard Toggle word wrap

5.3.2. 使用 Java Serialization

您可以将 Java 序列化与 Data Grid 搭配使用,以汇总您的对象,但只有 Java 对象实现 Java Serializable 接口。

流程

  1. 将 Data Grid 配置为使用 JavaSerializationMarshaller 作为 marshaller。
  2. 将您的 Java 类添加到反序列化白名单中。

    • 以编程方式:

      GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder();
      builder.serialization()
             .marshaller(new JavaSerializationMarshaller())
             .whiteList()
             .addRegexps("org.infinispan.example.", "org.infinispan.concrete.SomeClass");
      Copy to Clipboard Toggle word wrap
    • 声明:

      <serialization marshaller="org.infinispan.commons.marshall.JavaSerializationMarshaller">
          <white-list>
              <class>org.infinispan.concrete.SomeClass</class>
              <regex>org.infinispan.example.*</regex>
          </white-list>
      </serialization>
      Copy to Clipboard Toggle word wrap

5.3.3. 使用 Kryo Marshaller

Data Grid 提供了一个使用 Kryo 库的 marshalling 实现。

Data Grid Servers 的先决条件

要将 Kryo marshalling 与 Data Grid 服务器搭配使用,请添加 JAR,其中包含 Kryo marshalling 实现的运行时类文件,如下所示:

  1. 从 Data Grid Maven 存储库复制 infinispan-marshaller-kryo-bundle.jar
  2. 将 JAR 文件添加到 Data Grid 服务器安装目录中的 server/lib 目录中。

Data Grid Library Mode 的先决条件

要将 Kryo marshalling 与 Data Grid 搭配使用,作为应用程序中的嵌入式库,请执行以下操作:

  1. infinispan-marshaller-kryo 依赖项添加到您的 pom.xml

    <dependency>
      <groupId>org.infinispan</groupId>
      <artifactId>infinispan-marshaller-kryo</artifactId>
      <version>${version.infinispan}</version>
    </dependency>
    Copy to Clipboard Toggle word wrap
  2. org.infinispan.marshaller.kryo.KryoMarshaller 类指定为 marshaller。

    GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder();
    builder.serialization()
           .marshaller(new org.infinispan.marshaller.kryo.KryoMarshaller());
    Copy to Clipboard Toggle word wrap

流程

  1. SerializerRegistryService.java 接口实施服务提供商。
  2. 将所有序列化器注册放在 寄存器(Kryo) 方法中;其中 serializers 使用 Kryo API 向提供的 Kryo 对象注册,例如:

    kryo.register(ExampleObject.class, new ExampleObjectSerializer())
    Copy to Clipboard Toggle word wrap
  3. 指定在部署 JAR 文件中实施类的完整路径:

    META-INF/services/org/infinispan/marshaller/kryo/SerializerRegistryService
    Copy to Clipboard Toggle word wrap

5.3.4. 使用 Protostuff Marshaller

Data Grid 提供了一个 marshalling 实现,它使用 Protostuff 库。

Data Grid Servers 的先决条件

要将 Protostuff marshalling 与 Data Grid 服务器一起使用,请添加 JAR,其中包含 Protostuff marshalling 实现的运行时类文件,如下所示:

  1. 从 Data Grid Maven 存储库复制 infinispan-marshaller-protostuff-bundle.jar
  2. 将 JAR 文件添加到 Data Grid 服务器安装目录中的 server/lib 目录中。

Data Grid Library Mode 的先决条件

要将 Protostuff marshalling 与 Data Grid 一起用作应用程序中嵌入的库,请执行以下操作:

  1. infinispan-marshaller-protostuff 依赖项添加到 pom.xml

    <dependency>
      <groupId>org.infinispan</groupId>
      <artifactId>infinispan-marshaller-protostuff</artifactId>
      <version>${version.infinispan}</version>
    </dependency>
    Copy to Clipboard Toggle word wrap
  2. org.infinispan.marshaller.protostuff.ProtostuffMarshaller 类指定为 marshaller。

    GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder();
    builder.serialization()
           .marshaller(new org.infinispan.marshaller.protostuff.ProtostuffMarshaller());
    Copy to Clipboard Toggle word wrap

流程

执行以下操作之一为对象 marshalling 注册自定义 Protostuff 模式:

  • 调用 register () 方法。

    RuntimeSchema.register(ExampleObject.class, new ExampleObjectSchema());
    Copy to Clipboard Toggle word wrap
  • SerializerRegistryService.java 接口实施服务供应商,该接口将所有模式注册放置在 register () 方法中。

    然后,您应该指定在部署 JAR 文件中实施类的完整路径:

    META-INF/services/org/infinispan/marshaller/protostuff/SchemaRegistryService
    Copy to Clipboard Toggle word wrap

5.3.5. 使用自定义 Marshallers

Data Grid 提供了一个 Marshaller 接口,您可以为自定义 marshallers 实施。

流程

  1. 实施 Marshaller 接口。
  2. 将 Data Grid 配置为使用您的 marshaller。
  3. 将您的 Java 类添加到反序列化白名单中。

    • 以编程方式:

      GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder();
      builder.serialization()
            .marshaller(new org.infinispan.example.marshall.CustomMarshaller())
            .whiteList().addRegexp("org.infinispan.example.*");
      Copy to Clipboard Toggle word wrap
    • 声明:

      <serialization marshaller="org.infinispan.example.marshall.CustomMarshaller">
          <white-list>
              <class>org.infinispan.concrete.SomeClass</class>
              <regex>org.infinispan.example.*</regex>
          </white-list>
      </serialization>
      Copy to Clipboard Toggle word wrap
提示

自定义 marshaller 实现可以通过 initialize () 方法访问配置的白板列表,该方法在启动时调用。

出于安全原因,数据网格不允许对 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=.*
Copy to Clipboard Toggle word wrap

声明

<cache-container>
   <serialization version="1.0" marshaller="org.infinispan.marshall.TestObjectStreamMarshaller">
      <white-list>
         <class>org.infinispan.test.data.Person</class>
         <regex>org.infinispan.test.data.*</regex>
       </white-list>
   </serialization>
</cache-container>
Copy to Clipboard Toggle word wrap

注意

您添加到反序列化白名单中的 Java 类适用于 Data Grid CacheContainer,并可以被 CacheContainer 控制的所有缓存反序列化。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat