10.5. 热 Rod 客户端


hot Rod 是一个二进制 TCP 协议,Data Grid 提供带有远程客户端的高性能数据传输功能。

客户端智能

客户端智能指的是 Hot Rod 协议提供的机制,以便客户端可以定位并发送请求到 Data Grid 节点。

在 OpenShift 上运行的热 Rod 客户端可以访问 Data Grid 节点的内部 IP 地址,以便您可以使用任何客户端智能。建议默认智能 HASH_DISTRIBUTION_AWARE,因为它允许客户端将请求路由到主所有者,从而提高性能。

在 OpenShift 外部运行的热 Rod 客户端必须使用 BASIC 智能。

10.5.1. 热 Rod 配置 API

您可以使用 ConfigurationBuilder 接口以编程方式配置 Hot Rod 客户端连接。

注意

$SERVICE_HOSTNAME:$PORT 表示允许访问您的数据网格集群的主机名和端口。您应该使用环境的实际主机名和端口替换这些变量。

在 OpenShift 中

在 OpenShift 上运行的热 Rod 客户端可使用以下配置:

import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.client.hotrod.configuration.SaslQop;
import org.infinispan.client.hotrod.impl.ConfigurationProperties;
...

ConfigurationBuilder builder = new ConfigurationBuilder();
      builder.addServer()
               .host("$SERVICE_HOSTNAME")
               .port(ConfigurationProperties.DEFAULT_HOTROD_PORT)
             .security().authentication()
               .username("username")
               .password("password")
               .realm("default")
               .saslQop(SaslQop.AUTH)
               .saslMechanism("SCRAM-SHA-512")
             .ssl()
               .sniHostName("$SERVICE_HOSTNAME")
               .trustStorePath("/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt");
Copy to Clipboard Toggle word wrap
外部 OpenShift

在 OpenShift 外部运行的热 Rod 客户端可使用以下配置:

import org.infinispan.client.hotrod.configuration.ClientIntelligence;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.client.hotrod.configuration.SaslQop;
...

ConfigurationBuilder builder = new ConfigurationBuilder();
      builder.addServer()
               .host("$SERVICE_HOSTNAME")
               .port("$PORT")
             .security().authentication()
               .username("username")
               .password("password")
               .realm("default")
               .saslQop(SaslQop.AUTH)
               .saslMechanism("SCRAM-SHA-512")
             .ssl()
               .sniHostName("$SERVICE_HOSTNAME")
               .trustStorePath("/path/to/tls.crt");
      builder.clientIntelligence(ClientIntelligence.BASIC);
Copy to Clipboard Toggle word wrap

10.5.2. 热 Rod 客户端属性

您可以使用应用程序 classpath 上的 hotrod-client.properties 文件配置 Hot Rod 客户端连接。

注意

$SERVICE_HOSTNAME:$PORT 表示允许访问您的数据网格集群的主机名和端口。您应该使用环境的实际主机名和端口替换这些变量。

在 OpenShift 中

在 OpenShift 上运行的热 Rod 客户端可使用以下属性:

# Connection
infinispan.client.hotrod.server_list=$SERVICE_HOSTNAME:$PORT

# Authentication
infinispan.client.hotrod.use_auth=true
infinispan.client.hotrod.auth_username=developer
infinispan.client.hotrod.auth_password=$PASSWORD
infinispan.client.hotrod.auth_server_name=$CLUSTER_NAME
infinispan.client.hotrod.sasl_properties.javax.security.sasl.qop=auth
infinispan.client.hotrod.sasl_mechanism=SCRAM-SHA-512

# Encryption
infinispan.client.hotrod.sni_host_name=$SERVICE_HOSTNAME
# Path to the TLS certificate.
# Clients automatically generate trust stores from certificates.
infinispan.client.hotrod.trust_store_path=/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt
Copy to Clipboard Toggle word wrap
外部 OpenShift

在 OpenShift 外部运行的热 Rod 客户端可使用以下属性:

