第 2 章 热 Rod Java 客户端配置


Data Grid 提供了一个 Hot Rod Java 客户端配置 API,用于公开配置属性。

2.1. 配置 Hot Rod 客户端连接

配置 Hot Rod Java 客户端连接数据网格服务器。

流程

  • 使用 ConfigurationBuilder 类来生成不可变配置对象,您可以传递给 RemoteCacheManager,或使用应用 classpath 上的 hotrod-client.properties 文件。

ConfigurationBuilder

ConfigurationBuilder builder = new ConfigurationBuilder();
builder.addServer()
         .host("127.0.0.1")
         .port(ConfigurationProperties.DEFAULT_HOTROD_PORT)
       .addServer()
         .host("192.0.2.0")
         .port(ConfigurationProperties.DEFAULT_HOTROD_PORT)
       .security().authentication()
         .username("username")
         .password("changeme")
         .realm("default")
         .saslMechanism("SCRAM-SHA-512");
RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
Copy to Clipboard Toggle word wrap

hotrod-client.properties

infinispan.client.hotrod.server_list = 127.0.0.1:11222,192.0.2.0:11222
infinispan.client.hotrod.auth_username = username
infinispan.client.hotrod.auth_password = changeme
infinispan.client.hotrod.auth_realm = default
infinispan.client.hotrod.sasl_mechanism = SCRAM-SHA-512
Copy to Clipboard Toggle word wrap

配置 Hot Rod URI

您还可以使用 URI 配置 Hot Rod 客户端连接,如下所示:

ConfigurationBuilder

ConfigurationBuilder builder = new ConfigurationBuilder();
builder.uri("hotrod://username:changeme@127.0.0.1:11222,192.0.2.0:11222?auth_realm=default&sasl_mechanism=SCRAM-SHA-512");
RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
Copy to Clipboard Toggle word wrap

hotrod-client.properties

infinispan.client.hotrod.uri = hotrod://username:changeme@127.0.0.1:11222,192.0.2.0:11222?auth_realm=default&sasl_mechanism=SCRAM-SHA-512
Copy to Clipboard Toggle word wrap

在 classpath 之外添加属性

如果 hotrod-client.properties 文件不在应用程序类路径上,则需要指定位置,如下例所示:

ConfigurationBuilder builder = new ConfigurationBuilder();
Properties p = new Properties();
try(Reader r = new FileReader("/path/to/hotrod-client.properties")) {
   p.load(r);
   builder.withProperties(p);
}
RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
Copy to Clipboard Toggle word wrap

2.1.1. 在客户端配置中定义 Data Grid 集群

在 Hot Rod 客户端配置中提供 Data Grid 集群的位置。

流程

  • 至少提供一个 Data Grid 集群名称以及至少一个带有 ClusterConfigurationBuilder 类的节点的主机名和端口。

    如果要将集群定义为默认集群,以便客户端始终首先尝试连接到它,然后使用 addServers ("<host_name>:<port>; <host_name>:<port>") 方法定义服务器列表。

多个集群连接

ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
clientBuilder.addCluster("siteA")
               .addClusterNode("hostA1", 11222)
               .addClusterNode("hostA2", 11222)
             .addCluster("siteB")
               .addClusterNodes("hostB1:11222; hostB2:11222");
RemoteCacheManager remoteCacheManager = new RemoteCacheManager(clientBuilder.build());
Copy to Clipboard Toggle word wrap

带有故障转移集群的默认服务器列表

ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
clientBuilder.addServers("hostA1:11222; hostA2:11222")
             .addCluster("siteB")
               .addClusterNodes("hostB1:11222; hostB2:11223");
RemoteCacheManager remoteCacheManager = new RemoteCacheManager(clientBuilder.build());
Copy to Clipboard Toggle word wrap

2.1.2. 手动切换 Data Grid 集群

在 Data Grid 集群间手动切换 Hot Rod Java 客户端连接。

流程

  • RemoteCacheManager 类中调用以下方法之一:

    switchToCluster (clusterName) 切换到客户端配置中定义的特定集群。

    switchToDefaultCluster () 切换到客户端配置中的默认集群,该集群被定义为 Data Grid 服务器列表。

2.1.3. 配置连接池

热 Rod Java 客户端保持与数据网格服务器的持久连接池,以重复利用 TCP 连接,而不是在每个请求上创建它们。

流程

  • 配置 Hot Rod 客户端连接池设置,如下例所示:

ConfigurationBuilder

ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
clientBuilder.addServer()
               .host("127.0.0.1")
               .port(11222)
             .connectionPool()
               .maxActive(10)
               exhaustedAction(ExhaustedAction.valueOf("WAIT"))
               .maxWait(1)
               .minIdle(20)
               .minEvictableIdleTime(300000)
               .maxPendingRequests(20);
RemoteCacheManager remoteCacheManager = new RemoteCacheManager(clientBuilder.build());
Copy to Clipboard Toggle word wrap

hotrod-client.properties

infinispan.client.hotrod.server_list = 127.0.0.1:11222
infinispan.client.hotrod.connection_pool.max_active = 10
infinispan.client.hotrod.connection_pool.exhausted_action = WAIT
infinispan.client.hotrod.connection_pool.max_wait = 1
infinispan.client.hotrod.connection_pool.min_idle = 20
infinispan.client.hotrod.connection_pool.min_evictable_idle_time = 300000
infinispan.client.hotrod.connection_pool.max_pending_requests = 20
Copy to Clipboard Toggle word wrap

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat