简体   繁体   English

在 Google App Engine 中部署 Spring boot gradle 应用

[英]Deploy Spring boot gradle app in Google App Engine

I have searched for the tutorial to deploy Spring boot application using Gradle .我已经搜索了使用Gradle部署Spring boot 应用程序教程 I failed to find any resource that explains the process to do so.我找不到任何资源来解释这样做的过程。

Can anyone guide me the process?谁能指导我这个过程?

My project works like a charm when its run locally on my machine.当我的项目在我的机器上本地运行时,它的工作就像一个魅力。 But I would like to deploy on the Google app engine's Flexible Java Environment.但我想部署在 Google 应用引擎的灵活 Java 环境上。

Thanks, in advance.提前致谢。

My build.gradle looks like this我的 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')
}

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

This sample demonstrates how to deploy a Spring Boot application on Google App Engine.此示例演示了如何在 Google App Engine 上部署 Spring Boot 应用程序。

See the Google App Engine standard - documentation for more detailed instructions.请参阅Google App Engine 标准 - 文档以获取更详细的说明。 See the Using Cloud SQL for MySQL for working with mysql请参阅使用 Cloud SQL for MySQL以使用 mysql

Setup设置

  • Download and initialize the Cloud SDK下载并初始化Cloud SDK

    gcloud init

  • Create an App Engine app within the current Google Cloud Project在当前的 Google Cloud 项目中创建一个 App Engine 应用

    gcloud app create

Maven行家

Running locally在本地运行

mvn appengine:run

To use vist: http://localhost:8080/使用访问: http://localhost:8080/

Deploying部署中

mvn appengine:deploy

To use vist: https://YOUR-PROJECT-ID.appspot.com使用访问: https ://YOUR-PROJECT-ID.appspot.com

Testing测试

mvn verify

As you add / modify the source code ( src/main/java/... ) it's very useful to add unit testing to ( src/main/test/... ).当您添加/修改源代码 ( src/main/java/... ) 时,将单元测试添加到 ( src/main/test/... ) 是非常有用的。 The following resources are quite useful:以下资源非常有用:

For further information, consult the Java App Engine documentation.有关详细信息,请参阅Java App Engine文档。

Steps to convert a Spring Boot application for App Engine Standard为 App Engine Standard 转换 Spring Boot 应用程序的步骤

Use the WAR packaging使用 WAR 包装

You must use WAR packaging to deploy into Google App Engine Standard.您必须使用 WAR 打包来部署到 Google App Engine Standard。

If you generate a Spring Boot project from start.spring.io , make sure you switch to the full version view of the initializer site, and select WAR packaging.如果从start.spring.io生成 Spring Boot 项目,请确保切换到初始化程序站点的完整版本视图,并选择WAR打包。

If you have an existing JAR packaging project, you can convert it into a WAR project by: 1. In pom.xml , change <packaging>jar</packaging> to <packaging>war</packaging> 1. Create a new SpringBootServletInitializer implementation:如果您已有一个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);
  }
}

Remove Tomcat Starter删除 Tomcat 启动器

Google App Engine Standard deploys your WAR into a Jetty server. Google App Engine Standard 将您的WAR部署到 Jetty 服务器中。 Spring Boot's starter includes Tomcat by default. Spring Boot 的 starter 默认包含 Tomcat。 This will introduce conflicts.这将引入冲突。 Exclude Tomcat dependencies:排除 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>

Do not include the Jetty dependencies.不要包括 Jetty 依赖项。 But you must include Servlet API dependency:但是您必须包括 Servlet API 依赖项:

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

Add App Engine Standard Plugin添加 App Engine 标准插件

In the pom.xml , add the App Engine Standard plugin:pom.xml中,添加 App Engine 标准插件:

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

This plugin is used to run local development server as well as deploying the application into Google App Engine.此插件用于运行本地开发服务器以及将应用程序部署到 Google App Engine 中。

Add App Engine Configuration添加应用引擎配置

Add a src/main/webapp/WEB-INF/appengine-web.xml :添加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>

This configure is required for applications running in Google App Engine.在 Google App Engine 中运行的应用程序需要此配置。

Exclude JUL to SLF4J Bridge排除 JUL 到 SLF4J 桥

Spring Boot's default logging bridge conflicts with Jetty's logging system. Spring Boot 的默认日志记录桥与 Jetty 的日志系统冲突。 To be able to capture the Spring Boot startup logs, you need to exclude org.slf4j:jul-to-slf4j dependency.为了能够捕获 Spring Boot 启动日志,您需要排除org.slf4j:jul-to-slf4j依赖项。 The easiest way to do this is to set the dependency scope to provided , so that it won't be included in the WAR file:最简单的方法是将依赖范围设置为provided ,这样它就不会包含在WAR文件中:

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

Out of memory errors内存不足错误

With Spring Boot >= 1.5.6, you may run into out of memory errors on startup.使用 Spring Boot >= 1.5.6,您可能会在启动时遇到内存不足错误。 Please follow these instructions to work around this issue:请按照以下说明解决此问题:

  1. Inside src/main/resources, adding a logging.properties file with:在 src/main/resources 中,添加一个 logging.properties 文件:
.level = INFO
  1. Inside src/main/webapp/WEB-INF/appengine-web.xml, add a config that points to the new logging.properties file.在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM