Data Grid Spring Boot スターター


Red Hat Data Grid 8.0

Data Grid のドキュメント

概要

Spring Boot プロジェクトが Data Grid とシームレスに対話するために必要なすべてを含む一連の管理された推移的な依存関係を使用して、Spring Boot プロジェクトをすばやく起動して実行します。Data Grid Spring Boot スターターは、Spring Boot を開始するための便利な方法を提供しますが、オプションであることに注意してください。Spring Boot で Data Grid を使用するには、必要な依存関係を追加するだけです。

第1章 Red Hat Data Grid

Data Grid は、高性能の分散型インメモリーデータストアです。

スキーマレスデータ構造
さまざまなオブジェクトをキーと値のペアとして格納する柔軟性があります。
グリッドベースのデータストレージ
クラスター間でデータを分散および複製するように設計されています。
エラスティックスケーリング
サービスを中断することなく、ノードの数を動的に調整して要件を満たします。
データの相互運用性
さまざまなエンドポイントからグリッド内のデータを保存、取得、およびクエリーします。

1.1. Data Grid のドキュメント

Data Grid のドキュメントは、Red Hat カスタマーポータルで入手できます。

1.2. Data Grid のダウンロード

Red Hat カスタマーポータルで Data Grid Software Downloads にアクセスします。

注記

Data Grid ソフトウェアにアクセスしてダウンロードするには、Red Hat アカウントが必要です。

第2章 プロジェクトの設定

Data Grid Spring Boot スターターの依存関係をプロジェクトに追加します。

2.1. Data Grid バージョンの適用

このスターターは、高レベルの API を使用して、Data Grid のメジャーバージョン間の互換性を確保します。ただし、infinispan-bom モジュールを使用して特定のバージョンの Data Grid を適用できます。

以下のように、スターターの依存関係の前に、pom.xml ファイルに infinispan-bom を追加します。

<dependencyManagement>
    <dependencies>
       <dependency>
           <groupId>org.infinispan</groupId>
           <artifactId>infinispan-bom</artifactId>
           <version>${version.infinispan}</version>
           <type>pom</type>
           <scope>import</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>${version.spring.boot}</version>
           <type>pom</type>
           <scope>import</scope>
       </dependency>
       <dependency>
           <groupId>org.infinispan</groupId>
           <artifactId>infinispan-spring-boot-starter</artifactId>
           <version>2.2.4.Final-redhat-00001</version>
       </dependency>
    </dependencies>
 </dependencyManagement>
Copy to Clipboard Toggle word wrap
重要

Data Grid Spring Boot スターターは、Red Hat OpenShift Application Runtimes などの他のプロジェクトとは異なる Spring Boot バージョンを使用します。他のプロジェクトと互換性を確保するために特定の Spring Boot バージョンを使用する場合は、プロジェクトに正しい依存関係を追加する必要があります。

2.2. 使用モードの依存関係の追加

Data Grid は、使用モードごとに異なる依存関係を提供します。次のいずれかを pom.xml ファイルに追加します。

組み込みモード

<dependency>
  <groupId>org.infinispan</groupId>
  <artifactId>infinispan-spring-boot-starter-embedded</artifactId>
  <version>2.2.4.Final-redhat-00001</version>
</dependency>
Copy to Clipboard Toggle word wrap

リモートクライアント/サーバーモード

<dependency>
  <groupId>org.infinispan</groupId>
  <artifactId>infinispan-spring-boot-starter-remote</artifactId>
  <version>2.2.4.Final-redhat-00001</version>
</dependency>
Copy to Clipboard Toggle word wrap

第3章 組み込みモードでの実行

インメモリーデータストレージ用に、プロジェクトに Data Grid ライブラリーを埋め込みます。

3.1. EmbeddedCacheManager Bean の追加

  1. infinispan-spring-boot-starter-embedded をプロジェクトのクラスパスに追加して、組み込みモードを有効にします。

    このスターターは、デフォルトでクラスパス上の infinispan-spring-boot-starter-remote で、リモートクライアント/サーバーモードで動作します。

  2. 次の例のように、Spring @Autowired アノテーションを使用して、Java 設定クラスに EmbeddedCacheManagerBean を含めます。

    private final EmbeddedCacheManager cacheManager;
    
    @Autowired
    public YourClassName(EmbeddedCacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }
    Copy to Clipboard Toggle word wrap

    これで、組み込みモードで Data Grid を使用できるようになりました。以下は簡単な例です。

    cacheManager.getCache("testCache").put("testKey", "testValue");
    System.out.println("Received value from cache: " + cacheManager.getCache("testCache").get("testKey"));
    Copy to Clipboard Toggle word wrap

3.2. キャッシュマネージャーの設定 Bean

