Spring Boot 简明教程

Spring Boot - Google Cloud Platform

Google Cloud Platform 提供云计算服务,可在云环境中运行 Spring Boot 应用程序。在本章中,我们将了解如何将 Spring Boot 应用程序部署到 GCP app engine 平台中。

Google Cloud Platform provides a cloud computing services that run the Spring Boot application in the cloud environment. In this chapter, we are going to see how to deploy the Spring Boot application in GCP app engine platform.

首先,从 Spring Initializer 页面 www.start.spring.io 下载 Gradle 构建的 Spring Boot 应用程序。观察以下屏幕截图。

First, download the Gradle build Spring Boot application from Spring Initializer page www.start.spring.io. Observe the following screenshot.

spring initializer page

现在,在 build.gradle 文件中,添加 Google Cloud Appengine 插件和 Appengine 类路径依赖项。

Now, in build.gradle file, add the Google Cloud appengine plugin and appengine classpath dependency.

build.gradle 文件的代码如下所示 −

The code for build.gradle file is given below −

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 端点,使其返回字符串成功,如下所示 −

Now, write a simple HTTP Endpoint and it returns the String success as shown −

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 文件,如下所示 −

Next, add the app.yml file under src/main/appengine directory as shown −

runtime: java
env: flex

handlers:
- url: /.*
   script: this field is required, but ignored

现在,请转到 Google Cloud 控制台,然后单击页面顶部的激活 Google Cloud Shell。

Now, go to the Google Cloud console and click the Activate Google cloud shell at the top of the page.

activate google cloud shell

现在,使用 Google Cloud Shell 将您的源文件和 Gradle 文件移入 Google Cloud 机器的主目录中。

Now, move your source files and Gradle file into home directory of your google cloud machine by using google cloud shell.

moving to home directory using google cloud shell

现在,执行命令 gradle appengineDeploy,它会将您的应用程序部署到 Google Cloud Appengine 中。

Now, execute the command gradle appengineDeploy and it will deploy your application into the Google Cloud appengine.

Note − GCP 应启用计费并且在将您的应用程序部署到 appengine 之前,您应在 GCP 中创建 appengine 平台。

Note − GCP should be billing enabled and before deploying your application into appengine, you should create appengine platform in GCP.

将您的应用程序部署到 GCP appengine 平台需要几分钟时间。

It will take few minutes to deploy your application into GCP appengine platform.

构建成功后,您可以在控制台窗口中看到服务 URL。

After build successful you can see the Service URL in console window.

spring initializer page

现在,点击服务 URL 并查看输出。

Now, hit the service URL and see the output.

app engine development success

Google Cloud SQL

要将 Google Cloud SQL 连接到您的 Spring Boot 应用程序,您应将以下属性添加到您的 application.properties 文件中。

To connect the Google Cloud SQL into your Spring Boot application, you should add the following properties into your application.properties file.

JDBC URL Format

jdbc:mysql://google/<DATABASE-NAME>?cloudSqlInstance = <GOOGLE_CLOUD_SQL_INSTANCE_NAME> &socketFactory = com.google.cloud.sql.mysql.SocketFactory&user = <USERNAME>&password = <PASSWORD>

Note − Spring Boot 应用程序和 Google Cloud SQL 应位于同一 GCP 项目中。

Note − The Spring Boot application and Google Cloud SQL should be in same GCP project.

应用程序属性文件如下。

The application.properties file is given below.

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 文件中。

YAML file users can add the below properties to your application.yml file.

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