練習 - 建立使用 Spring Data Redis 的 Spring Boot 應用程式
在此單元中,您會建立一個 Spring Boot 應用程式,以使用 Spring Data Redis 從 Azure Cache for Redis 儲存和擷取資料。 在等候 Azure Cache for Redis 執行個體完成部署時,你可以建立應用程式,但 Azure Cache for Redis 的最終連線除外。
建立 Spring Boot 專案
若要建立 Spring Boot 專案,請執行下列 Spring Initializr 命令列:
curl https://start.spring.io/starter.tgz -d type=maven-project -d dependencies=web,data-redis -d baseDir=spring-redis-application -d bootVersion=2.4.1.RELEASE -d javaVersion=1.8 | tar -xzvf -
注意
命令會使用 Spring Web
和 Spring Data Redis
元件。 Spring Data Redis
會使用 Lettuce Redis 驅動程式,您也可以用於更進階的工作。
將 Spring Code 新增至管理資料
在 Spring Boot 專案中,在 [DemoApplication] 類別旁新增一個 [Todo] 網域物件,如下所示:
package com.example.demo; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; import java.io.Serializable; @RedisHash("Todo") public class Todo implements Serializable { public Todo() { } public Todo(String description, String details, boolean done) { this.description = description; this.details = details; this.done = done; } @Id private Long id; private String description; private String details; private boolean done; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getDetails() { return details; } public void setDetails(String details) { this.details = details; } public boolean isDone() { return done; } public void setDone(boolean done) { this.done = done; } }
建立名為 TodoRepository 的 Spring Data Redis 存放庫來管理此集合,如下所示:
package com.example.demo; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository public interface TodoRepository extends CrudRepository<Todo, String> { }
新增名為 TodoController 的 Spring MVC 控制器,如下所示:
package com.example.demo; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/") public class TodoController { private final TodoRepository todoRepository; public TodoController(TodoRepository todoRepository) { this.todoRepository = todoRepository; } @PostMapping("/") @ResponseStatus(HttpStatus.CREATED) public Todo createTodo(@RequestBody Todo todo) { return todoRepository.save(todo); } @GetMapping("/") public Iterable<Todo> findAllTodos() { return todoRepository.findAll(); } }
取得 Azure Cache for Redis 安全性金鑰
執行下列命令來檢查您的 Azure Cache for Redis 執行個體是否準備就緒。
az redis show --name $AZ_REDIS_NAME --resource-group $AZ_RESOURCE_GROUP
此命令會傳回包含
provisioningState
屬性的 JSON 資料。 當provisioningState
具有值時Succeeded
時,您的 Azure Cache for Redis 執行個體會完全可用。提示
如果您有 jq 公用程式,您可以使用下列單一命令列來檢查整備程度:
az redis show --name $AZ_REDIS_NAME --resource-group $AZ_RESOURCE_GROUP | jq '.provisioningState'
當 Azure Cache for Redis 執行個體準備就緒時,請執行下列命令來擷取其安全性金鑰:
az redis list-keys \ --resource-group $AZ_RESOURCE_GROUP \ --name $AZ_REDIS_NAME
從輸出複製
primaryKey
值,以在下一個步驟中使用。
設定 Spring Boot 以連線到 Azure Cache for Redis
在您的應用程式中開啟 src/main/resources/application.properties 設定檔案,然後新增下列屬性。 以您的 Redis 執行個體名稱取代 <redisName>
預留位置,並用您從上一個步驟取得的 primaryKey
值取代 <redisPrimaryKey>
預留位置。
spring.redis.host=<redisName>.redis.cache.windows.net
spring.redis.password=<redisPrimaryKey>
spring.redis.port=6380
spring.redis.ssl=true
在本機測試應用程式
執行 Spring Boot 應用程式,方法是在開發環境中執行可執行的 DemoApplication ,或執行 Spring Boot Maven 外掛程式,如下所示:
./mvnw spring-boot:run
執行應用程式時,請使用下列命令,將某些資料儲存在 Redis 中:
curl -d '{"description":"a description", "details":"some details"}' -H "Content-Type: application/json" -X POST http://127.0.0.1:8080
現在從 Redis 擷取該資料:
curl http://127.0.0.1:8080
請繼續下一個單元,了解如何使用 Azure Cache for Redis 透過 Spring Session 儲存 HTTP 工作階段資料。