共用方式為


在 Spring 中使用 Azure Redis 快取

Azure Cache for Redis 會提供採用 Redis 軟體的記憶體內部資料存放區。 Redis 可改善大量使用後端數據存放區之應用程式的效能和延展性。

本教學課程示範如何使用 Redis 快取,在 Spring Boot 應用程式中儲存和擷取數據。

在本教學課程中,我們包含兩種驗證方法:Microsoft Entra 驗證和 Redis 驗證。 [無密碼] 索引標籤會顯示 Microsoft Entra 驗證,而 [密碼] 索引標籤會顯示 Redis 驗證。

Microsoft Entra 驗證是一種機制,可使用在 entra ID Microsoft 中定義的身分識別連線到 Azure Cache for Redis。 透過 Microsoft Entra 驗證,您可以在集中的位置管理資料庫使用者的身分識別和其他 Microsoft 服務,從而簡化權限管理。

Redis 驗證會在 Redis 中使用密碼。 如果您選擇使用密碼作為認證,則必須自行管理密碼。

必要條件

將數據快取至 Azure Cache for Redis

使用 Azure Cache for Redis 實例,您可以使用 Spring Cloud Azure 快取數據。

若要安裝 Spring Cloud Azure Starter Data Redis with Lettuce 模組,請將下列相依性新增至您的 pom.xml 檔案:

<dependencies>
 <dependency>
   <groupId>com.azure.spring</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
   <groupId>com.azure.spring</groupId>
   <artifactId>spring-cloud-azure-starter-data-redis-lettuce</artifactId>
 </dependency>
 <dependency>
   <groupId>com.azure.spring</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
</dependencies>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.azure.spring</groupId>
      <artifactId>spring-cloud-azure-dependencies</artifactId>
      <version>5.14.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

注意

此材料帳單 (BOM) 應該在<dependencyManagement>pom.xml檔案的 區段中設定。 此設定可確保所有 Spring Cloud Azure 相依性都使用相同的版本。 如需此 BOM 所用版本的詳細資訊,請參閱 應該使用哪個版本的 Spring Cloud Azure。

編碼應用程式

若要使用 Redis 快取來儲存和擷取數據,請使用下列步驟來設定應用程式:

  1. application.properties 組態檔中設定 Redis 快取認證,如下列範例所示。

    spring.data.redis.host=<your-redis-name>.redis.cache.windows.net
    spring.data.redis.port=6380
    spring.data.redis.username=<your-redis-username>
    spring.data.redis.ssl.enabled=true
    spring.data.redis.azure.passwordless-enabled=true
    

    注意

    若要取得的值username,請遵循在快取上使用 Microsoft Entra ID 進行快取驗證的快取區段啟用 Microsoft Entra ID 驗證中的指示,並複製使用者名稱值。

  2. 編輯啟動類別檔案以顯示下列內容。 此程式代碼會儲存和擷取數據。

    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 快取擷取數據。 您應該會看到類似下列範例的記錄:

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 應用程式提供生命週期管理,使用全方位的監視和診斷、組態管理、服務探索、持續整合與持續傳遞的整合、藍綠部署等等。 若要將應用程式部署至 Azure Spring Apps,請參閱 將第一個應用程式部署至 Azure Spring Apps

下一步

若要深入了解 Spring 和 Azure,請繼續閱讀「Azure 上的 Spring」文件中心中的資訊。