📅  最后修改于: 2020-11-11 05:59:07             🧑  作者: Mango
Google Cloud Platform提供了一种云计算服务,该服务在云环境中运行Spring Boot应用程序。在本章中,我们将了解如何在GCP应用引擎平台中部署Spring Boot应用程序。
首先,从Spring Initializer页面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.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端点,它返回String成功,如下所示:
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 cloud shell。
现在,通过使用Google Cloud Shell将源文件和Gradle文件移动到Google Cloud Machine的主目录中。
现在,执行命令gradle appengineDeploy,它将把您的应用程序部署到Google Cloud appengine中。
注意-GCP应该已启用计费,并且在将应用程序部署到appengine中之前,您应该在GCP中创建appengine平台。
将您的应用程序部署到GCP appengine平台中将花费几分钟。
构建成功后,您可以在控制台窗口中看到服务URL。
现在,点击服务URL并查看输出。
要将Google Cloud SQL连接到Spring Boot应用程序中,应将以下属性添加到application.properties文件中。
jdbc:mysql://google/?cloudSqlInstance = &socketFactory = com.google.cloud.sql.mysql.SocketFactory&user = &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