
- Spring Boot 教程
- Spring Boot - 首页
- Spring Boot - 简介
- Spring Boot - 快速入门
- Spring Boot - 引导
- Spring Tool Suite
- Spring Boot - Tomcat 部署
- Spring Boot - 构建系统
- Spring Boot - 代码结构
- Spring Bean & 依赖注入
- Spring Boot - 运行器
- Spring Boot - 启动器
- Spring Boot - 应用程序属性
- Spring Boot - 配置
- Spring Boot - 注解
- Spring Boot - 日志记录
- 构建 RESTful Web 服务
- Spring Boot - 异常处理
- Spring Boot - 拦截器
- Spring Boot - Servlet 过滤器
- Spring Boot - Tomcat 端口号
- Spring Boot - Rest 模板
- Spring Boot - 文件处理
- Spring Boot - 服务组件
- Spring Boot - Thymeleaf
- 使用 RESTful Web 服务
- Spring Boot - CORS 支持
- Spring Boot - 国际化
- Spring Boot - 调度
- Spring Boot - 启用 HTTPS
- Spring Boot - Eureka 服务器
- 使用 Eureka 进行服务注册
- 网关代理服务器和路由
- Spring Cloud 配置服务器
- Spring Cloud 配置客户端
- Spring Boot - 执行器
- Spring Boot - Admin 服务器
- Spring Boot - Admin 客户端
- Spring Boot - 启用 Swagger2
- Spring Boot - 使用 SpringDoc OpenAPI
- Spring Boot - 创建 Docker 镜像
- 跟踪微服务日志
- Spring Boot - Flyway 数据库
- Spring Boot - 发送电子邮件
- Spring Boot - Hystrix
- Spring Boot - Web Socket
- Spring Boot - 批处理服务
- Spring Boot - Apache Kafka
- Spring Boot - Twilio
- Spring Boot - 单元测试用例
- Rest Controller 单元测试
- Spring Boot - 数据库处理
- 保护 Web 应用程序
- Spring Boot - 使用 JWT 的 OAuth2
- Spring Boot - Google Cloud Platform
- Spring Boot - Google OAuth2 登录
- Spring Boot 资源
- Spring Boot - 快速指南
- Spring Boot - 有用资源
- Spring Boot - 讨论
Spring Boot - Google Cloud Platform
Google Cloud Platform 提供云计算服务,可以在云环境中运行 Spring Boot 应用程序。在本章中,我们将了解如何在 GCP App Engine 平台上部署 Spring Boot 应用程序。
首先,从 Spring Initializer 页面 www.start.spring.io 下载 Gradle 构建的 Spring Boot 应用程序。观察以下屏幕截图。

现在,在 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”。

现在,使用 Google Cloud Shell 将您的源文件和 Gradle 文件移动到 Google Cloud 虚拟机的 home 目录中。

现在,执行命令 gradle appengineDeploy,它将把您的应用程序部署到 Google Cloud App Engine。
注意 - GCP 应启用计费,并且在将您的应用程序部署到 App Engine 之前,您应在 GCP 中创建 App Engine 平台。
将您的应用程序部署到 GCP App Engine 平台需要几分钟。
构建成功后,您可以在控制台窗口中看到服务 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
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"