1. 概述

本文将带你完成一个实战任务:将我们之前在 Bootstrap a Simple Application using Spring Boot 教程中创建的简单应用,部署到 Google Cloud Platform(GCP)上的 App Engine

整个过程包括以下内容:

✅ 配置 Google Cloud Platform 控制台和 SDK
✅ 使用 Cloud SQL 创建一个 MySQL 实例
✅ 为 Spring Cloud GCP 配置应用
✅ 将应用部署到 App Engine 并进行测试

2. 配置 Google Cloud Platform

首先我们要准备好本地环境,确保能与 GCP 交互。你可以访问 GCP 官网 下载并安装 SDK。

初始化 GCP 环境:

gcloud init

设置你的项目 ID(这里我们假设项目名为 baeldung-spring-boot-bootstrap):

gcloud config set project baeldung-spring-boot-bootstrap

安装 App Engine 的 Java 支持,并创建 App Engine 实例:

gcloud components install app-engine-java
gcloud app create

创建 Cloud SQL 实例

我们的应用需要连接 MySQL 数据库,所以要通过 Cloud SQL 创建一个实例。

⚠️ 注意:Cloud SQL 没有免费额度,因此需要在 GCP 账户中启用 计费功能

查看可用的实例规格:

gcloud sql tiers list

部署前,请确保已在 GCP 控制台中启用 Cloud SQL Admin API

接着,使用 CLI 创建 MySQL 实例和数据库。注意:应用和数据库实例必须在同一个 region

我们选择部署区域为 europe-west2,所以数据库实例也放在该区域:

# 创建实例
gcloud sql instances create \
  baeldung-spring-boot-bootstrap-db \
    --tier=db-f1-micro \
    --region=europe-west2

# 创建数据库
gcloud sql databases create \
  baeldung_bootstrap_db \
    --instance=baeldung-spring-boot-bootstrap-db

3. 添加 Spring Cloud GCP 依赖

为了让应用能与 GCP 资源无缝集成,我们需要引入 Spring Cloud GCP 的相关依赖。

pom.xml 中添加一个名为 cloud-gcp 的 Maven Profile:

<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 运行环境:

✅ Standard 环境:基于 Jetty 和 JDK8,适合轻量级应用
✅ Flexible 环境:支持 JDK8,更适合 Spring Boot 应用

我们选择 Flexible 环境,并通过 app.yaml 激活所需的 Spring Profile(gcpmysql):

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

然后使用 Maven 插件打包并部署:

mvn clean package appengine:deploy -P cloud-gcp

部署完成后,可以查看或实时追踪日志:

# 查看日志
gcloud app logs read

# 实时日志
gcloud app logs tail

测试一下接口是否正常工作,比如添加一本书:

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

预期返回:

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

6. 应用扩缩容

默认情况下,App Engine 启用自动扩缩容(Automatic Scaling)。

但在初期,建议使用手动扩缩容(Manual Scaling)以更好地控制资源消耗和成本。

你可以在 app.yaml 中配置资源和自动扩缩容策略:

# 应用资源配置
resources:
  cpu: 2
  memory_gb: 2
  disk_size_gb: 10
  volumes:
  - name: ramdisk1
    volume_type: tmpfs
    size_gb: 0.5

# 自动扩缩容
automatic_scaling: 
  min_num_instances: 1 
  max_num_instances: 4 
  cool_down_period_sec: 180 
  cpu_utilization: 
    target_utilization: 0.6

7. 总结

在本教程中我们完成了以下操作:

✅ 配置了 GCP 和 App Engine
✅ 使用 Cloud SQL 创建了 MySQL 实例
✅ 引入 Spring Cloud GCP 并配置数据库连接
✅ 成功部署并测试了 Spring Boot 应用
✅ 对应用进行了扩缩容配置

如需深入学习,可以查阅 Google 官方 App Engine 文档

完整代码示例一如既往地托管在 GitHub:https://github.com/eugenp/tutorials/tree/master/spring-boot-modules/spring-boot-bootstrap


原始标题:Deploy a Spring Boot Application to Google App Engine