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

Self-paced environment setup

If you don't already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID.

Next, you'll need to enable billing in the Cloud Console in order to use Google Cloud resources.

Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see "cleanup" section at the end of this document).

New users of Google Cloud Platform are eligible for a $300 free trial.

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.

Run the following command in the cloud shell to confirm that you are authenticated:

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.2</version>
        <configuration>
          <version>1</version>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
</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">
  <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;

...

// Add imports
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 spring-boot: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
[INFO] Running -A ... -V 1 --oauth2 update ...
The following URL can be used to authenticate:
  https://accounts.google.com/o/oauth2/...
Attempting to open it in your browser now.
Unable to open browser. Please open the URL above and copy the resulting code.
[Open the previous URL and paste the code here, press ENTER]

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.