在 Java 应用程序中嵌入数据仓库


Red Hat Data Grid 8.3

使用 Data Grid 创建嵌入式缓存

Red Hat Customer Content Services

摘要

向 Java 项目添加 Data Grid,并在您的应用程序中使用嵌入式缓存。

Red Hat Data Grid

Data Grid 是一个高性能分布式内存数据存储。

无架构数据结构
将不同对象存储为键值对的灵活性。
基于网格的数据存储
旨在在集群中分发和复制数据。
弹性扩展
动态调整节点数量,以便在不中断服务的情况下满足需求。
数据互操作性
从不同端点在网格中存储、检索和查询数据。

Data Grid 文档

红帽客户门户网站中提供了 Data Grid 的文档。

Data Grid 下载

访问 红帽客户门户上的 Data Grid 软件下载。

注意

您必须有一个红帽帐户才能访问和下载数据中心软件。

使开源包含更多

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。我们从这四个术语开始:master、slave、黑名单和白名单。由于此项工作十分艰巨,这些更改将在即将推出的几个发行版本中逐步实施。详情请查看 CTO Chris Wright 的信息

第 1 章 配置 Data Grid Maven 存储库

Data Grid Java 发行版可从 Maven 提供。

您可以从客户门户网站下载 Data Grid Maven 存储库,或者从公共 Red Hat Enterprise Maven 存储库拉取 Data Grid 依赖项。

1.1. 下载 Data Grid Maven 存储库

如果您不想使用公共 Red Hat Enterprise Maven 存储库,将 Data Grid Maven 存储库下载并安装到本地文件系统、Apache HTTP 服务器或 Maven 存储库管理器。

流程

  1. 登录到红帽客户门户。
  2. 导航到 Software Downloads for Data Grid
  3. 下载 Red Hat Data Grid 8.3 Maven 存储库。
  4. 将存档 Maven 存储库提取到本地文件系统。
  5. 打开 README.md 文件并按照适当的安装说明进行操作。

1.2. 添加 Red Hat Maven 存储库

在您的 Maven 构建环境中包括 Red Hat GA 存储库,以获取 Data Grid 工件和依赖项。

流程

  • 将 Red Hat GA 存储库添加到您的 Maven 设置文件中,通常为 ~/.m2/settings.xml,或者直接在项目的 pom.xml 文件中添加。

    <repositories>
      <repository>
        <id>redhat-ga-repository</id>
        <name>Red Hat GA Repository</name>
        <url>https://maven.repository.redhat.com/ga/</url>
      </repository>
    </repositories>
    <pluginRepositories>
      <pluginRepository>
        <id>redhat-ga-repository</id>
        <name>Red Hat GA Repository</name>
        <url>https://maven.repository.redhat.com/ga/</url>
      </pluginRepository>
    </pluginRepositories>
    Copy to Clipboard Toggle word wrap

1.3. 配置您的 Data Grid POM

Maven 使用名为 Project Object Model (POM)文件的配置文件来定义项目并管理构建。POM 文件采用 XML 格式,描述生成的项目打包和输出的模块和组件依赖项、构建顺序和目标。

流程

  1. 打开项目 pom.xml 进行编辑。
  2. 使用正确的 Data Grid 版本定义 version.infinispan 属性。
  3. 在 dependencies Management 部分包含 infinispan-bom

    Bill Of Materials (BOM)控制依赖项版本,这样可避免版本冲突,意味着您不需要为项目添加的每个 Data Grid 工件设置版本。

  4. 保存并关闭 pom.xml

以下示例显示了 Data Grid 版本和 BOM:

<properties>
  <version.infinispan>13.0.10.Final-redhat-00001</version.infinispan>
</properties>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.infinispan</groupId>
      <artifactId>infinispan-bom</artifactId>
      <version>${version.infinispan}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
Copy to Clipboard Toggle word wrap

后续步骤

根据需要,将 Data Grid 工件作为依赖项添加到 pom.xml 中。

第 2 章 创建嵌入式缓存

Data Grid 提供了一个 嵌入式CacheManager API,可让您以编程方式控制缓存管理器和嵌入式缓存生命周期。

2.1. 在您的项目中添加数据仓库

在您的项目中添加 Data Grid,以便在应用程序中创建嵌入式缓存。

先决条件

  • 配置您的项目,以从 Maven 存储库获取 Data Grid 工件。

流程

  • infinispan-core 工件作为依赖项添加到 pom.xml 中,如下所示:
<dependencies>
  <dependency>
    <groupId>org.infinispan</groupId>
    <artifactId>infinispan-core</artifactId>
  </dependency>
</dependencies>
Copy to Clipboard Toggle word wrap

2.2. 配置嵌入式缓存

Data Grid 提供了一个 GlobalConfigurationBuilder API,用于控制缓存管理器和配置嵌入式缓存的 ConfigurationBuilder API。

先决条件

  • infinispan-core 工件作为依赖项添加到 pom.xml 中。

流程

  1. 初始化默认缓存管理器,以便您可以添加嵌入式缓存。
  2. 使用 ConfigurationBuilder API 添加至少一个嵌入式缓存。
  3. 调用 getOrCreateCache () 方法,该方法可在集群中的所有节点上创建嵌入式缓存,或者返回已存在的缓存。
