Google App Engine applications are easy to create, easy to maintain, and easy to scale as your traffic and data storage needs change. With App Engine, there are no servers to maintain. You simply upload your application and it's ready to go.

App Engine applications automatically scale based on incoming traffic. load balancing, microservices, authorization, SQL and NoSQL databases, Memcache, traffic splitting, logging, search, versioning, roll out and roll backs, and security scanning are all supported natively and are highly customizable.

App Engine's environments, the Standard Environment and the Flexible environment, support a host of programming languages, including Java, Python, PHP, NodeJS, Go, etc.. The two environments give users maximum flexibility in how their application behaves since each environment has certain strengths. Read The App Engine Environments for more information.

In this codelab, you will learn how to deploy a Spring Boot application into App Engine Standard environment on Google Cloud Platform via the web. This environment will scale down to 0 instances when no one is using it, and automatically scale up!

What you'll learn

What you'll need

How will you use use this tutorial?

Read it through only Read it and complete the exercises

How would you rate your experience with building HTML/CSS web apps?

Novice Intermediate Proficient

How would you rate your experience with using Google Cloud Platform services?

Novice Intermediate Proficient

Codelab-at-a-conference setup

If you see a "request account button" at the top of the main Codelabs window, click it to obtain a temporary account. Otherwise ask one of the staff for a coupon with username/password.

These temporary accounts have existing projects that are set up with billing so that there are no costs associated for you with running this codelab.

Note that all these accounts will be disabled soon after the codelab is over.

Use these credentials to log into the machine or to open a new Google Cloud Console window https://console.cloud.google.com/. Accept the new account Terms of Service and any updates to Terms of Service.

Here's what you should see once logged in:

When presented with this console landing page, please select the only project available. Alternatively, from the console home page, click on "Select a Project" :

Google Cloud Shell

While Google Cloud and Kubernetes can be operated remotely from your laptop, in this codelab we will be using Google Cloud Shell, a command line environment running in the Cloud.

Activate Google Cloud Shell

From the GCP Console click the Cloud Shell icon on the top right toolbar:

Then click "Start Cloud Shell":

It should only take a few moments to provision and connect to the environment:

This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this lab can be done with simply a browser or your Google Chromebook.

Once connected to the cloud shell, you should see that you are already authenticated and that the project is already set to your PROJECT_ID:

gcloud auth list

Command output

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)
gcloud config list project

Command output

[core]
project = <PROJECT_ID>

If it is not, you can set it with this command:

gcloud config set project <PROJECT_ID>

Command output

Updated property [core/project].

After Cloud Shell launches, you can use the command line to generate a new Spring Boot application with Spring initializr:

$ curl https://start.spring.io/starter.tgz -d packaging=war \
  -d dependencies=web -d baseDir=gae-standard-example | tar -xzvf -
$ cd gae-standard-example

There are multiple ways to deploy a Java server application - either by using a Maven or Gradle plugin, or by deploying the war package directory. In the code lab, we'll use Maven to deploy the application.

Add App Engine Plugin

Update the pom.xml to include a Google Cloud Platform plugin that simplifies the deployment process. You can use vim,nano,or emacs to edit the file.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>appengine-maven-plugin</artifactId>
        <version>1.3.1</version>
      </plugin>
      ...
    </plugins>
  </build>
</project>

Remove Tomcat Starter

App Engine Standard uses Jetty web server underneath. Spring Boot includes Tomcat in the WAR package that will conflict with Jetty. Exclude Tomcat from the Spring Boot Starter, and add back Servlet API as a provided dependency:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
  ...
  <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>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
</project>

Exclude JUL to SLF4J Bridge

Spring Boot also includes a JUL to SLF4J bridge that interferes with App Engine's log handler that's provided through Jetty server. To get proper log entries, you must exclude the jul-to-slf4j artifact by marking it as provided.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
  ...
  <dependencies>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jul-to-slf4j</artifactId>
      <scope>provided</scope>
    </dependency>
  </dependencies>
</project>

To deploy the application into App Engine standard, you must add create a new src/main/webapp/WEB-INF/appengine-web.xml descriptor file:

$ mkdir -p src/main/webapp/WEB-INF/
$ touch src/main/webapp/WEB-INF/appengine-web.xml

Edit src/main/webapp/WEB-INF/appengine-web.xml file and add the following content:

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>

Add a new controller that returns "hello" in DemoApplication.java.

src/main/java/com/example/demo/DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;

@SpringBootApplication
@RestController
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

  @GetMapping("/")
  public String hello() {
    return "hello world!";
  }
}

You can start the Spring Boot application normally with the Spring Boot plugin:

$ ./mvnw -DskipTests appengine:run

Once the application started, click on the Web Preview icon in the Cloud Shell toolbar and choose preview on port 8080.

A tab in your browser opens and connects to the server you just started.

First, initialize the Project to be able to run App Engine applications. We'll initialize the project to run in the US Central region:

$ gcloud app create --region us-central
You are creating an app for project [...].
WARNING: Creating an App Engine application for a project is irreversible and the region
cannot be changed. More information about regions is at
https://cloud.google.com/appengine/docs/locations

Then, deploy your application into App Engine environment, run mvn appengine:deploy:

$ ./mvnw -DskipTests appengine:deploy

After the application is deployed, you can visit it by opening the URL http://<project-id>.appspot.com in your web browser.

In this step, you set up a simple Spring Boot application and ran and deployed your application on App Engine.

You learned how to write your first App Engine web application!

Learn More

License

This work is licensed under a Creative Commons Attribution 2.0 Generic License.