21.4. 热 Rod 客户端
hot Rod 是一个二进制 TCP 协议,Data Grid 提供带有远程客户端的高性能数据传输功能。
客户端智能
Hot Rod 协议包含一个机制,可为客户端提供缓存拓扑的最新视图。客户端智能通过减少读写操作的网络跃点数量来提高性能。
在同一 OpenShift 集群中运行的客户端可以访问 Data Grid pod 的内部 IP 地址,以便您可以使用任何客户端智能。
HASH_DISTRIBUTION_AWARE 是默认的智能机制,使客户端能够将请求路由到主所有者,从而为 Hot Rod 客户端提供最佳性能。
在不同 OpenShift 或 OpenShift 外部运行的客户端可以使用 LoadBalancer、NodePort 或 OpenShift Route 访问数据网格。
通过 OpenShift 路由进行热 Rod 客户端连接 需要加密。您必须使用 SNI 配置 TLS,否则 Hot Rod 连接会失败。
对于未加密的 Hot Rod 客户端连接,您必须使用 LoadBalancer 服务或 NodePort 服务。
在以下情况下,热 Rod 客户端必须使用 BASIC 智能:
-
通过
LoadBalancer服务、NodePort服务或 OpenShift 路由连接到 Data Grid。 - 使用跨站点复制时故障转移到不同的 OpenShift 集群。
OpenShift 集群管理员可以定义网络策略,以限制到 Data Grid 的流量。在某些情况下,网络隔离策略可能需要使用 BASIC 智能,即使客户端在同一 OpenShift 集群中运行,但使用不同的命名空间。
21.4.1. hot Rod 客户端配置 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
外部 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)的一部分。热 Rod 客户端还必须使用 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 控制台、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