// Set up a clustered cache manager.
GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();
// Initialize the default cache manager.
DefaultCacheManager cacheManager = new DefaultCacheManager(global.build());
// Create a distributed cache with synchronous replication.
ConfigurationBuilder builder = new ConfigurationBuilder();
                     builder.clustering().cacheMode(CacheMode.DIST_SYNC);
// Obtain a volatile cache.
Cache<String, String> cache = cacheManager.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE).getOrCreateCache("myCache", builder.build());
Copy to Clipboard Toggle word wrap

第 3 章 启用和配置 Data Grid 统计和 JMX 监控

数据中心可以提供缓存管理器和缓存统计信息以及导出 JMX MBeans。

3.1. 在嵌入式缓存中启用统计信息

配置 Data Grid,以导出缓存管理器和嵌入式缓存的统计信息。

流程

  1. 打开您的 Data Grid 配置进行编辑。
  2. 添加 statistics="true" 属性或 .statistics (true) 方法。
  3. 保存并关闭您的数据仓库配置。
嵌入式缓存统计信息

XML

<infinispan>
  <cache-container statistics="true">
    <distributed-cache statistics="true"/>
    <replicated-cache statistics="true"/>
  </cache-container>
</infinispan>
Copy to Clipboard Toggle word wrap

GlobalConfigurationBuilder

GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder().cacheContainer().statistics(true);
DefaultCacheManager cacheManager = new DefaultCacheManager(global.build());

Configuration builder = new ConfigurationBuilder();
builder.statistics().enable();
Copy to Clipboard Toggle word wrap

3.2. 配置 Data Grid 指标

Data Grid 生成与 MicroProfile Metrics API 兼容的指标。

  • gauges 提供值,如写入操作或 JVM 运行时间的平均纳秒数。
  • 直方图提供有关操作执行时间的详细信息,如读取、写入和删除时间。

默认情况下,当启用统计信息时,Data Grid 会生成 gauges,但您也可以将其配置为生成直方图。

流程

  1. 打开您的 Data Grid 配置进行编辑。
  2. metrics 元素或对象添加到 cache 容器。
  3. 使用 gauges 属性或 字段 启用或禁用 gauges。
  4. 使用 histograms 属性或字段启用或禁用直方图。
  5. 保存并关闭您的客户端配置。
指标配置

XML

<infinispan>
  <cache-container statistics="true">
    <metrics gauges="true"
             histograms="true" />
  </cache-container>
</infinispan>
Copy to Clipboard Toggle word wrap

JSON

{
  "infinispan" : {
    "cache-container" : {
      "statistics" : "true",
      "metrics" : {
        "gauges" : "true",
        "histograms" : "true"
      }
    }
  }
}
Copy to Clipboard Toggle word wrap

YAML

infinispan:
  cacheContainer:
    statistics: "true"
    metrics:
      gauges: "true"
      histograms: "true"
Copy to Clipboard Toggle word wrap

GlobalConfigurationBuilder

GlobalConfiguration globalConfig = new GlobalConfigurationBuilder()
  //Computes and collects statistics for the Cache Manager.
  .statistics().enable()
  //Exports collected statistics as gauge and histogram metrics.
  .metrics().gauges(true).histograms(true)
  .build();
Copy to Clipboard Toggle word wrap

验证

对于嵌入式缓存,您必须将所需的 MicroProfile API 和 provider JAR 添加到类路径,以导出 Data Grid 指标。

3.3. 注册 JMX MBeans

数据中心可以注册可用于收集统计信息并执行管理操作的 JMX MBeans。您还必须启用统计信息,否则 Data Grid 为 JMX MBeans 中的所有统计属性提供 0 值。

流程

  1. 打开您的 Data Grid 配置进行编辑。
  2. jmx 元素或对象添加到缓存容器中,并将 true 指定为 enabled 属性或字段的值。
  3. 添加 domain 属性或字段,并根据需要指定公开 JMX MBeans 的域。
  4. 保存并关闭您的客户端配置。
JMX 配置

XML

<infinispan>
  <cache-container statistics="true">
    <jmx enabled="true"
         domain="example.com"/>
  </cache-container>
</infinispan>
Copy to Clipboard Toggle word wrap

JSON

{
  "infinispan" : {
    "cache-container" : {
      "statistics" : "true",
      "jmx" : {
        "enabled" : "true",
        "domain" : "example.com"
      }
    }
  }
}
Copy to Clipboard Toggle word wrap

YAML

infinispan:
  cacheContainer:
    statistics: "true"
    jmx:
      enabled: "true"
      domain: "example.com"
Copy to Clipboard Toggle word wrap

GlobalConfigurationBuilder

GlobalConfiguration global = GlobalConfigurationBuilder.defaultClusteredBuilder()
   .jmx().enable()
   .domain("org.mydomain");
Copy to Clipboard Toggle word wrap

3.3.1. 启用 JMX 远程端口

提供唯一的远程 JMX 端口,以通过 JMXServiceURL 格式的连接公开 Data Grid MBeans。

您可以使用以下方法之一启用远程 JMX 端口:

  • 启用需要向其中一个 Data Grid 服务器安全域进行身份验证的远程 JMX 端口。
  • 使用标准 Java 管理配置选项手动启用远程 JMX 端口。

先决条件

  • 对于带有身份验证的远程 JMX,请使用默认安全域定义用户角色。用户必须具有具有读/写访问权限的 controlRole,或者具有只读访问权限的 monitorRole 才能访问任何 JMX 资源。

