Spring-boot-google-cloud-platform
Spring Boot-Google Cloud Platform
Google Cloud Platformは、クラウド環境でSpring Bootアプリケーションを実行するクラウドコンピューティングサービスを提供します。 この章では、GCPアプリエンジンプラットフォームにSpring Bootアプリケーションを展開する方法を説明します。
まず、Spring Initializerページhttps://start.spring.io/[www.start.spring.io]からGradleビルドSpring Bootアプリケーションをダウンロードします。 次のスクリーンショットを確認してください。
次に、build.gradleファイルで、Google Cloud appengineプラグインとappengineクラスパスの依存関係を追加します。
build.gradleファイルのコードは次のとおりです-
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'com.google.cloud.tools:appengine-gradle-plugin:1.3.3'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'com.google.cloud.tools.appengine'
group = 'com.finddevguides'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
今、簡単なHTTPエンドポイントを書くと、それは示されているように文字列の成功を返します-
package com.finddevguides.appenginedemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class AppengineDemoApplication {
public static void main(String[] args) {
SpringApplication.run(AppengineDemoApplication.class, args);
}
@RequestMapping(value = "/")
public String success() {
return "APP Engine deployment success";
}
}
次に、示されているようにsrc/main/appengineディレクトリの下にapp.ymlファイルを追加します-
runtime: java
env: flex
handlers:
- url:/.*
script: this field is required, but ignored
次に、Google Cloudコンソールに移動し、ページ上部の[Googleクラウドのアクティブ化]シェルをクリックします。
次に、Googleクラウドシェルを使用して、ソースファイルとGradleファイルをGoogleクラウドマシンのホームディレクトリに移動します。
Google Cloud Shellを使用してホームディレクトリに移動
ここで、コマンドgradle appengineDeployを実行すると、アプリケーションがGoogle Cloud appengineにデプロイされます。
注-GCPは課金を有効にして、アプリケーションをappengineにデプロイする前に、GCPでappengineプラットフォームを作成する必要があります。
アプリケーションをGCP appengineプラットフォームにデプロイするには数分かかります。
ビルドが成功すると、コンソールウィンドウにサービスURLが表示されます。
ここで、サービスURLにアクセスして、出力を確認します。
Google Cloud SQL
Google Cloud SQLをSpring Bootアプリケーションに接続するには、application.propertiesファイルに次のプロパティを追加する必要があります。
JDBC URL形式
jdbc:mysql://google/<DATABASE-NAME>?cloudSqlInstance = <GOOGLE_CLOUD_SQL_INSTANCE_NAME> &socketFactory = com.google.cloud.sql.mysql.SocketFactory&user = <USERNAME>&password = <PASSWORD>
注-Spring BootアプリケーションとGoogle Cloud SQLは同じGCPプロジェクトにある必要があります。
application.propertiesファイルを以下に示します。
spring.dbProductService.driverClassName = com.mysql.jdbc.Driver
spring.dbProductService.url = jdbc:mysql://google/PRODUCTSERVICE?cloudSqlInstance = springboot-gcp-cloudsql:asia-northeast1:springboot-gcp-cloudsql-instance&socketFactory = com.google.cloud.sql.mysql.SocketFactory&user = root&password = rootspring.dbProductService.username = root
spring.dbProductService.password = root
spring.dbProductService.testOnBorrow = true
spring.dbProductService.testWhileIdle = true
spring.dbProductService.timeBetweenEvictionRunsMillis = 60000
spring.dbProductService.minEvictableIdleTimeMillis = 30000
spring.dbProductService.validationQuery = SELECT 1
spring.dbProductService.max-active = 15
spring.dbProductService.max-idle = 10
spring.dbProductService.max-wait = 8000
YAMLファイルユーザーは、application.ymlファイルに以下のプロパティを追加できます。
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: "jdbc:mysql://google/PRODUCTSERVICE?cloudSqlInstance=springboot-gcp-cloudsql:asia-northeast1:springboot-gcp-cloudsql-instance&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=root&password=root"
password: "root"
username: "root"
testOnBorrow: true
testWhileIdle: true
validationQuery: SELECT 1
max-active: 15
max-idle: 10
max-wait: 8000