次の設定 Bean を使用してキャッシュマネージャーをカスタマイズできます。

  • InfinispanGlobalConfigurer
  • InfinispanCacheConfigurer
  • 設定
  • InfinispanConfigurationCustomizer
  • InfinispanGlobalConfigurationCustomizer

    注記

    InfinispanGlobalConfigurerBean は 1 つしか作成できません。ただし、他の Bean を使用して複数の設定を作成できます。

InfinispanCacheConfigurer Bean

@Bean
public InfinispanCacheConfigurer cacheConfigurer() {
	return manager -> {
		final Configuration ispnConfig = new ConfigurationBuilder()
                        .clustering()
                        .cacheMode(CacheMode.LOCAL)
                        .build();

		manager.defineConfiguration("local-sync-config", ispnConfig);
	};
}
Copy to Clipboard Toggle word wrap

設定 Bean

次のように、設定するキャッシュに Bean 名をリンクします。

@Bean(name = "small-cache")
public org.infinispan.configuration.cache.Configuration smallCache() {
    return new ConfigurationBuilder()
        .read(baseCache)
        .memory().size(1000L)
        .memory().evictionType(EvictionType.COUNT)
        .build();
}

@Bean(name = "large-cache")
public org.infinispan.configuration.cache.Configuration largeCache() {
    return new ConfigurationBuilder()
        .read(baseCache)
        .memory().size(2000L)
        .build();
}
Copy to Clipboard Toggle word wrap

カスタマイザー Bean

@Bean
public InfinispanGlobalConfigurationCustomizer globalCustomizer() {
   return builder -> builder.transport().clusterName(CLUSTER_NAME);
}

@Bean
public InfinispanConfigurationCustomizer configurationCustomizer() {
   return builder -> builder.memory().evictionType(EvictionType.COUNT);
}
Copy to Clipboard Toggle word wrap

3.3. Spring キャッシュサポートの有効化

@EnableCaching アノテーションをアプリケーションに追加し、Spring Cache のサポートを有効にします。

このスターターが EmbeddedCacheManager Bean を検出すると、Spring Cache の実装を提供する新しい SpringEmbeddedCacheManager をインスタンス化します。

第4章 サーバーモードでの実行

カスタム TCP バイナリーワイヤプロトコルである Hot Rod を使用して、リモートの Data Grid クラスターからデータを保存および取得します。

4.1. RemoteCacheManager の設定

  1. スターターが RemoteCacheManager Bean を作成できるように Data Grid サーバーの場所を指定します。

    スターターは最初に hotrod-client.properties でサーバーの場所を見つけ、次に application.properties から検索しようとします。

  2. Spring @Autowired アノテーションを使用して、独自のカスタムキャッシュマネージャークラスをアプリケーションに含めます。

    private final RemoteCacheManager cacheManager;
    
    @Autowired
    public YourClassName(RemoteCacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }
    Copy to Clipboard Toggle word wrap

Hot Rod クライアントプロパティー

以下のように、クラスパスの hotrod-client.properties でクライアント設定を指定します。

# List Infinispan or Data Grid servers by IP address or hostname at port 11222.
infinispan.client.hotrod.server_list=127.0.0.1:6667
Copy to Clipboard Toggle word wrap

詳細は、org.infinispan.client.hotrod.configuration を参照してください。

Application properties

application.properties を使用してプロジェクトを設定します。詳細は、アプリケーションプロパティー を参照してください。

4.2. マーシャリングの設定

Java シリアライゼーションを使用してオブジェクトをマーシャリングするように Data Grid サーバーを設定します。

デフォルトでは、Data Grid サーバーは ProtoStream シリアライゼーションライブラリーをデフォルトのマーシャラーとして使用します。ただし、ProtoStream マーシャラーは Spring 統合ではサポートされません。このため、Java Serialization Marshaller を使用する必要があります。

  • application.properties で以下のプロパティーを指定します。

    infinispan.remote.marshaller=org.infinispan.commons.marshall.JavaSerializationMarshaller 
    1
    
    infinispan.remote.java-serial-whitelist=your_marshalled_beans_package.* 
    2
    Copy to Clipboard Toggle word wrap
1
Java Serialization Marshaller を使用します。
2
クラスをシリアル化ホワイトリストに追加して、Data Grid がオブジェクトをマーシャリングできるようにします。完全修飾クラス名のコンマ区切りリストまたはクラスを照合するための正規表現を指定できます。

4.3. キャッシュマネージャーの設定 Bean

次の設定 Bean を使用してキャッシュマネージャーをカスタマイズします。

  • InfinispanRemoteConfigurer
  • 設定
  • InfinispanRemoteCacheCustomizer

    注記

    InfinispanRemoteConfigurerBean は 1 つしか作成できません。ただし、他の Bean を使用して複数の設定を作成できます。

InfinispanRemoteConfigurer Bean