流程

使用以下方法之一启动启用了远程 JMX 端口的 Data Grid 服务器:

  • 通过端口 9999 启用远程 JMX。

    bin/server.sh --jmx 9999
    Copy to Clipboard Toggle word wrap
    警告

    在生产环境中不适用于禁用 SSL 的远程 JMX。

  • 在启动时将以下系统属性传递给 Data Grid 服务器。

    bin/server.sh -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false
    Copy to Clipboard Toggle word wrap
    警告

    启用没有身份验证或 SSL 的远程 JMX 不安全,在任何环境中都不推荐使用。禁用身份验证和 SSL 允许未授权用户连接到服务器并访问托管的数据。

3.3.2. Data Grid MBeans

Data Grid 会公开代表可以管理资源的 JMX MBeans。

org.infinispan:type=Cache
可用于缓存实例的属性和操作。
org.infinispan:type=CacheManager
用于缓存管理器的属性和操作,包括数据网格缓存和集群健康统计信息。

有关可用 JMX MBeans 的完整列表以及描述和可用操作和属性,请参阅 Data Grid JMX 组件 文档。

3.3.3. 在自定义 MBean 服务器中注册 MBeans

数据中心包含一个 MBeanServerLookup 接口,您可以使用它在自定义 MBeanServer 实例中注册 MBeans。

先决条件

  • 创建 MBeanServerLookup 实施,以便 getMBeanServer () 方法返回自定义 MBeanServer 实例。
  • 配置 Data Grid 以注册 JMX MBeans。

流程

  1. 打开您的 Data Grid 配置进行编辑。
  2. mbean-server-lookup 属性或字段添加到缓存管理器的 JMX 配置中。
  3. 指定 MBeanServerLookup 实现的完全限定名称(FQN)。
  4. 保存并关闭您的客户端配置。
JMX MBean 服务器查找配置

XML

<infinispan>
  <cache-container statistics="true">
    <jmx enabled="true"
         domain="example.com"
         mbean-server-lookup="com.example.MyMBeanServerLookup"/>
  </cache-container>
</infinispan>
Copy to Clipboard Toggle word wrap

JSON

{
  "infinispan" : {
    "cache-container" : {
      "statistics" : "true",
      "jmx" : {
        "enabled" : "true",
        "domain" : "example.com",
        "mbean-server-lookup" : "com.example.MyMBeanServerLookup"
      }
    }
  }
}
Copy to Clipboard Toggle word wrap

YAML

infinispan:
  cacheContainer:
    statistics: "true"
    jmx:
      enabled: "true"
      domain: "example.com"
      mbeanServerLookup: "com.example.MyMBeanServerLookup"
Copy to Clipboard Toggle word wrap

GlobalConfigurationBuilder

GlobalConfiguration global = GlobalConfigurationBuilder.defaultClusteredBuilder()
   .jmx().enable()
   .domain("org.mydomain")
   .mBeanServerLookup(new com.acme.MyMBeanServerLookup());
Copy to Clipboard Toggle word wrap

第 4 章 设置 Data Grid 集群传输

Data Grid 需要传输层,以便节点可以自动加入和离开集群。传输层还可让 Data Grid 节点在网络中复制或分发数据,并执行重新平衡和状态传输等操作。

4.1. 默认 JGroups 堆栈

Data Grid 在 infinispan-core-13.0.10 .Final-redhat-00001.jar 文件中的 default-configs 目录中提供默认的 JGroups 堆栈文件 default-jgroups114.xml

Expand
文件名堆栈名称描述

default-jgroups-udp.xml

udp

使用 UDP 进行传输和 UDP 多播进行发现。适用于较大的集群(超过 100 个节点),或者您使用复制缓存或无效模式。最小化打开的插槽数量。

default-jgroups-tcp.xml

tcp

使用 TCP 进行传输和 MPING 协议进行发现,该协议使用 UDP 多播。只有在您使用分布式缓存 ,只适用于较小的集群(在 100 个节点下),因为 TCP 比 UDP 作为点对点协议更高效。

default-jgroups-kubernetes.xml

kubernetes

使用 TCP 进行传输,使用 DNS_PING 进行发现。适用于并不总是可用的 UDP 多播的 Kubernetes 和 Red Hat OpenShift 节点。

default-jgroups-ec2.xml

ec2

使用 TCP 进行传输和 NATIVE_S3_PING 进行发现。适用于 UDP 多播不可用的 Amazon EC2 节点。需要额外的依赖项。

default-jgroups-google.xml

google

使用 TCP 进行传输,使用 GOOGLE_PING2 进行发现。适用于 UDP 多播不可用的 Google Cloud Platform 节点。需要额外的依赖项。

default-jgroups-azure.xml

azure

使用 TCP 进行传输,AZURE_PING 进行发现。适用于 UDP 多播不可用的 Microsoft Azure 节点。需要额外的依赖项。

4.2. 集群发现协议

Data Grid 支持不同的协议,允许节点在网络和组成集群中自动查找。

Data Grid 可使用两种发现机制:

  • 在大多数网络上工作且不依赖于外部服务的通用发现协议。
  • 发现协议,它依赖于外部服务来为 Data Grid 集群存储和检索拓扑信息。
    例如,DNS_PING 协议通过 DNS 服务器记录执行发现。
注意

在托管平台上运行 Data Grid 需要使用发现机制,它们适应各个云供应商实施的网络限制。

