Deploy a Spring Boot Application to Google App Engine – 将Spring Boot应用部署到Google App Engine上

最后修改: 2018年 9月 30日

1. Overview


In this tutorial, we’ll show how to deploy an application from our Bootstrap a Simple Application using Spring Boot tutorial to App Engine on Google Cloud Platform.

在本教程中,我们将展示如何将使用Spring Boot启动一个简单的应用程序教程中的应用程序部署到Google云平台的App Engine

As part of this we’ll:


  • Configure Google Cloud Platform Console and SDK
  • Use Cloud SQL to create a MySQL instance
  • Configure the application for Spring Cloud GCP
  • Deploy the application to App Engine and test it

2. Google Cloud Platform Configuration


We can use the GCP Console to get our local environment ready for GCP. We can find the installation process on the official website.


Let’s create a project on GCP using the GCP Console:


gcloud init

Next, let’s configure the project name:


gcloud config set project baeldung-spring-boot-bootstrap

Then we’ll install the App Engine support and create an App Engine instance:

然后我们将安装App Engine支持并创建一个App Engine实例。

gcloud components install app-engine-java
gcloud app create

Our application will need to connect to a MySQL database within the Cloud SQL environment. As Cloud SQL doesn’t provide a free tier we’ll have to enable billing on the GCP account.

我们的应用程序将需要连接到Cloud SQL环境中的MySQL数据库。由于Cloud SQL不提供免费层,我们必须在GCP账户上启用计费

We can check available tiers easily:


gcloud sql tiers list

Before continuing, we should use the GCP Website to enable the Cloud SQL Admin API.

在继续之前,我们应该使用GCP网站来启用Cloud SQL Admin API

Now we can create a MySQL instance and database in Cloud SQL using the Cloud Console or the SDK CLI. During this process, we’ll choose the region and provide an instance name and database name. It’s important that the app and the database instance are in the same region.

现在我们可以使用云控制台或SDK CLI在Cloud SQL中创建一个MySQL实例和数据库。在此过程中,我们将选择区域并提供实例名称和数据库名称。重要的是,应用程序和数据库实例应在同一地区。

Since we’re going to deploy the app to europe-west2, let’s do the same for the instance:


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

3. Spring Cloud GCP Dependencies

3.Spring Cloud GCP的依赖性

Our application will need dependencies from the Spring Cloud GCP project for the cloud-native APIs. For this, let’s use a Maven profile named cloud-gcp:

我们的应用程序将需要依赖Spring Cloud GCP项目的云原生API。为此,让我们使用一个名为cloud-gcp的Maven配置文件。


Then we add the App Engine Maven plugin:

然后我们添加App Engine Maven插件。


4. Application Configuration


Now, let’s define the configuration that allows the application to use the cloud-native resources like the database.


Spring Cloud GCP uses to determine the application name:

Spring Cloud GCP使用spring-cloud-bootstrap.properties来确定应用程序名称。

We’ll use a Spring Profile named gcp for this deployment and we’ll need to configure the database connection. Therefore we create src/main/resources/

我们将使用一个名为gcp的Spring Profile进行部署,我们需要配置数据库连接。因此我们创建src/main/resources/\

5. Deployment


The Google App Engine provides two Java environments:


  • the Standard environment provides Jetty and JDK8 and the Flexible environment provides just JDK8 and
  • the Flexible environment is the best option for Spring Boot applications.

We require the gcp and mysql Spring profiles to be active, so we provide the SPRING_PROFILES_ACTIVE environmental variable to the application by adding it to the deployment configuration in src/main/appengine/app.yaml:


runtime: java
env: flex
  jdk: openjdk8
- url: /.*
  script: this field is required, but ignored
  instances: 1

Now, let’s build and deploy the application using the appengine maven plugin:

现在,让我们使用appengine maven插件构建和部署该应用程序

mvn clean package appengine:deploy -P cloud-gcp

After deployment we can view or tail log files:


# view
gcloud app logs read

# tail
gcloud app logs tail

Now, let’s verify that our application is working by adding a book:


http POST \
        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. Scaling the Application


The default scaling in App Engine is automatic.

App Engine中的默认缩放比例是自动的。

It may be better to start with manual scaling until we understand the runtime behavior, and the associated budgets and costs involved. We can assign resources to the application and configure automatic scaling in app.yaml:


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

7. Conclusion


In this tutorial, we:


  • Configured Google Cloud Platform and the App Engine
  • Created a MySQL instance with Cloud SQL
  • Configured Spring Cloud GCP for using MySQL
  • Deployed our configured Spring Boot application, and
  • Tested and scaled the application

We can always refer to Google’s extensive App Engine documentation for further details.

我们可以随时参考Google广泛的App Engine文档以了解更多细节。

The complete source code of our examples here is, as always, over on GitHub.