@Bean
public InfinispanRemoteConfigurer infinispanRemoteConfigurer() {
    return () -> new ConfigurationBuilder()
        .addServer()
        .host("127.0.0.1")
        .port(12345)
        .build();
}
Copy to Clipboard Toggle word wrap

設定 Bean

@Bean
public org.infinispan.client.hotrod.configuration.Configuration customConfiguration() {
    new ConfigurationBuilder()
        .addServer()
        .host("127.0.0.1")
        .port(12345)
        .build();
}
Copy to Clipboard Toggle word wrap

InfinispanRemoteCacheCustomizer Bean

@Bean
public InfinispanRemoteCacheCustomizer customizer() {
    return b -> b.tcpKeepAlive(false);
}
Copy to Clipboard Toggle word wrap

ヒント

@Ordered アノテーションを使用して、カスタマイザーを特定の順序で適用します。

4.4. Spring キャッシュサポートの有効化

@EnableCaching アノテーションをアプリケーションに追加し、Spring Cache のサポートを有効にします。

Data Grid スターターが RemoteCacheManager Bean を検出すると、Spring Cache の実装を提供する新しい SpringRemoteCacheManager をインスタンス化します。

4.5. Data Grid 統計の公開

Data Grid は、Spring Boot Actuator をサポートして、キャッシュ統計をメトリクスとして公開します。

Actuator を使用するには、以下を pom.xml ファイルに追加します。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
  <version>${version.spring.boot}</version>
 </dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>${version.spring.boot}</version>
</dependency>
Copy to Clipboard Toggle word wrap

続いて、プログラムまたは宣言を使用して、適切なキャッシュインスタンスの統計を有効にする必要があります。

プログラムで

@Bean
public InfinispanCacheConfigurer cacheConfigurer() {
  return cacheManager -> {
     final org.infinispan.configuration.cache.Configuration config =
           new ConfigurationBuilder()
                 .jmxStatistics().enable()
                 .build();

     cacheManager.defineConfiguration("my-cache", config);
  };
}
Copy to Clipboard Toggle word wrap

宣言

<local-cache name="my-cache" statistics="true"/>
Copy to Clipboard Toggle word wrap

Spring Boot Actuator レジストリーは、アプリケーションの起動時にキャッシュインスタンスをバインドします。キャッシュを動的に作成する場合は、次のように、 CacheMetricsRegistrarBean を使用してキャッシュを Actuator レジストリーにバインドする必要があります。

@Autowire
CacheMetricsRegistrar cacheMetricsRegistrar;

@Autowire
CacheManager cacheManager;
...

cacheMetricsRegistrar.bindCacheToRegistry(cacheManager.getCache("my-cache"));
Copy to Clipboard Toggle word wrap

第5章 SpringSession の使用

5.1. Spring Session サポートの有効化

Data Grid Spring Session サポートは SpringRemoteCacheManagerSpringEmbeddedCacheManager をもとに構築されます。このスターターはは、デフォルトでこれらの Bean を生成します。

プロジェクトで Spring Session を使用するには、以下を実行します。

  1. このスターターをプロジェクトに追加します。
  2. Spring Session をクラスパスに追加します。
  3. 次のアノテーションを設定に追加します。

    • @EnableCaching
    • @EnableInfinispanRemoteHttpSession
    • @EnableInfinispanEmbeddedHttpSession

第6章 アプリケーションのプロパティー

application.properties または application.yaml を使用してプロジェクトを設定します。

# List Infinispan or Data Grid servers by IP address or hostname at port 11222.
infinispan.remote.server-list=127.0.0.1:11222

#
# Embedded Properties - Uncomment properties to use them.
#

# Enables embedded capabilities in your application.
# Values are true (default) or false.
#infinispan.embedded.enabled =

# Sets the Spring state machine ID.
#infinispan.embedded.machineId =

# Sets the name of the embedded cluster.
#infinispan.embedded.clusterName =

# Specifies a XML configuration file that takes priority over the global
# configuration bean or any configuration customizer.
#infinispan.embedded.configXml =

#
# Server Properties - Uncomment properties to use them.
#

# Specifies a custom filename for Hot Rod client properties.
#infinispan.remote.clientProperties =

# Enables remote server connections.
# Values are true (default) or false.
#infinispan.remote.enabled =

# Defines a comma-separated list of servers in this format:
# `host1[:port],host2[:port]`.
#infinispan.remote.serverList =

# Sets a timeout value, in milliseconds, for socket connections.
#infinispan.remote.socketTimeout =

# Sets a timeout value for initializing connections with servers.
#infinispan.remote.connectTimeout =

# Sets the maximum number of attempts to connect to servers.
#infinispan.remote.maxRetries =

# Specifies the marshaller to use.
#infinispan.remote.marshaller =

# Adds your classes to the serialization whitelist.
#infinispan.remote.java-serial-whitelist=
Copy to Clipboard Toggle word wrap
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2026 Red Hat