4.2.1. PING

PING 或 UDPPING 是一种通用的 JGroups 发现机制,它使用 UDP 协议的动态多播。

加入时,节点会向 IP 多播地址发送 PING 请求,以发现 Data Grid 集群中已存在的其他节点。每个节点都使用包含协调器节点的地址的数据包响应 PING 请求。c=coordinator 的地址和 A=own 地址。如果没有节点响应 PING 请求,则加入的节点将变为新集群中的协调器节点。

PING 配置示例

<PING num_discovery_runs="3"/>
Copy to Clipboard Toggle word wrap

4.2.2. TCPPING

TCPPING 是一种通用 JGroups 发现机制,可为集群成员使用静态地址列表。

使用 TCPPING 时,您可以手动指定 Data Grid 集群中每个节点的 IP 地址或主机名,作为 JGroups 堆栈的一部分,而不是让节点互相发现。

TCPPING 配置示例

<TCP bind_port="7800" />
<TCPPING timeout="3000"
         initial_hosts="${jgroups.tcpping.initial_hosts:hostname1[port1],hostname2[port2]}"
          port_range="0"
          num_initial_members="3"/>
Copy to Clipboard Toggle word wrap

4.2.3. MPING

MPING 使用 IP 多播来发现 Data Grid 集群的初始成员资格。

您可以使用 MPING 将 TCPPING 发现替换为 TCP 堆栈,并使用多casing 进行发现,而不是初始主机的静态列表。但是,您还可以将 MPING 与 UDP 堆栈搭配使用。

MPING 配置示例

<MPING mcast_addr="${jgroups.mcast_addr:228.6.7.8}"
       mcast_port="${jgroups.mcast_port:46655}"
       num_discovery_runs="3"
       ip_ttl="${jgroups.udp.ip_ttl:2}"/>
Copy to Clipboard Toggle word wrap

4.2.4. TCPGOSSIP

gossip 路由器在网络上提供一个集中的位置,您的 Data Grid 集群可以从中检索其他节点的地址。

您可以将 Gossip 路由器的地址(IP:PORT)注入 Data Grid 节点,如下所示:

  1. 将地址作为系统属性传递给 JVM,例如 -DGossipRouterAddress="10.10.2.4[12001]"。
  2. 在 JGroups 配置文件中引用该系统属性。

gossip 路由器配置示例

<TCP bind_port="7800" />
<TCPGOSSIP timeout="3000"
           initial_hosts="${GossipRouterAddress}"
           num_initial_members="3" />
Copy to Clipboard Toggle word wrap

4.2.5. JDBC_PING

JDBC_PING 使用共享数据库来存储有关 Data Grid 集群的信息。此协议支持任何可以使用 JDBC 连接的数据库。

将 IP 地址写入共享数据库的节点,以便加入节点可以在网络中找到 Data Grid 集群。当节点离开 Data Grid 集群时,它们会从共享数据库中删除其 IP 地址。

JDBC_PING 配置示例

<JDBC_PING connection_url="jdbc:mysql://localhost:3306/database_name"
           connection_username="user"
           connection_password="password"
           connection_driver="com.mysql.jdbc.Driver"/>
Copy to Clipboard Toggle word wrap

重要

在 classpath 中添加适当的 JDBC 驱动程序,以便 Data Grid 可以使用 JDBC_PING。

4.2.6. DNS_PING

JGroups DNS_PING 查询 DNS 服务器,以在 Kubernetes 环境中发现 Data Grid 集群成员,如 OKD 和 Red Hat OpenShift。

DNS_PING 配置示例

<dns.DNS_PING dns_query="myservice.myproject.svc.cluster.local" />
Copy to Clipboard Toggle word wrap

4.2.7. 云发现协议

Data Grid 包括默认的 JGroups 堆栈,它使用特定于云供应商的发现协议实现。

Expand
发现协议默认堆栈文件工件版本

NATIVE_S3_PING

default-jgroups-ec2.xml

org.jgroups.aws.s3:native-s3-ping

1.0.0.Final

GOOGLE_PING2

default-jgroups-google.xml

org.jgroups.google:jgroups-google

1.0.0.Final

AZURE_PING

default-jgroups-azure.xml

org.jgroups.azure:jgroups-azure

1.3.0.Final

为云发现协议提供依赖项

要使用 NATIVE_S3_PINGGOOGLE_PING2AZURE_PING 云发现协议,您需要向 Data Grid 提供依赖的库。

流程

  • 将工件依赖项添加到项目 pom.xml 中。

然后,您可以将云发现协议配置为 JGroups 堆栈文件或系统属性的一部分。

4.3. 使用默认的 JGroups 堆栈

Data Grid 使用 JGroups 协议堆栈,以便节点可以在专用集群频道中发送其他消息。

Data Grid 为 UDPTCP 协议提供预配置的 JGroups 堆栈。您可以使用这些默认堆栈来构建自定义集群传输配置,该配置根据您的网络要求进行了优化。

流程

