1.3. 请求负载平衡
热 Rod Java 客户端平衡对数据平面集群的请求,以便读取和写入操作分散到节点上。
使用 BASIC 或 TOPOLOGY_AWARE 智能的客户端为所有请求使用请求平衡。使用 HASH_DISTRIBUTION_AWARE 智能的客户端将请求直接发送到存储所需密钥的节点。如果节点没有响应,客户端会返回请求均衡。
默认平衡策略是循环的,因此 Hot Rod 客户端执行请求平衡,如下例所示,s1、s 2、s3 是 Data Grid 集群中的节点:
// Connect to the Data Grid cluster
RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
// Obtain the remote cache
RemoteCache<String, String> cache = cacheManager.getCache("test");
//Hot Rod client sends a request to the "s1" node
cache.put("key1", "aValue");
//Hot Rod client sends a request to the "s2" node
cache.put("key2", "aValue");
//Hot Rod client sends a request to the "s3" node
String value = cache.get("key1");
//Hot Rod client sends the next request to the "s1" node again
cache.remove("key2");
自定义平衡策略
如果在 Hot Rod 客户端配置中添加类,您可以使用自定义 FailoverRequestBalancingStrategy 实现。
ConfigurationBuilder
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.addServer()
.host("127.0.0.1")
.port(11222)
.balancingStrategy(new MyCustomBalancingStrategy());
hotrod-client.properties
infinispan.client.hotrod.request_balancing_strategy=my.package.MyCustomBalancingStrategy