在Google App Engine中部署Spring boot gradle app

Syl*_*Das 7 java google-app-engine gradle spring-boot gradle-plugin

我搜索了使用Gradle部署Spring启动应用程序的教程.我没有找到任何解释这个过程的资源.

任何人都可以指导我的过程吗?

当我的机器在本地运行时,我的项目就像一个魅力.但我想在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')
}
Run Code Online (Sandbox Code Playgroud)

Syl*_*Das -3

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

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

请参阅Google App Engine 标准 - 文档以获取更详细的说明。请参阅使用 Cloud SQL for MySQL了解如何使用 mysql

设置

  • 下载并初始化Cloud SDK

    gcloud init

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

    gcloud app create

梅文

本地运行

mvn appengine:run

使用访问:http://localhost:8080/

部署

mvn appengine:deploy

要使用访问: https: //YOUR-PROJECT-ID.appspot.com

测试

mvn verify

当您添加/修改源代码 ( ) 时, 向 ( )src/main/java/...添加单元测试src/main/test/...非常有用。以下资源非常有用:

有关更多信息,请参阅 Java App Engine文档。

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

使用 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);
  }
}
Run Code Online (Sandbox Code Playgroud)

删除 Tomcat 启动器

Google App Engine Standard 将您部署WAR到 Jetty 服务器中。Spring Boot 的 starter 默认包含 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>
Run Code Online (Sandbox Code Playgroud)

不要包含 Jetty 依赖项。但您必须包含 Servlet API 依赖项:

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

添加 App Engine 标准插件

在 中pom.xml,添加 App Engine Standard 插件:

<plugin>
  <groupId>com.google.cloud.tools</groupId>
  <artifactId>appengine-maven-plugin</artifactId>
  <version>1.3.1</version>
</plugin>
Run Code Online (Sandbox Code Playgroud)

该插件用于运行本地开发服务器以及将应用程序部署到 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>
Run Code Online (Sandbox Code Playgroud)

在 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>
Run Code Online (Sandbox Code Playgroud)

内存不足错误

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

  1. 在 src/main/resources 内,添加一个logging.properties文件:
.level = INFO
Run Code Online (Sandbox Code Playgroud)
  1. 在 src/main/webapp/WEB-INF/appengine-web.xml 内,添加指向新的logging.properties 文件的配置。
.level = INFO
Run Code Online (Sandbox Code Playgroud)

  • 请求 Gradle 并获取 Maven。好的。 (11认同)