执行以下操作之一以使用其中一个默认 JGroups 堆栈:

  • 使用 infinispan.xml 文件中的 stack 属性。

    <infinispan>
      <cache-container default-cache="replicatedCache">
        <!-- Use the default UDP stack for cluster transport. -->
        <transport cluster="${infinispan.cluster.name}"
                   stack="udp"
                   node-name="${infinispan.node.name:}"/>
      </cache-container>
    </infinispan>
    Copy to Clipboard Toggle word wrap
  • 使用 addProperty () 方法设置 JGroups 堆栈文件:

    GlobalConfiguration globalConfig = new GlobalConfigurationBuilder().transport()
            .defaultTransport()
            .clusterName("qa-cluster")
            //Uses the default-jgroups-udp.xml stack for cluster transport.
            .addProperty("configurationFile", "default-jgroups-udp.xml")
            .build();
    Copy to Clipboard Toggle word wrap

验证

Data Grid 会记录以下信息来指示它使用的堆栈:

[org.infinispan.CLUSTER] ISPN000078: Starting JGroups channel cluster with stack udp
Copy to Clipboard Toggle word wrap

4.4. 自定义 JGroups 堆栈

调整并调优属性,以创建适合您的网络要求的集群传输配置。

Data Grid 提供允许您扩展默认 JGroups 堆栈的属性,以便更轻松地配置。您可以继承默认堆栈的属性,同时组合、删除和替换其他属性。

流程

  1. infinispan.xml 文件中创建一个新的 JGroups 堆栈声明。
  2. 添加 extends 属性,并指定 JGroups 堆栈来继承属性。
  3. 使用 stack.combine 属性修改继承堆栈中配置的协议的属性。
  4. 使用 stack.position 属性定义自定义堆栈的位置。
  5. 将 stack 名称指定为 传输 配置中 stack 属性的值。

    例如,您可以评估使用默认 TCP 堆栈的 Gossip 路由器和对称加密,如下所示:

    <infinispan>
      <jgroups>
        <!-- Creates a custom JGroups stack named "my-stack". -->
        <!-- Inherits properties from the default TCP stack. -->
        <stack name="my-stack" extends="tcp">
          <!-- Uses TCPGOSSIP as the discovery mechanism instead of MPING -->
          <TCPGOSSIP initial_hosts="${jgroups.tunnel.gossip_router_hosts:localhost[12001]}"
                 stack.combine="REPLACE"
                 stack.position="MPING" />
          <!-- Removes the FD_SOCK protocol from the stack. -->
          <FD_SOCK stack.combine="REMOVE"/>
          <!-- Modifies the timeout value for the VERIFY_SUSPECT protocol. -->
          <VERIFY_SUSPECT timeout="2000"/>
          <!-- Adds SYM_ENCRYPT to the stack after VERIFY_SUSPECT. -->
          <SYM_ENCRYPT sym_algorithm="AES"
                       keystore_name="mykeystore.p12"
                       keystore_type="PKCS12"
                       store_password="changeit"
                       key_password="changeit"
                       alias="myKey"
                       stack.combine="INSERT_AFTER"
                       stack.position="VERIFY_SUSPECT" />
        </stack>
        <cache-container name="default" statistics="true">
          <!-- Uses "my-stack" for cluster transport. -->
          <transport cluster="${infinispan.cluster.name}"
                     stack="my-stack"
                     node-name="${infinispan.node.name:}"/>
       </cache-container>
      </jgroups>
    </infinispan>
    Copy to Clipboard Toggle word wrap
  6. 检查 Data Grid 日志以确保它使用堆栈。

    [org.infinispan.CLUSTER] ISPN000078: Starting JGroups channel cluster with stack my-stack
    Copy to Clipboard Toggle word wrap

参考

4.4.1. 继承属性

当您扩展 JGroups 堆栈时,继承属性可让您调整您要扩展的堆栈中的协议和属性。

  • stack.position 指定要修改的协议。
  • stack.combine 使用以下值来扩展 JGroups 堆栈:

    Expand
    描述

    组合

    覆盖协议属性。

    REPLACE

    替换 protocol。

    INSERT_AFTER

    在另一个协议后,将协议添加到堆栈中。不会影响您指定的协议作为插入点。

    JGroups 堆栈中的协议根据其堆栈中的位置相互影响。例如,您应该在 SYM_ENCRYPTASYM_ENCRYPT 协议后放置 NAKACK2 等协议,以便 NAKACK2 受保护。

    INSERT_BEFORE

    在另一个协议之前,将协议插入到堆栈中。影响您指定的协议作为插入点。

    删除

    从堆栈中删除协议。

4.5. 使用 JGroups 系统属性

在启动时将系统属性传递给 Data Grid,以调优集群传输。

流程

  • 根据需要使用 -D<property-name>=<property-value > 参数来设置 JGroups 系统属性。

例如,设置自定义绑定端口和 IP 地址,如下所示:

java -cp ... -Djgroups.bind.port=1234 -Djgroups.bind.address=192.0.2.0
Copy to Clipboard Toggle word wrap
注意

当您在集群 Red Hat JBoss EAP 应用程序中嵌入 Data Grid 集群时,JGroups 系统属性可以相互清除或覆盖。

例如,您无法为您的 Data Grid 集群或 Red Hat JBoss EAP 应用程序设置唯一的绑定地址。在这种情况下,Data Grid 和 Red Hat JBoss EAP 应用程序都使用 JGroups 默认属性,并尝试使用相同的绑定地址组成集群。

4.5.1. 集群传输属性

使用以下属性来自定义 JGroups 集群传输:

Expand
系统属性描述默认值必填/选填

jgroups.bind.address

集群传输的绑定地址。

SITE_LOCAL

选填

jgroups.bind.port

