Spring で Azure Redis Cache を使用する
Azure Cache for Redis は、Redis ソフトウェアをベースにしたメモリ内データストアを提供します。 Redis では、バックエンドのデータ ストアを多用するアプリケーションのパフォーマンスとスケーラビリティを高めることができます。
このチュートリアルでは、Redis Cache を使用して Spring Boot アプリケーションにデータを格納および取得する方法について説明します。
前提条件
Azure サブスクリプション - 無料アカウントを作成します。
Java Development Kit (JDK) バージョン 8 以降。
Apache Maven、バージョン 3.0 以降。
cURL または機能をテストするための類似の HTTP ユーティリティ。
Redis Cache インスタンス。 ない場合は、「クイック スタート: オープンソースの Redis Cache を作成する」を参照してください。
Spring Boot アプリケーション。 ない場合は、Spring Initializr で Maven プロジェクトを作成します。 [Maven プロジェクト] を選択して、[依存関係] で、[Spring Web] および [Spring Data Reactive Redis] 依存関係を追加したら、バージョン 8 以降の Java を選択します。
アプリケーションをコーディングする
Redis Cache を使用してデータを格納および取得するには、次の手順を実行してアプリケーションを構成します。
次の例に示すように、application.properties 構成ファイルで Redis Cache 資格情報を構成します。
# Specify the DNS URI of your Redis cache. spring.data.redis.host=<your-redis-name>.redis.cache.windows.net # Specify the port for your Redis cache. spring.data.redis.port=6379 # Specify the access key for your Redis cache. spring.data.redis.password=<your-redis-access-key>
Note
SSL を有効にする Jedis のような異なる Redis クライアントを使用している場合は、SSL 使用する旨を application.properties ファイルで指定し、ポート 6380 を使用します。 次に例を示します。
# Specify the DNS URI of your Redis cache. spring.data.redis.host=<your-redis-name>.redis.cache.windows.net # Specify the access key for your Redis cache. spring.data.redis.password=<your-redis-access-key> # Specify that you want to use SSL. spring.data.redis.ssl.enabled=true # Specify the SSL port for your Redis cache. spring.data.redis.port=6380
詳細については、「クイック スタート: Java で Azure Cache for Redis を使用する」を参照してください。
スタートアップ クラス ファイルを編集して、次の内容を表示します。 このコードは、データを格納および取得します。
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; @SpringBootApplication public class DemoCacheApplication implements CommandLineRunner { private static final Logger LOGGER = LoggerFactory.getLogger(DemoCacheApplication.class); @Autowired private StringRedisTemplate template; public static void main(String[] args) { SpringApplication.run(DemoCacheApplication.class, args); } @Override public void run(String... args) { ValueOperations<String, String> ops = this.template.opsForValue(); String key = "testkey"; if(!this.template.hasKey(key)){ ops.set(key, "Hello World"); LOGGER.info("Add a key is done"); } LOGGER.info("Return the value from the cache: {}", ops.get(key)); } }
アプリケーションを起動します。 アプリケーションは Redis Cache からデータを取得します。 次の例のようなログが表示されます。
Add a key is done Return the value from the cache: Hello World
Azure Spring Apps にデプロイする
Spring Boot アプリケーションがローカルで実行されたので、運用環境に移行します。 Azure Spring Apps では、コードを変更せずに、Spring Boot アプリケーションを Azure に簡単にデプロイできます。 Spring アプリケーションのインフラストラクチャはこのサービスによって管理されるため、開発者はコードに専念できます。 Azure Spring Apps では、包括的な監視と診断、構成管理、サービス検出、CI/CD 統合、ブルー/グリーン デプロイなどを使用して、ライフサイクルを管理できます。 Azure Spring Apps にアプリケーションをデプロイするには、「初めてのアプリケーションを Azure Spring Apps にデプロイする」を参照してください。
次のステップ
Spring および Azure の詳細については、Azure ドキュメント センターで引き続き Spring に関するドキュメントをご確認ください。
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示