# Connection
infinispan.client.hotrod.server_list=$SERVICE_HOSTNAME:$PORT

# Client intelligence
infinispan.client.hotrod.client_intelligence=BASIC

# Authentication
infinispan.client.hotrod.use_auth=true
infinispan.client.hotrod.auth_username=developer
infinispan.client.hotrod.auth_password=$PASSWORD
infinispan.client.hotrod.auth_server_name=$CLUSTER_NAME
infinispan.client.hotrod.sasl_properties.javax.security.sasl.qop=auth
infinispan.client.hotrod.sasl_mechanism=SCRAM-SHA-512

# Encryption
infinispan.client.hotrod.sni_host_name=$SERVICE_HOSTNAME
# Path to the TLS certificate.
# Clients automatically generate trust stores from certificates.
infinispan.client.hotrod.trust_store_path=tls.crt
Copy to Clipboard Toggle word wrap

10.5.3. 使用 Hot Rod 客户端创建缓存

您可以使用 Hot Rod 客户端在 OpenShift 上运行的 Data Grid 集群上远程创建缓存。但是,Data Grid 建议使用 Data Grid 控制台、CLI 或带有 Cache CR 而不是 Hot Rod 客户端创建缓存。

以编程方式创建缓存

以下示例演示了如何将缓存配置添加到 ConfigurationBuilder 中,然后使用 RemoteCacheManager 创建它们:

import org.infinispan.client.hotrod.DefaultTemplate;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
...

      builder.remoteCache("my-cache")
             .templateName(DefaultTemplate.DIST_SYNC);
      builder.remoteCache("another-cache")
             .configuration("<infinispan><cache-container><distributed-cache name=\"another-cache\"><encoding media-type=\"application/x-protostream\"/></distributed-cache></cache-container></infinispan>");
      try (RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build())) {
      // Get a remote cache that does not exist.
      // Rather than return null, create the cache from a template.
      RemoteCache<String, String> cache = cacheManager.getCache("my-cache");
      // Store a value.
      cache.put("hello", "world");
      // Retrieve the value and print it.
      System.out.printf("key = %s\n", cache.get("hello"));
Copy to Clipboard Toggle word wrap

本例演示了如何使用 XMLStringConfiguration () 方法创建名为 CacheWithXMLConfiguration 的缓存,将缓存配置作为 XML 传递:

import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.commons.configuration.XMLStringConfiguration;
...

private void createCacheWithXMLConfiguration() {
    String cacheName = "CacheWithXMLConfiguration";
    String xml = String.format("<infinispan>" +
                                  "<cache-container>" +
                                  "<distributed-cache name=\"%s\" mode=\"SYNC\">" +
                                    "<encoding media-type=\"application/x-protostream\"/>" +
                                    "<locking isolation=\"READ_COMMITTED\"/>" +
                                    "<transaction mode=\"NON_XA\"/>" +
                                    "<expiration lifespan=\"60000\" interval=\"20000\"/>" +
                                  "</distributed-cache>" +
                                  "</cache-container>" +
                                "</infinispan>"
                                , cacheName);
    manager.administration().getOrCreateCache(cacheName, new XMLStringConfiguration(xml));
    System.out.println("Cache with configuration exists or is created.");
}
Copy to Clipboard Toggle word wrap
使用 Hot Rod 客户端属性

当您为指定缓存调用 cacheManager.getCache () 调用时,Data Grid 从 Hot Rod 客户端属性创建它们,而不是返回 null。

在 Hot Rod 客户端属性中添加缓存配置,如下例所示:

# Add cache configuration
infinispan.client.hotrod.cache.my-cache.template_name=org.infinispan.DIST_SYNC
infinispan.client.hotrod.cache.another-cache.configuration=<infinispan><cache-container><distributed-cache name=\"another-cache\"/></cache-container></infinispan>
infinispan.client.hotrod.cache.my-other-cache.configuration_uri=file:/path/to/configuration.xml
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2026 Red Hat