套接字的绑定端口。

7800

选填

jgroups.mcast_addr

用于多播的 IP 地址,包括发现和集群间通信。IP 地址必须是适合 IP 多播的有效"类 D"地址。

228.6.7.8

选填

jgroups.mcast_port

多播套接字的端口。

46655

选填

jgroups.ip_ttl

用于 IP 多播数据包的时间到实时(TTL)。该值定义数据包在丢弃前可以进行的网络跃点数量。

2

选填

jgroups.thread_pool.min_threads

线程池的最小线程数量。

0

选填

jgroups.thread_pool.max_threads

线程池的最大线程数量。

200

选填

jgroups.join_timeout

等待加入请求成功的最大毫秒数。

2000

选填

jgroups.thread_dumps_threshold

在记录线程转储前,线程池需要已满的次数。

10000

选填

4.5.2. 云发现协议的系统属性

使用以下属性为托管平台配置 JGroups 发现协议。

4.5.2.1. Amazon EC2

用于配置 NATIVE_S3_PING 的系统属性。

Expand
系统属性描述默认值必填/选填

jgroups.s3.region_name

Amazon S3 区域的名称。

没有默认值。

选填

jgroups.s3.bucket_name

Amazon S3 存储桶的名称。名称必须存在,且必须是唯一的。

没有默认值。

选填

4.5.2.2. Google Cloud Platform

用于配置 GOOGLE_PING2 的系统属性。

Expand
系统属性描述默认值必填/选填

jgroups.google.bucket_name

Google Compute Engine 存储桶的名称。名称必须存在,且必须是唯一的。

没有默认值。

必填

4.5.2.3. Azure

AZURE_PING 的系统属性。

Expand
系统属性描述默认值必填/选填

jboss.jgroups.azure_ping.storage_account_name

Azure 存储帐户的名称。名称必须存在,且必须是唯一的。

没有默认值。

必填

jboss.jgroups.azure_ping.storage_access_key

Azure 存储访问密钥的名称。

没有默认值。

必填

jboss.jgroups.azure_ping.container

存储 ping 信息的容器的有效 DNS 名称。

没有默认值。

必填

4.5.2.4. OpenShift

DNS_PING 的系统属性。

Expand
系统属性描述默认值必填/选填

jgroups.dns.query

设置返回集群成员的 DNS 记录。

没有默认值。

必填

4.6. 使用内联 JGroups 堆栈

您可以将完整的 JGroups 堆栈定义插入到 infinispan.xml 文件中。

流程

  • infinispan.xml 文件中嵌入自定义 JGroups 堆栈声明。

    <infinispan>
      <!-- Contains one or more JGroups stack definitions. -->
      <jgroups>
        <!-- Defines a custom JGroups stack named "prod". -->
        <stack name="prod">
          <TCP bind_port="7800" port_range="30" recv_buf_size="20000000" send_buf_size="640000"/>
          <MPING break_on_coord_rsp="true"
                 mcast_addr="${jgroups.mping.mcast_addr:228.2.4.6}"
                 mcast_port="${jgroups.mping.mcast_port:43366}"
                 num_discovery_runs="3"
                 ip_ttl="${jgroups.udp.ip_ttl:2}"/>
          <MERGE3 />
          <FD_SOCK />
          <FD_ALL timeout="3000" interval="1000" timeout_check_interval="1000" />
          <VERIFY_SUSPECT timeout="1000" />
          <pbcast.NAKACK2 use_mcast_xmit="false" xmit_interval="200" xmit_table_num_rows="50"
                          xmit_table_msgs_per_row="1024" xmit_table_max_compaction_time="30000" />
          <UNICAST3 conn_close_timeout="5000" xmit_interval="200" xmit_table_num_rows="50"
                    xmit_table_msgs_per_row="1024" xmit_table_max_compaction_time="30000" />
          <pbcast.STABLE desired_avg_gossip="2000" max_bytes="1M" />
          <pbcast.GMS print_local_addr="false" join_timeout="${jgroups.join_timeout:2000}" />
          <UFC max_credits="4m" min_threshold="0.40" />
          <MFC max_credits="4m" min_threshold="0.40" />
          <FRAG3 />
        </stack>
      </jgroups>
      <cache-container default-cache="replicatedCache">
        <!-- Uses "prod" for cluster transport. -->
        <transport cluster="${infinispan.cluster.name}"
               stack="prod"
               node-name="${infinispan.node.name:}"/>
      </cache-container>
    </infinispan>
    Copy to Clipboard Toggle word wrap

4.7. 使用外部 JGroups 堆栈

infinispan.xml 文件中定义自定义 JGroups 堆栈的外部文件。

流程

  1. 将自定义 JGroups 堆栈文件放在应用程序 classpath 上。

    或者,您可以在声明外部堆栈文件时指定绝对路径。

  2. 使用 stack-file 元素引用外部 stack 文件。

    <infinispan>
      <jgroups>
         <!-- Creates a "prod-tcp" stack that references an external file. -->
         <stack-file name="prod-tcp" path="prod-jgroups-tcp.xml"/>
      </jgroups>
      <cache-container default-cache="replicatedCache">
        <!-- Use the "prod-tcp" stack for cluster transport. -->
        <transport stack="prod-tcp" />
        <replicated-cache name="replicatedCache"/>
      </cache-container>
      <!-- Cache configuration goes here. -->
    </infinispan>
    Copy to Clipboard Toggle word wrap

