21.4. 热 Rod 客户端

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

客户端智能

Hot Rod 协议包括为客户端提供缓存拓扑视图的机制。客户端智能通过减少读写操作的网络跃点数量来提高性能。

在同一 OpenShift 集群中运行的客户端可以访问 Data Grid pod 的内部 IP 地址,您可以使用任何客户端智能。

HASH_DISTRIBUTION_AWARE 是默认的智能机制,使客户端能够将请求路由到主所有者,为 Hot Rod 客户端提供最佳性能。

BASIC 智能

热备份客户端必须在以下情况下使用 BASIC 智能:

  • 通过 LoadBalancerNodePort 或 OpenShift Route 连接到 Data Grid。
  • 在使用跨站点复制时,切换到不同的 OpenShift 集群。

OpenShift 集群管理员可以定义将流量限制到 Data Grid 的网络策略。在某些情况下,网络隔离策略需要您使用 BASIC 智能,即使客户端在同一 OpenShift 集群中运行,但不同的命名空间也是如此。

21.4.1. 热备份客户端配置 API

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

注意

将以下示例中的 $SERVICE_HOSTNAME 替换为您的 Data Grid 集群的内部服务名称。

metadata:
  name: infinispan
对于 OpenShift

ConfigurationBuilder

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("$HOSTNAME")
               .port(ConfigurationProperties.DEFAULT_HOTROD_PORT)
             .security().authentication()
               .username("username")
               .password("changeme")
               .realm("default")
               .saslQop(SaslQop.AUTH)
               .saslMechanism("SCRAM-SHA-512")
             .ssl()
               .sniHostName("$SERVICE_HOSTNAME")
               .trustStoreFileName("/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt")
               .trustStoreType("pem");

hotrod-client.properties

# Connection
infinispan.client.hotrod.server_list=$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
infinispan.client.hotrod.trust_store_file_name=/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt
infinispan.client.hotrod.trust_store_type=pem

Outside OpenShift

ConfigurationBuilder

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("$HOSTNAME")
               .port("$PORT")
             .security().authentication()
               .username("username")
               .password("changeme")
               .realm("default")
               .saslQop(SaslQop.AUTH)
               .saslMechanism("SCRAM-SHA-512")
             .ssl()
               .sniHostName("$SERVICE_HOSTNAME")
               //Create a client trust store with tls.crt from your project.
               .trustStoreFileName("/path/to/truststore.pkcs12")
               .trustStorePassword("trust_store_password")
               .trustStoreType("PCKS12");
      builder.clientIntelligence(ClientIntelligence.BASIC);

hotrod-client.properties

# Connection
infinispan.client.hotrod.server_list=$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
# Create a client trust store with tls.crt from your project.
infinispan.client.hotrod.trust_store_file_name=/path/to/truststore.pkcs12
infinispan.client.hotrod.trust_store_password=trust_store_password
infinispan.client.hotrod.trust_store_type=PCKS12

21.4.2. 配置 Hot Rod 客户端以进行证书身份验证

如果启用客户端证书身份验证,则客户端必须在使用 Data Grid 协商连接时显示有效的证书。

验证策略

如果使用 Validate 策略,则必须为客户端配置密钥存储,以便它们出现签名证书。您还必须使用 Data Grid 凭证以及任何合适的身份验证机制来配置客户端。

验证策略

如果使用 Authenticate 策略,您必须为客户端配置包含签名证书的密钥存储和有效的 Data Grid 凭证,作为可分辨名称(DN)的一部分。热备份客户端还必须使用 EXTERNAL 身份验证机制。

注意

如果启用安全授权,您应该从客户端证书分配具有适当权限的角色的通用名称(CN)。

以下示例显示了使用 Authenticate 策略进行客户端证书验证的 Hot Rod 客户端配置:

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

ConfigurationBuilder builder = new ConfigurationBuilder();
      builder.security()
             .authentication()
               .saslMechanism("EXTERNAL")
             .ssl()
               .keyStoreFileName("/path/to/keystore")
               .keyStorePassword("keystorepassword".toCharArray())
               .keyStoreType("PCKS12");

21.4.3. 从 Hot Rod 客户端创建缓存

您可以使用 Hot Rod 客户端在 OpenShift 上运行的 Data Grid 集群上远程创建缓存。但是,Data Grid 建议您使用 Data Grid Console、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"));

本例演示了如何使用 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("<distributed-cache name=\"%s\">" +
                                  "<encoding media-type=\"application/x-protostream\"/>" +
                                  "<locking isolation=\"READ_COMMITTED\"/>" +
                                  "<transaction mode=\"NON_XA\"/>" +
                                  "<expiration lifespan=\"60000\" interval=\"20000\"/>" +
                                "</distributed-cache>"
                                , cacheName);
    manager.administration().getOrCreateCache(cacheName, new XMLStringConfiguration(xml));
    System.out.println("Cache with configuration exists or is created.");
}
使用 Hot Rod 客户端属性

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

hotrod-client.properties 中添加缓存配置,如下例所示:

# 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