1概要
このチュートリアルでは、https://www.baeldung.com/spring-boot-start[Spring Bootを使用して単純なアプリケーションをブートストラップする]チュートリアルからhttps://cloud.google.co.jpまでアプリケーションをデプロイする方法を示します。 Google Cloud Platformの/appengine/[App Engine]。
その一環として、
-
Google Cloud Platform ConsoleとSDKを設定する
-
Cloud SQLを使用してMySQLインスタンスを作成する
-
Spring Cloud GCP用にアプリケーションを設定する
-
アプリケーションをApp Engineにデプロイしてテストする
2 Google Cloud Platformの設定
GCPコンソールを使用して、ローカル環境をGCPに対応させることができます。
installation process は、公式Webサイトにあります。
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アカウントでhttps://cloud.google.com/billing/docs/how-to/modify-project[billing]を有効にする必要があります。
利用可能な層を簡単に確認できます。
gcloud sql tiers list
続行する前に、GCP Webサイトを使用してhttps://console.cloud.google.com/flows/enableapi?apiid=sqladmin[Cloud SQL Admin API]を有効にする必要があります。
これで、Cloud ConsoleまたはSDK CLIを使用して、https://console.cloud.google.com/sql/instances[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の依存関係
私たちのアプリケーションはhttps://cloud.spring.io/spring-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>
それから、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は2つのJava環境を提供します。
-
__標準環境はJettyとJDK 8を提供します。
環境はJDK8と ** Flexible環境は、Spring Bootに最適な選択肢です。
アプリケーション
gcp および mysql Springプロファイルをアクティブにする必要があるため、 src/main/appengine/app.yaml のデプロイメント構成に追加することによって SPRING PROFILES ACTIVE 環境変数をアプリケーションに提供します。
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"
次の出力が期待されます。
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インスタンスを作成しました
-
MySQLを使用するためのSpring Cloud GCPの設定
-
設定したSpring Bootアプリケーションをデプロイし、そして
-
アプリケーションのテストとスケーリング
詳細については、Googleの広範なhttps://cloud.google.com/appengine/docs/flexible/java/[App Engineのドキュメント]をいつでも参照できます。
ここにある私たちの例の完全なソースコードは、いつものように、https://github.com/eugenp/tutorials/tree/master/spring-boot-bootstrap[over the GitHub]です。