Spring Boot - Google Cloud Platform



Google Cloud Platform 提供雲計算服務,可以在雲環境中執行 Spring Boot 應用程式。在本章中,我們將瞭解如何在 GCP App Engine 平臺上部署 Spring Boot 應用程式。

首先,從 Spring Initializer 頁面 www.start.spring.io 下載 Gradle 構建的 Spring Boot 應用程式。觀察以下螢幕截圖。

Spring Initializer Page

現在,在 build.gradle 檔案中,新增 Google Cloud App Engine 外掛和 App Engine 類路徑依賴項。

build.gradle 檔案的程式碼如下所示:

buildscript {
   ext {
      springBootVersion = '3.3.4'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
      classpath 'com.google.cloud.tools:appengine-gradle-plugin:2.8.1'
   }
}

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 = 21

repositories {
   mavenCentral()
}
dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
   testCompile('org.springframework.boot:spring-boot-starter-test')
} 

現在,編寫一個簡單的 HTTP 端點,它返回字串“success”,如下所示:

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 控制檯,並點選頁面頂部的“啟用 Google Cloud Shell”。

Activate Google Cloud Shell

現在,使用 Google Cloud Shell 將您的原始檔和 Gradle 檔案移動到 Google Cloud 虛擬機器的 home 目錄中。

Moving to Home Directory Using Google Cloud Shell

現在,執行命令 gradle appengineDeploy,它將把您的應用程式部署到 Google Cloud App Engine。

注意 - GCP 應啟用計費,並且在將您的應用程式部署到 App Engine 之前,您應在 GCP 中建立 App Engine 平臺。

將您的應用程式部署到 GCP App Engine 平臺需要幾分鐘。

構建成功後,您可以在控制檯視窗中看到服務 URL。

現在,訪問服務 URL 並檢視輸出。

App Engine Development Success

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

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"
廣告