【问题标题】:Deploy Spring boot gradle app in Google App Engine在 Google App Engine 中部署 Spring Boot gradle 应用
【发布时间】:2019-02-02 04:14:27
【问题描述】:

我已经搜索了 tutorial 以使用 Gradle 部署 Spring boot 应用程序。我找不到任何资源来解释这样做的过程。

谁能指导我这个过程?

当它在我的机器上本地运行时,我的项目就像一个魅力。但我想部署在 Google 应用引擎的灵活 Java 环境上。

谢谢,提前。

我的 build.gradle 看起来像这样

buildscript {
    ext {
        springBootVersion = '2.0.4.RELEASE'
        jwtVersion = '3.4.0'
        appEngineVersion = '1.9.56'
        appEngineGradleToolsVersion = '1.3.4'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile("org.springframework.boot:spring-boot-starter-security")

    // JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'

    // Use MySQL Connector-J
    compile 'mysql:mysql-connector-java'

    implementation "com.auth0:java-jwt:${jwtVersion}"

    runtime('org.springframework.boot:spring-boot-devtools')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

【问题讨论】:

标签: java spring-boot google-app-engine gradle gradle-plugin


【解决方案1】:

适用于 Google App Engine 标准 (Java 8) 的 Spring Boot

此示例演示如何在 Google App Engine 上部署 Spring Boot 应用程序。

有关详细说明,请参阅Google App Engine standard - documentation。 请参阅Using Cloud SQL for MySQL 了解如何使用 mysql

设置

  • 下载并初始化Cloud SDK

    gcloud init

  • 在当前的 Google Cloud 项目中创建 App Engine 应用

    gcloud app create

Maven

在本地运行

mvn appengine:run

使用vist:http://localhost:8080/

部署

mvn appengine:deploy

使用vist:https://YOUR-PROJECT-ID.appspot.com

测试

mvn verify

当您添加/修改源代码 (src/main/java/...) 时,添加 unit testing 非常有用 到(src/main/test/...)。以下资源非常有用:

如需更多信息,请咨询 Java App Engine 文档。

将 Spring Boot 应用程序转换为 App Engine 标准的步骤

使用 WAR 包装

您必须使用 WAR 打包才能部署到 Google App Engine Standard。

如果你从start.spring.io生成一个Spring Boot项目, 确保您切换到初始化站点的完整版本视图,然后选择WAR 包装。

如果您有一个现有的 JAR 打包项目,您可以通过以下方式将其转换为 WAR 项目: 1. 在pom.xml 中,将<packaging>jar</packaging> 更改为<packaging>war</packaging> 1.创建一个新的SpringBootServletInitializer实现:

public class ServletInitializer extends SpringBootServletInitializer {
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  return application.sources(YourApplication.class);
  }
}

删除 Tomcat 启动器

Google App Engine Standard 将您的 WAR 部署到 Jetty 服务器中。 Spring Boot 的启动器 默认情况下包括 Tomcat。这会引入冲突。排除 Tomcat 依赖项:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
</dependency>

不包括 Jetty 依赖项。但是你必须包含 Servlet API 依赖:

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>

添加 App Engine 标准插件

pom.xml 中,添加 App Engine 标准插件:

<plugin>
  <groupId>com.google.cloud.tools</groupId>
  <artifactId>appengine-maven-plugin</artifactId>
  <version>1.3.1</version>
</plugin>

此插件用于运行本地开发服务器以及部署应用程序 进入 Google App Engine。

添加 App Engine 配置

添加src/main/webapp/WEB-INF/appengine-web.xml

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <version>1</version>
  <threadsafe>true</threadsafe>
  <runtime>java8</runtime>
</appengine-web-app>

在 Google App Engine 中运行的应用程序需要此配置。

排除 JUL 到 SLF4J 桥

Spring Boot 的默认日志桥与 Jetty 的日志系统冲突。 为了能够捕获 Spring Boot 启动日志,您需要排除 org.slf4j:jul-to-slf4j 依赖。最简单的方法是 将依赖范围设置为provided,这样它就不会被包含在 WAR 文件:

<!-- Exclude any jul-to-slf4j -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jul-to-slf4j</artifactId>
  <scope>provided</scope>
</dependency>

内存不足错误

使用 Spring Boot >= 1.5.6,您可能会在启动时遇到内存不足错误。 请按照以下说明解决此问题:

  1. 在 src/main/resources 中,添加一个 logging.properties 文件,其中包含:
.level = INFO
  1. 在 src/main/webapp/WEB-INF/appengine-web.xml 中,添加指向新 logging.properties 文件的配置。
<system-properties>
  <property name="java.util.logging.config.file" value="WEB-INF/classes/logging.properties"/>
</system-properties>

【讨论】:

最近更新 更多