您还可以使用 TransportConfigurationBuilder 类中的 addProperty () 方法指定自定义 JGroups 堆栈文件,如下所示:

GlobalConfiguration globalConfig = new GlobalConfigurationBuilder().transport()
        .defaultTransport()
        .clusterName("prod-cluster")
        //Uses a custom JGroups stack for cluster transport.
        .addProperty("configurationFile", "my-jgroups-udp.xml")
        .build();
Copy to Clipboard Toggle word wrap

在本例中,my-jgroups-udp.xml 使用自定义属性引用 UDP 堆栈,如下所示:

自定义 UDP 堆栈示例

<config xmlns="urn:org:jgroups"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="urn:org:jgroups http://www.jgroups.org/schema/jgroups-4.2.xsd">
    <UDP bind_addr="${jgroups.bind_addr:127.0.0.1}"
         mcast_addr="${jgroups.udp.mcast_addr:192.0.2.0}"
         mcast_port="${jgroups.udp.mcast_port:46655}"
         tos="8"
         ucast_recv_buf_size="20000000"
         ucast_send_buf_size="640000"
         mcast_recv_buf_size="25000000"
         mcast_send_buf_size="640000"
         max_bundle_size="64000"
         ip_ttl="${jgroups.udp.ip_ttl:2}"
         enable_diagnostics="false"
         thread_naming_pattern="pl"
         thread_pool.enabled="true"
         thread_pool.min_threads="2"
         thread_pool.max_threads="30"
         thread_pool.keep_alive_time="5000" />
    <!-- Other JGroups stack configuration goes here. -->
</config>
Copy to Clipboard Toggle word wrap

4.8. 使用自定义 JChannels

构建自定义 JGroups JChannels,如下例所示:

GlobalConfigurationBuilder global = new GlobalConfigurationBuilder();
JChannel jchannel = new JChannel();
// Configure the jchannel as needed.
JGroupsTransport transport = new JGroupsTransport(jchannel);
global.transport().transport(transport);
new DefaultCacheManager(global.build());
Copy to Clipboard Toggle word wrap
注意

Data Grid 无法使用已经连接的自定义 JChannel。

4.9. 加密集群传输

保护集群传输,以便节点与加密消息通信。您还可以配置 Data Grid 集群来执行证书验证,以便只有具有有效身份的节点才能加入。

4.9.1. JGroups 加密协议

要保护集群流量,您可以配置 Data Grid 节点,以使用 secret 密钥加密 JGroups 消息有效负载。

Data Grid 节点可以从以下之一获取 secret 密钥:

  • 协调器节点(作为中间加密)。
  • 共享密钥存储(中间加密)。

从协调器节点检索 secret 密钥

您可以通过在 Data Grid 配置中的 JGroups 堆栈中添加 ASYM_ENCRYPT 协议来配置非对称加密。这允许 Data Grid 集群生成和分发 secret 密钥。

重要

在使用非对称加密时,您还应提供密钥存储,以便节点能够执行证书身份验证并安全地交换机密密钥。这会保护集群不受中间人(MitM)攻击的影响。

非对称加密保护集群流量,如下所示:

  1. Data Grid 集群中的第一个节点(协调器节点)会生成一个 secret 密钥。
  2. 加入的节点通过 coordinator 执行证书验证,以互斥验证身份。
  3. 加入节点从协调器节点请求 secret 密钥。该请求包括加入节点的公钥。
  4. 协调器节点使用公钥加密 secret 密钥,并将它返回到加入节点。
  5. 加入的节点解密并安装 secret 密钥。
  6. 节点加入集群,使用 secret 密钥加密和解密消息。

从共享密钥存储检索 secret 密钥

您可以通过在 Data Grid 配置中的 JGroups 堆栈中添加 SYM_ENCRYPT 协议来配置对称加密。这允许 Data Grid 集群从您提供的密钥存储中获取 secret 密钥。

  1. 节点在启动时从 Data Grid classpath 的密钥存储安装 secret 密钥。
  2. 节点加入集群,使用 secret 密钥加密和解密信息。

非对称和对称加密的比较

ASYM_ENCRYPT 和证书身份验证提供了与 SYM_ENCRYPT 进行比较的额外加密层。您提供密钥存储来加密对 secret 密钥协调器节点的请求。Data Grid 会自动生成该 secret 密钥并处理集群流量,同时允许您指定何时生成 secret 密钥。例如,您可以将集群配置为在节点离开时生成新的 secret 密钥。这样可确保节点无法绕过证书验证,并使用旧密钥加入。

另一方面,SYM_ENCRYPTASYM_ENCRYPT 快,因为节点不需要与集群协调器交换密钥。SYM_ENCRYPT 的潜在缺陷是,当集群成员资格更改时没有配置自动生成新 secret 密钥。用户负责生成和分发节点用来加密集群流量的 secret 密钥。

4.9.2. 使用非对称加密保护集群传输

配置 Data Grid 集群,以生成和分发加密 JGroups 消息的 secret 密钥。

