1. Overview
1.概述
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
2.谷歌云平台配置
We can use the GCP Console to get our local environment ready for GCP. We can find the installation process on the official website.
我们可以使用GCP控制台来为GCP准备好本地环境。我们可以在官方网站上找到安装过程。
Let’s create a project on GCP using the GCP Console:
让我们使用GCP控制台在GCP上创建一个项目。
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:
既然我们要把应用程序部署到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 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配置文件。
<profile>
<id>cloud-gcp</id>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
</dependencies>
Then we add the App Engine Maven plugin:
然后我们添加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. Application Configuration
4.应用配置
Now, let’s define the configuration that allows the application to use the cloud-native resources like the database.
现在,让我们定义配置,允许应用程序使用数据库等云原生资源。
Spring Cloud GCP uses spring-cloud-bootstrap.properties to determine the application name:
Spring Cloud GCP使用spring-cloud-bootstrap.properties来确定应用程序名称。
spring.cloud.appId=baeldung-spring-boot-bootstrap
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/application-gcp.properties:
我们将使用一个名为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. Deployment
5.部署
The Google App Engine provides two Java environments:
谷歌应用引擎提供了两种Java环境。
- 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:
我们要求gcp和mysql的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
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 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. Scaling the Application
6.扩展应用程序
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:
在我们了解运行时的行为,以及相关的预算和成本之前,从手动扩展开始可能会更好。我们可以在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. Conclusion
7.结论
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.
我们这里的例子的完整源代码一如既往地在GitHub上。