在Google App Engine中部署Spring Boot Gradle应用程序

时间:2018-08-27 15:22:45

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

我已经搜索了教程,以使用 Gradle 部署 Spring Boot应用程序。我找不到任何解释此过程的资源。

有人可以指导我这个过程吗?

当我的项目在我的计算机上本地运行时,它的工作原理就像一个魅力。但我想在Google应用引擎的Flexible Java Environment上进行部署。

  

谢谢。

我的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')
}

1 个答案:

答案 0 :(得分:1)

Google App Engine标准版(Java 8)的Spring Boot

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

有关更多详细说明,请参见Google App Engine standard - documentation。 有关使用mysql的信息,请参见Using Cloud SQL for MySQL

设置

  • 下载并初始化Cloud SDK

    gcloud init

  • 在当前的Google Cloud Project中创建一个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文档。

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

使用WAR包装

您必须使用WAR打包程序才能部署到Google App Engine标准中。

如果您从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 Starter

Google App Engine标准将您的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>