程式碼研究室簡介
1. 總覽
Memorystore for Redis 是 Google Cloud 的全代管 Redis 服務。在 Google Cloud 上執行的應用程式可運用具備高擴充性、可用性與安全性的 Redis 服務,輕鬆管理極致的效能,而且無須管理複雜的 Redis 部署作業。它可以用來當做資料快取的後端,以提高 Spring Boot 應用程式的效能。本程式碼研究室說明如何進行設定。
您將會瞭解的內容
- 如何使用 Memorystore 做為 Spring Boot 應用程式的快取後端。
軟硬體需求
- Google Cloud 專案
- 瀏覽器,例如 Google Chrome
- 熟悉 Vi Linux、Emacs 和 GNU Nano 等標準 Linux 文字編輯器
您要如何使用程式碼研究室?
您對於 Google Cloud 服務的使用體驗評價如何?
2. 設定和需求
自行調整環境設定
如果您還沒有 Google 帳戶 (Gmail 或 Google Apps),請先建立帳戶。登入 Google Cloud Platform 主控台 (console.cloud.google.com),然後建立新專案:
提醒您,專案編號是所有 Google Cloud 專案的不重複名稱 (使用上述名稱後就無法使用,敬請見諒!)此程式碼研究室稍後將稱為 PROJECT_ID
。
接著,您必須在 Cloud Console 中啟用計費功能,才能使用 Google Cloud 資源。
完成這個程式碼研究室的成本應該不會超過新臺幣 $300 元,但如果您決定繼續使用更多資源,或是讓資源繼續運作 (請參閱本文件結尾的「清除設定」一節),就有可能需要更多成本。
新加入 Google Cloud Platform 的使用者可免費試用$300 美元。
啟用 Google Cloud Shell
在 GCP 主控台中,按一下右上角的工具列的 Cloud Shell 圖示:
然後按一下 [Start Cloud Shell]。
佈建和連線至環境只需要幾分鐘的時間:
這部虛擬機器已載入所有您需要的開發工具。這項服務提供永久性的 5GB 主目錄,可在 Google Cloud 中運作,大幅提升網路效能和驗證效能。在這個研究室中,您可以透過瀏覽器或 Google Chromebook 完成大部分的工作。
連線至 Cloud Shell 之後,您應該會看到您已經通過驗證,且專案已經設定為 PROJECT_ID。
在 Cloud Shell 中執行下列指令,確認您的身分已通過驗證:
gcloud auth list
指令輸出
Credentialed accounts: - <myaccount>@<mydomain>.com (active)
gcloud config list project
指令輸出
[core] project = <PROJECT_ID>
如果沒有,則可使用下列指令設定:
gcloud config set project <PROJECT_ID>
指令輸出
Updated property [core/project].
3. 設定 Memorystore for Redis 執行個體
啟動 Cloud Shell。
Cloud Shell 啟動後,您可以使用指令列來建立新的 Memorystore 執行個體。
$ gcloud redis instances create myinstance --size=1 --region=us-central1
如果 Memorystore API 未啟用,系統會詢問您是否要啟用該 API。回答 y。
API [redis.googleapis.com] not enabled on project [204466653457]. Would you like to enable and retry (this will take a few minutes)? (y/N)? y Enabling service redis.googleapis.com on project 204166153457... Waiting for async operation operations/tmo-acf.c8909997-1b4e-1a62-b6f5-7da75cce1416 to complete... Operation finished successfully. The following command can describe the Operation details: gcloud services operations describe operations/tmo-acf.c8909997-1b4e-1a62-b6f5-7da75cce1416 Create request issued for: [myinstance] Waiting for operation [operation-1538645026454-57763b937ad39-2564ab37-3fea7701] to complete...done. Created instance [myinstance].
作業完成後,執行個體即可開始使用。
執行下列指令取得執行個體的 redis host ip-address。稍後在設定 Spring Boot 應用程式時,將需要再次使用。
$ gcloud redis instances describe myinstance --region=us-central1 \ | grep host host: 10.0.0.4
如果您瀏覽至 Google Cloud Console 中的 [Storage] > [Memorystore],您應該會看到處於 [就緒] 狀態的執行個體:
4. 設定 Compute Engine 執行個體
在相同區域中建立 Compute Engine 執行個體。
$ gcloud compute instances create instance-1 --zone us-central1-c
作業完成後,執行個體即可開始使用。
透過 SSH 連線至 Compute > Compute Engine > VM 執行個體,然後按一下「連線」欄中的 [SSH],透過 SSH 連線至您的執行個體:
在虛擬機器 (VM) 執行個體殼層 (非 Cloud Shell) 中,安裝 OpenJDK、Maven 和 telnet:
$ sudo apt-get install openjdk-8-jdk-headless maven telnet
等待安裝完成,然後繼續進行下一個步驟。
5. 設定 Spring Boot 應用程式
以 web
、redis
和 cache
依附元件建立新的 Spring Boot 專案:
$ curl https://start.spring.io/starter.tgz \ -d dependencies=web,redis,cache -d language=java -d baseDir=cache-app \ | tar -xzvf - && cd cache-app
編輯 application.properties
檔案,將應用程式設為使用 Redis 執行個體的 Memorystore 主機 IP 位址。
$ nano src/main/resources/application.properties
將下列這一行替換為您的 Memorystore for Redis IP 位址 (前幾個步驟):
spring.redis.host=<memorystore-host-ip-address>
之後新增一行,並建立 REST 控制器 Java 類別:
$ nano src/main/java/com/example/demo/HelloWorldController.java
將下列內容加入檔案中:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @Autowired private StringRedisTemplate template; @RequestMapping("/hello/{name}") @Cacheable("hello") public String hello(@PathVariable String name) throws InterruptedException { Thread.sleep(5000); return "Hello " + name; } }
@RequestMapping
註解會以方法做為 HTTP 端點,將部分路徑對應至方法參數 (如 @PathVariable
註解所示)。
@Cacheable("hello")
註解表示應快取該方法的執行程序,而快取名稱為「hello
」;這是將參數值與快取金鑰搭配使用的值。您會在程式碼研究室中看到一個範例。
另外,您也必須在 Spring Boot 應用程式類別中啟用快取。
編輯「DemoApplication.java
」:
$ nano src/main/java/com/example/demo/DemoApplication.java
匯入 org.springframework.cache.annotation.EnableCaching
並以此註解為類別加上註解。結果看起來會像這樣:
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication @EnableCaching public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
6. 執行應用程式並存取端點
現在您已經準備好執行應用程式了!
$ mvn spring-boot:run
開啟之前與執行個體相同的 SSH 連線,方法與先前相同。在新的 SSH 視窗中多次存取 /hello/
端點,並傳送「bob
」做為名稱。
$ time curl http://localhost:8080/hello/bob Hello bob! real 0m5.408s user 0m0.036s sys 0m0.009s $ time curl http://localhost:8080/hello/bob Hello bob! real 0m0.092s user 0m0.021s sys 0m0.027s
請注意,雖然你第一次要求收到 5 秒,但在下一次執行時Thread.sleep(5000)
由於實際執行方式只執行一次,而結果已儲存至快取,所有後續的呼叫都會直接從快取傳回結果。
7. 查看快取物件
您可以實際查看應用程式快取的內容。使用您在上一個步驟所使用的終端機,透過 telnet 連線至 Memorystore for Redis 主機:
$ telnet <memorystore-host-ip-address> 6379
如要查看快取金鑰清單,請使用下列指令:
KEYS * hello::bob
如您所見,快取名稱會做為鍵的前置字串,參數值則是第二個部分。
如要擷取值,請使用 GET
指令:
$ GET hello::bob Hello bob!
使用 QUIT
指令結束。
8. 清除所用資源
如要進行清除,請從 Cloud Shell 中刪除 Compute Engine 與 Memorystore 執行個體。
刪除運算執行個體:
$ gcloud compute instances delete instance-1 --zone us-central1-c
刪除 Memorystore for Redis 執行個體:
$ gcloud redis instances delete myinstance --region=us-central1
9. 恭喜!
您已建立 Memorystore for Redis 和 Compute Engine 執行個體。此外,您還將 Spring Boot 應用程式設為搭配 Spring Boot 快取使用 Memorystore!
瞭解詳情
- 春靴靴
- Memorystore
- Spring for Google Cloud 專案
- 在 Google Cloud GitHub 存放區中使用 Spring
- 在 Google Cloud 中使用 Java
授權
本作品採用創用 CC 姓名標示 2.0 一般授權。