流程

  1. 使用证书链创建密钥存储,使数据仓库能够验证节点身份。
  2. 将密钥存储放在集群中的每个节点的 classpath 上。

    对于 Data Grid Server,您要将密钥存储放在 $114G_HOME 目录中。

  3. SSL_KEY_EXCHANGEASYM_ENCRYPT 协议添加到 Data Grid 配置中的 JGroups 堆栈,如下例所示:

    <infinispan>
      <jgroups>
        <!-- Creates a secure JGroups stack named "encrypt-tcp" that extends the default TCP stack. -->
        <stack name="encrypt-tcp" extends="tcp">
          <!-- Adds a keystore that nodes use to perform certificate authentication. -->
          <!-- Uses the stack.combine and stack.position attributes to insert SSL_KEY_EXCHANGE into the default TCP stack after VERIFY_SUSPECT. -->
          <SSL_KEY_EXCHANGE keystore_name="mykeystore.jks"
                            keystore_password="changeit"
                            stack.combine="INSERT_AFTER"
                            stack.position="VERIFY_SUSPECT"/>
          <!-- Configures ASYM_ENCRYPT -->
          <!-- Uses the stack.combine and stack.position attributes to insert ASYM_ENCRYPT into the default TCP stack before pbcast.NAKACK2. -->
          <!-- The use_external_key_exchange = "true" attribute configures nodes to use the `SSL_KEY_EXCHANGE` protocol for certificate authentication. -->
          <ASYM_ENCRYPT asym_keylength="2048"
                        asym_algorithm="RSA"
                        change_key_on_coord_leave = "false"
                        change_key_on_leave = "false"
                        use_external_key_exchange = "true"
                        stack.combine="INSERT_BEFORE"
                        stack.position="pbcast.NAKACK2"/>
        </stack>
      </jgroups>
      <cache-container name="default" statistics="true">
        <!-- Configures the cluster to use the JGroups stack. -->
        <transport cluster="${infinispan.cluster.name}"
                   stack="encrypt-tcp"
                   node-name="${infinispan.node.name:}"/>
      </cache-container>
    </infinispan>
    Copy to Clipboard Toggle word wrap

验证

当您启动 Data Grid 集群时,以下日志消息表示集群使用 secure JGroups 堆栈:

[org.infinispan.CLUSTER] ISPN000078: Starting JGroups channel cluster with stack <encrypted_stack_name>
Copy to Clipboard Toggle word wrap

只有在数据中心节点使用 ASYM_ENCRYPT 且可以从协调器节点获取 secret 密钥时,才会加入集群。否则,以下信息被写入 Data Grid 日志:

[org.jgroups.protocols.ASYM_ENCRYPT] <hostname>: received message without encrypt header from <hostname>; dropping it
Copy to Clipboard Toggle word wrap

4.9.3. 使用对称加密保护集群传输

配置 Data Grid 集群,以使用您提供的密钥存储中的 secret 密钥加密 JGroups 消息。

流程

  1. 创建包含 secret 密钥的密钥存储。
  2. 将密钥存储放在集群中的每个节点的 classpath 上。

    对于 Data Grid Server,您要将密钥存储放在 $114G_HOME 目录中。

  3. SYM_ENCRYPT 协议添加到 Data Grid 配置中的 JGroups 堆栈。
<infinispan>
  <jgroups>
    <!-- Creates a secure JGroups stack named "encrypt-tcp" that extends the default TCP stack. -->
    <stack name="encrypt-tcp" extends="tcp">
      <!-- Adds a keystore from which nodes obtain secret keys. -->
      <!-- Uses the stack.combine and stack.position attributes to insert SYM_ENCRYPT into the default TCP stack after VERIFY_SUSPECT. -->
      <SYM_ENCRYPT keystore_name="myKeystore.p12"
                   keystore_type="PKCS12"
                   store_password="changeit"
                   key_password="changeit"
                   alias="myKey"
                   stack.combine="INSERT_AFTER"
                   stack.position="VERIFY_SUSPECT"/>
    </stack>
  </jgroups>
  <cache-container name="default" statistics="true">
    <!-- Configures the cluster to use the JGroups stack. -->
    <transport cluster="${infinispan.cluster.name}"
               stack="encrypt-tcp"
               node-name="${infinispan.node.name:}"/>
  </cache-container>
</infinispan>
Copy to Clipboard Toggle word wrap

验证

当您启动 Data Grid 集群时,以下日志消息表示集群使用 secure JGroups 堆栈:

[org.infinispan.CLUSTER] ISPN000078: Starting JGroups channel cluster with stack <encrypted_stack_name>
Copy to Clipboard Toggle word wrap

只有在数据中心节点使用 SYM_ENCRYPT 且可以从共享密钥存储获取 secret 密钥时,才可以加入集群。否则,以下信息被写入 Data Grid 日志:

[org.jgroups.protocols.SYM_ENCRYPT] <hostname>: received message without encrypt header from <hostname>; dropping it
Copy to Clipboard Toggle word wrap

4.10. 集群流量的 TCP 和 UDP 端口

Data Grid 使用以下端口进行集群传输信息:

Expand
默认端口协议描述

7800

TCP/UDP

JGroups 集群绑定端口

46655

UDP

JGroups 多播

跨站点复制

Data Grid 对 JGroups RELAY2 协议使用以下端口:

7900
对于在 OpenShift 上运行的 Data Grid 集群。
7800
如果将 UDP 用于节点间的流量,使用 TCP 进行集群间的流量。
7801
如果将 TCP 用于节点间的流量,使用 TCP 进行集群间的流量。

法律通告

Copyright © 2023 Red Hat, Inc.
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2026 Red Hat
返回顶部