Deploy a Spring Boot app to App Engine standard environment

1. Before you begin

App Engine apps 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 app and it's ready to go.

App Engine apps automatically scale based on incoming traffic. Load balancing, microservices, authorization, SQL and NoSQL databases, memory caching, traffic splitting, logging, search, versioning, rollouts and rollbacks, and security scanning are all supported natively and are highly customizable.

App Engine standard environment and App Engine flexible environment support a host of programming languages, including Java, Python, PHP, NodeJS, and Go. The two environments give developers maximum flexibility with how their app behaves. Each environment has certain strengths. For more information, see Choosing an App Engine environment.

You'll learn to deploy a Spring Boot app to App Engine standard environment. The standard environment scales down to zero instances when no one is using it and automatically scales up!

Prerequisites

  • Familiarity with Java programming language and tools
  • Knowledge of standard Linux text editors, such as Vim, Emacs, and nano

What you'll do

  • How to create a Spring Boot Java app on App Engine

What you'll need

2. Setup and requirements

Self-paced environment setup

  1. Sign-in to the Google Cloud Console and create a new project or reuse an existing one. If you don't already have a Gmail or Google Workspace account, you must create one.

b35bf95b8bf3d5d8.png

a99b7ace416376c4.png

bd84a6d3004737c5.png

  • The Project name is the display name for this project's participants. It is a character string not used by Google APIs. You can always update it.
  • The Project ID is unique across all Google Cloud projects and is immutable (cannot be changed after it has been set). The Cloud Console auto-generates a unique string; usually you don't care what it is. In most codelabs, you'll need to reference your Project ID (typically identified as PROJECT_ID). If you don't like the generated ID, you might generate another random one. Alternatively, you can try your own, and see if it's available. It can't be changed after this step and remains for the duration of the project.
  • For your information, there is a third value, a Project Number, which some APIs use. Learn more about all three of these values in the documentation.
  1. Next, you'll need to enable billing in the Cloud Console to use Cloud resources/APIs. Running through this codelab won't cost much, if anything at all. To shut down resources to avoid incurring billing beyond this tutorial, you can delete the resources you created or delete the project. New Google Cloud users are eligible for the $300 USD Free Trial program.

Cloud Shell

You'll use Cloud Shell, a command-line environment running in Google Cloud.

Activate Cloud Shell

  1. From the Cloud Console, click Activate Cloud Shell 853e55310c205094.png.

55efc1aaa7a4d3ad.png

If this is your first time starting Cloud Shell, you're presented with an intermediate screen describing what it is. If you were presented with an intermediate screen, click Continue.

9c92662c6a846a5c.png

It should only take a few moments to provision and connect to Cloud Shell.

9f0e51b578fecce5.png

This virtual machine is loaded with all the development tools needed. It offers a persistent 5 GB home directory and runs in Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this codelab can be done with a browser.

Once connected to Cloud Shell, you should see that you are authenticated and that the project is set to your project ID.

  1. Run the following command in Cloud Shell to confirm that you are authenticated:
gcloud auth list

Command output

 Credentialed Accounts
ACTIVE  ACCOUNT
*       <my_account>@<my_domain.com>

To set the active account, run:
    $ gcloud config set account `ACCOUNT`
  1. Run the following command in Cloud Shell to confirm that the gcloud command knows about your project:
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].

3. Create a new Spring Boot web app

After Cloud Shell launches, you can use the command line to generate a new Spring Boot app with Spring Initializr.

$ curl https://start.spring.io/starter.tgz \
  -d bootVersion=3.0.5 \
  -d dependencies=web \
  -d type=maven-project \
  -d baseDir=gae-standard-example | tar -xzvf -
$ cd gae-standard-example

4. Update Maven pom.xml

There are two ways to deploy a Java server app—either by using Maven App Engine Plugin or Gradle App Engine Plugin, or by deploying the war package directory. You'll use Maven App Engine Plugin to deploy the app.

Add Maven App Engine Plugin

Update pom.xml to include a Google Cloud 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>2.4.4</version>
        <configuration>
          <version>1</version>
          <projectId>GCLOUD_CONFIG</projectId>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
</project>

5. Add App Engine descriptor

  1. To deploy the app to App Engine standard environment, you must create a new src/main/appengine/app.yaml descriptor file.
$ mkdir -p src/main/appengine/
$ touch src/main/appengine/app.yaml
  1. Edit the src/main/appengine/app.yaml file and add the following content:

src/main/appengine/app.yaml

runtime: java17
instance_class: F1

6. Add a controller

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

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

package com.example.demo;

...

// Add imports
import org.springframework.web.bind.annotation.*;

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

// Add the controller.
@RestController
class HelloWorldController {
  @GetMapping("/")
  public String hello() {
    return "hello world!";
  }
}

7. Locally run the app

  1. Make sure JAVA_HOME is set to the correct JDK version:
$ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/
  1. You can start the Spring Boot app with the Spring Boot plugin:
$ ./mvnw -DskipTests spring-boot:run
  1. After the app starts, click Web Preview 1a94d5bd10bfc072.pngin the Cloud Shell toolbar and select Preview on port 8080.

3aca52f76c6c22a3.png

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

7b0d8494f647822a.png

8. Deploy the app to App Engine

  1. First, initialize the project to be able to run App Engine apps. Also, initialize the project to run in the central region of the US.
$ 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
  1. Then, deploy your app to App Engine standard environment by running mvn appengine:deploy.
$ ./mvnw -DskipTests package appengine:deploy
... first time deploy may take a couple of minutes
  1. After the app is deployed, you can visit it by opening http://<project-id>.appspot.com in your web browser or use the following command in Cloud Shell:
$ gcloud app browse
... [It may print out the URL for your app]

9. Cleanup

You can't delete an App Engine application but you can disable it.

Go to App Engine and Settings in Google Cloud Console and select Disable Application:

8052c1e4ad73d70e.png

Alternatively, you can delete the whole project:

$ gcloud projects delete YOUR-PROJECT-ID

10. Congratulations

You learned to write your first App Engine web app!

Learn more