SpringBootアプリケーションをGoogleAppEngineにデプロイする
1. 概要
このチュートリアルでは、 Bootstrap a Simple Application using Spring Boot Tutorial to App Engine on GoogleCloudPlatformにアプリケーションをデプロイする方法を示します。
この一環として、次のことを行います。
- Google CloudPlatformConsoleとSDKを構成する
- CloudSQLを使用してMySQLインスタンスを作成します
- SpringCloudGCP用にアプリケーションを構成します
- アプリケーションをAppEngineにデプロイしてテストします
2. GoogleCloudPlatformの構成
GCPコンソールを使用して、ローカル環境をGCPに対応させることができます。 インストールプロセスは公式ウェブサイトで見つけることができます。
GCPConsoleを使用してGCPでプロジェクトを作成しましょう。
gcloud init
次に、プロジェクト名を構成しましょう。
gcloud config set project baeldung-spring-boot-bootstrap
次に、App Engineサポートをインストールし、AppEngineインスタンスを作成します。
gcloud components install app-engine-java
gcloud app create
アプリケーションは、CloudSQL環境内のMySQLデータベースに接続する必要があります。 Cloud SQLは無料利用枠を提供していないため、GCPアカウントで請求を有効にする必要があります。
利用可能な階層を簡単に確認できます。
gcloud sql tiers list
続行する前に、GCPWebサイトを使用してCloud SQL AdminAPIを有効にする必要があります。
これで、CloudConsoleまたはSDKCLIを使用して、 CloudSQLで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. SpringCloudGCPの依存関係
このアプリケーションでは、クラウドネイティブAPIの Spring CloudGCPプロジェクトからの依存関係が必要になります。 このために、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>
次に、AppEngineMavenプラグインを追加します。
<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プロファイルを使用し、データベース接続を構成する必要があります。 したがって、 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は、次の2つのJava環境を提供します。
- Standard 環境はJettyとJDK8を提供し、Flexible環境はJDK8と
- 柔軟な環境は、SpringBootアプリケーションに最適なオプションです。
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
それでは、appenginemavenプラグインを使用してアプリケーションをビルドしてデプロイしましょう。
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"
次の出力が必要です。
HTTP/1.1 201
{
"author": "Iain M. Banks",
"id": 1,
"title": "The Player of Games"
}
6. アプリケーションのスケーリング
AppEngineのデフォルトのスケーリングは自動です。
実行時の動作、および関連する予算とコストを理解するまで、手動スケーリングから始める方がよい場合があります。 アプリケーションにリソースを割り当て、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. 結論
このチュートリアルでは、次のことを行います。
- 構成済みのGoogleCloudPlatformとAppEngine
- CloudSQLを使用してMySQLインスタンスを作成しました
- MySQLを使用するために構成されたSpringCloudGCP
- 構成済みのSpringBootアプリケーションをデプロイし、
- アプリケーションのテストとスケーリング
詳細については、Googleの広範なAppEngineドキュメントをいつでも参照できます。
ここでの例の完全なソースコードは、いつものように、GitHub上にあります。