第2章 Hot Rod Java クライアント設定
Data Grid は、設定プロパティーを公開する Hot Rod Java クライアント設定 API を提供します。
2.1. Hot Rod クライアント接続の設定 リンクのコピーリンクがクリップボードにコピーされました!
Data Grid Server への Hot Rod Java クライアント接続を設定します。
手順
-
RemoteCacheManagerに渡すか、アプリケーションのクラスパスのhotrod-client.propertiesファイルを使用できることを不変の設定オブジェクトを生成するConfigurationBuilderをクラス使用します。
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());
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
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());
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
クラスパスの外部へのプロパティーの追加
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());
2.1.1. クライアント設定での Data Grid クラスターの定義 リンクのコピーリンクがクリップボードにコピーされました!
Hot Rod クライアント設定で Data Grid クラスターの場所を指定します。
手順
ClusterConfigurationBuilderクラスを使用して、少なくとも 1 つのノードのホスト名とポートとともに少なくとも 1 つの Data Grid クラスター名を指定します。クラスターをデフォルトとして定義し、クライアントが常に最初にクラスターに接続を試みるようにする場合は、
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());
フェールオーバークラスターを含むデフォルトのサーバーリスト
ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
clientBuilder.addServers("hostA1:11222; hostA2:11222")
.addCluster("siteB")
.addClusterNodes("hostB1:11222; hostB2:11223");
RemoteCacheManager remoteCacheManager = new RemoteCacheManager(clientBuilder.build());
2.1.2. Data Grid クラスターの手動切り替え リンクのコピーリンクがクリップボードにコピーされました!
Data Grid クラスター間で Hot Rod Java クライアント接続を手動で切り替えます。
手順
RemoteCacheManagerクラスで次のいずれかのメソッドを呼び出します。switchToCluster(clusterName)は、クライアント設定で定義された特定のクラスターに切り替えます。switchToDefaultCluster()は、Data Grid サーバーのリストとして定義されているクライアント設定のデフォルトクラスターに切り替えます。
2.1.3. 接続プールの設定 リンクのコピーリンクがクリップボードにコピーされました!
Hot Rod Java クライアントは、Data Grid サーバーへの永続的な接続のプールを保持して、要求ごとに TCP 接続を作成するのではなく、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());
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