在 Spring 中使用 Azure Redis 快取

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

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

必要條件

  • Azure 訂用帳戶 - 免費建立一個訂用帳戶。

  • Java Development Kit (JDK) 第 8 版或更高版本。

  • Apache Maven 3.0 版或更高版本。

  • cURL 或類似的 HTTP 公用程式來測試功能。

  • Redis 快取實例。 如果您沒有,請參閱 快速入門:建立開放原始碼 Redis 快取

  • Spring Boot 應用程式。 如果您沒有 Maven 專案,請使用 Spring Initializr 建立 Maven 專案。 請務必選取 Maven 專案,然後在 [相依性] 底下新增 Spring WebSpring Data Reactive Redis 相依性,然後選取 [Java 第 8 版] 或更新版本。

編碼應用程式

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

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

    # 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>
    

    注意

    如果您使用不同的 Redis 用戶端,例如啟用 SSL 的 Jedis,您會指定您想要在 application.properties 檔案中使用 SSL,並使用埠 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。

  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));
        }
    
    }
    
  3. 啟動應用程式。 應用程式會從 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 檔中心。