知識庫 / DevOps RSS 訂閱

將 Spring Boot 應用程序部署到 Google App Engine

Cloud,DevOps,Spring
HongKong
4
01:38 PM · Dec 06 ,2025

1. 概述

本教程將演示如何從“使用 Spring Boot 啓動一個簡單應用程序”教程部署應用程序到 Google Cloud Platform 的 App Engine。

作為其中一部分,我們將:

  • 配置 Google Cloud Platform 控制枱和 SDK
  • 使用 Cloud SQL 創建一個 MySQL 實例
  • 配置應用程序以供 Spring Cloud GCP 使用
  • 將應用程序部署到 App Engine 並進行測試

2. Google Cloud Platform 配置

我們可以使用 GCP 控制枱為本地環境準備好以供 GCP 使用。您可以在官方網站上找到安裝過程:https://cloud.google.com/sdk/

讓我們使用 GCP 控制枱 在 GCP 上創建一個項目:

gcloud init

接下來,讓我們配置項目名稱:

gcloud config set project baeldung-spring-boot-bootstrap

然後我們將安裝 App Engine 支持並創建 App Engine 實例:

gcloud components install app-engine-java
gcloud app create

我們的應用程序需要連接到 Cloud SQL 環境中的 MySQL 數據庫。由於 Cloud SQL 不提供免費層級,我們需要在 GCP 賬户上啓用 計費

我們可以輕鬆地查看可用的層級。

gcloud sql tiers list

在繼續之前,我們應該使用 GCP 網站啓用 Cloud SQL Admin API

現在,我們可以使用 Cloud Console 或 SDK CLI 在 Cloud SQL 中創建 MySQL 實例和數據庫。 在此過程中,我們將選擇區域並提供實例名稱和數據庫名稱。 重要的是,應用程序和數據庫實例必須位於同一區域。

由於我們將應用程序部署到 europe-west2,因此我們也將對實例執行相同的操作:

# create instance
gcloud sql instances create \
  baeldung-spring-boot-bootstrap-db \
    --tier=db-f1-micro \
    --region=europe-west2
# create database
gcloud sql databases create \
  baeldung_bootstrap_db \
    --instance=baeldung-spring-boot-bootstrap-db

3. Spring Cloud GCP 依賴項

我們的應用程序將需要來自 Spring Cloud GCP 項目的依賴項,用於雲原生 API。為此,我們使用一個 Maven 配置文件,名為 cloud-gcp

<profile>
  <id>cloud-gcp</id>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-gcp-starter</artifactId>
      <version>1.2.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
      <version>1.2.8.RELEASE</version>
    </dependency>
  </dependencies>

然後我們添加 App Engine Maven 插件:

    <build>
      <plugins>
        <plugin>
          <groupId>com.google.cloud.tools</groupId>
          <artifactId>appengine-maven-plugin</artifactId>
          <version>1.3.2</version>
        </plugin>
      </plugins>
    </build>
</profile>

4. 應用配置

現在,讓我們定義配置,允許應用程序使用雲原生資源,例如數據庫。

Spring Cloud GCP 使用 spring-cloud-bootstrap.properties 來確定應用程序名稱:

spring.cloud.appId=baeldung-spring-boot-bootstrap

我們將在本次部署中使用一個名為 gcp 的 Spring Profile,並且需要配置數據庫連接。因此,我們創建了 src/main/resources/application-gcp.properties 文件:

spring.cloud.gcp.sql.instance-connection-name=\
    baeldung-spring-boot-bootstrap:europe-west2:baeldung-spring-boot-bootstrap-db
spring.cloud.gcp.sql.database-name=baeldung_bootstrap_db

5. 部署

Google App Engine 提供兩個 Java 環境:

  • 標準環境提供 Jetty 和 JDK8,靈活環境則僅提供 JDK8
  • 靈活環境是 Spring Boot 應用程序的最佳選擇。

我們需要激活 gcpmysql Spring 配置文件,因此我們通過將它們添加到部署配置中,將環境變量 SPRING_PROFILES_ACTIVE 添加到應用程序中:在 src/main/appengine/app.yaml 中。

runtime: java
env: flex
runtime_config:
  jdk: openjdk8
env_variables:
  SPRING_PROFILES_ACTIVE: "gcp,mysql"
handlers:
- url: /.*
  script: this field is required, but ignored
manual_scaling: 
  instances: 1

現在,讓我們使用 appengine Maven 插件構建和部署應用程序:

mvn clean package appengine:deploy -P cloud-gcp

部署完成後,我們可以查看或跟蹤日誌文件:

# view
gcloud app logs read

# tail
gcloud app logs tail

現在,讓我們通過添加一本書來驗證我們的應用程序是否正常工作:

http POST https://baeldung-spring-boot-bootstrap.appspot.com/api/books \
        title="The Player of Games" author="Iain M. Banks"

Expecting the following output:

HTTP/1.1 201 
{
    "author": "Iain M. Banks",
    "id": 1,
    "title": "The Player of Games"
}

6. 擴展應用程序

App Engine 的默認擴展是自動的。

在充分理解運行時行為、相關的預算和成本之前,最好先採用手動擴展,以便更好地控制資源分配。您可以在 app.yaml 中分配資源並配置自動擴展:

# Application Resources
resources:
  cpu: 2
  memory_gb: 2
  disk_size_gb: 10
  volumes:
  - name: ramdisk1
    volume_type: tmpfs
    size_gb: 0.5
# Automatic Scaling
automatic_scaling: 
  min_num_instances: 1 
  max_num_instances: 4 
  cool_down_period_sec: 180 
  cpu_utilization: 
    target_utilization: 0.6

7. 結論

在本教程中,我們:

  • 配置了 Google Cloud Platform 和 App Engine
  • 創建了使用 Cloud SQL 的 MySQL 實例
  • 配置了 Spring Cloud GCP 以使用 MySQL
  • 部署了我們配置的 Spring Boot 應用程序,以及
  • 測試和擴展了該應用程序

我們始終可以參考 Google 的廣泛 App Engine 文檔 以獲取更多詳細信息。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.