简体   繁体   中英

Deploy Spring boot gradle app in Google App Engine

I have searched for the tutorial to deploy Spring boot application using Gradle . 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.

Thanks, in advance.

My build.gradle looks like this

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)

This sample demonstrates how to deploy a Spring Boot application on Google App Engine.

See the Google App Engine standard - documentation for more detailed instructions. See the Using Cloud SQL for MySQL for working with mysql

Setup

  • Download and initialize the Cloud SDK

    gcloud init

  • Create an App Engine app within the current Google Cloud Project

    gcloud app create

Maven

Running locally

mvn appengine:run

To use vist: http://localhost:8080/

Deploying

mvn appengine:deploy

To use vist: 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/... ). The following resources are quite useful:

For further information, consult the Java App Engine documentation.

Steps to convert a Spring Boot application for App Engine Standard

Use the WAR packaging

You must use WAR packaging to deploy into 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.

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:

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

Remove Tomcat Starter

Google App Engine Standard deploys your WAR into a Jetty server. Spring Boot's starter includes Tomcat by default. This will introduce conflicts. Exclude Tomcat dependencies:

<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. But you must include Servlet API dependency:

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

Add App Engine Standard Plugin

In the pom.xml , add the App Engine Standard plugin:

<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.

Add App Engine Configuration

Add a 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.

Exclude JUL to SLF4J Bridge

Spring Boot's default logging bridge conflicts with Jetty's logging system. To be able to capture the Spring Boot startup logs, you need to exclude org.slf4j:jul-to-slf4j dependency. 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:

<!-- 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. Please follow these instructions to work around this issue:

  1. Inside src/main/resources, adding a logging.properties file with:
.level = INFO
  1. Inside src/main/webapp/WEB-INF/appengine-web.xml, add a config that points to the new logging.properties file.
<system-properties>
  <property name="java.util.logging.config.file" value="WEB-INF/classes/logging.properties"/>
</system-properties>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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