Spring Boot-Google Cloud Platform
Google Cloud Platformは、クラウド環境でSpringBootアプリケーションを実行するクラウドコンピューティングサービスを提供します。この章では、GCPアプリエンジンプラットフォームにSpringBootアプリケーションをデプロイする方法を説明します。
まず、SpringInitializerページwww.start.spring.ioからGradleビルドSpringBootアプリケーションをダウンロードします。次のスクリーンショットを確認してください。
次に、build.gradleファイルに、Google Cloudappengineプラグインと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.tutorialspoint'
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.tutorialspoint.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コンソールに移動し、ページの上部にある[Activate Google CloudShell]をクリックします。
次に、Google Cloud Shellを使用して、ソースファイルとGradleファイルをGoogle CloudMachineのホームディレクトリに移動します。
ここで、コマンドgradle appengineDeployを実行すると、アプリケーションがGoogle Cloudappengineにデプロイされます。
Note − GCPで課金が有効になっている必要があり、アプリケーションをappengineにデプロイする前に、GCPでappengineプラットフォームを作成する必要があります。
アプリケーションをGCPappengineプラットフォームにデプロイするのに数分かかります。
ビルドが成功すると、コンソールウィンドウにサービスURLが表示されます。
次に、サービスURLを押して、出力を確認します。
Google Cloud SQL
Google CloudSQLをSpringBootアプリケーションに接続するには、次のプロパティをapplication.propertiesファイルに追加する必要があります。
JDBCURL形式
jdbc:mysql://google/<DATABASE-NAME>?cloudSqlInstance = <GOOGLE_CLOUD_SQL_INSTANCE_NAME> &socketFactory = com.google.cloud.sql.mysql.SocketFactory&user = <USERNAME>&password = <PASSWORD>
Note − SpringBootアプリケーションとGoogleCloudSQLは同じ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