Loading...
No results found.

Apply your skills in Google Cloud console

Building Scalable Java Microservices with Spring Boot and Spring Cloud

Get access to 700+ labs and courses

JAVAMS09 Deploying to App Engine

Lab 2 hours universal_currency_alt 5 Credits show_chart Introductory
info This lab may incorporate AI tools to support your learning.
Get access to 700+ labs and courses

Overview

In this series of labs, you take a demo microservices Java application built with the Spring framework and modify it to use an external database server. You adopt some of the best practices for tracing, configuration management, and integration with other services using integration patterns.

So far in the labs, you've tested the application by running the application in Cloud Shell. Many options are available to deploy your application on Google Cloud. For example, you can deploy the application to virtual machines that you configure yourself. Or you can containerize your application and deploy it into a managed Google Kubernetes Engine cluster. You can also run your favorite platform as a service on Google Cloud (for example, Cloud Foundry and OpenShift).

App Engine is Google's fully managed serverless application platform. With App Engine, you can build and deploy applications on a fully managed platform. You can scale your applications without having to worry about managing the underlying infrastructure. App Engine includes capabilities such as automatic scaling-up and scaling-down of your application, and fully managed patching and management of your servers.

In this lab, you deploy the application into App Engine. You convert the application from fat WARs into the thin-WAR deployments that App Engine can deploy.

Objectives

In this lab, you learn how to perform the following tasks:

  • Initialize App Engine
  • Reconfigure an application to work with App Engine
  • Configure the Cloud Runtime Configuration API to automatically provide the backend URL to the Frontend application
  • Deploy the application to App Engine

Setup and requirements

How to start your lab and sign in to the Console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is a panel populated with the temporary credentials that you must use for this lab.

  2. Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Choose an account page.

    Note: Open the tabs in separate windows, side-by-side.
  3. On the Choose an account page, click Use Another Account. The Sign in page opens.

  4. Paste the username that you copied from the Connection Details panel. Then copy and paste the password.

Note: You must use the credentials from the Connection Details panel. Do not use your Google Cloud Skills Boost credentials. If you have your own Google Cloud account, do not use it for this lab (avoids incurring charges).
  1. Click through the subsequent pages:
  • Accept the terms and conditions.
  • Do not add recovery options or two-factor authentication (because this is a temporary account).
  • Do not sign up for free trials.

After a few moments, the Cloud console opens in this tab.

Note: You can view the menu with a list of Google Cloud Products and Services by clicking the Navigation menu at the top-left.

After you complete the initial sign-in steps, the project dashboard appears.

Task 1. Fetch the application source files

In this task, you clone the source repository files that are used throughout this lab.

  1. To begin the lab, click the Activate Cloud Shell button at the top of the Google Cloud Console and if prompted click Continue.

  2. To activate the code editor, click the Open Editor button on the toolbar of the Cloud Shell window.

  3. Click Open in a new window to set up the editor in a new tab with continued access to Cloud Shell.

Note: A Cloud Storage bucket that is named using the project ID for this lab is automatically created for you by the lab setup. The source code for your applications is copied into this bucket when the Cloud SQL server is ready. You might have to wait a few minutes for this action to complete.
  1. In Cloud Shell, click Open Terminal and then enter the following command to create an environment variable that contains the project ID for this lab:
export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
  1. Verify that the demo application files were created:
gcloud storage ls gs://$PROJECT_ID
  1. Copy the application folders to Cloud Shell:
gcloud storage cp -r gs://$PROJECT_ID/* ~/
  1. Make the Maven wrapper scripts executable:
chmod +x ~/guestbook-frontend/mvnw chmod +x ~/guestbook-service/mvnw

Now you're ready to go!

Task 2. Initialize App Engine

In this task, you initialize App Engine by enabling it in the project.

  • Enable App Engine in the project:
gcloud app create --region={{{project_0.startup_script.appengine_region|placeholder}}}

The message The app is now created indicates that App Engine is enabled.

Note: App Engine deployments are regional. That is, your application might be deployed to multiple zones within a region. In general, you should deploy your App Engine application near your backend or database.

Task 3. Deploy Guestbook Frontend

In this task, you add the App Engine plugin to the guestbook frontend's pom.xml file.

  1. In the Cloud Shell code editor, open ~/guestbook-frontend/pom.xml.
  2. Add the following code at the end of the <plugin> section, immediately before the closing </plugins> tag:
<plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>appengine-maven-plugin</artifactId> <version>2.2.0</version> <configuration> <version>1</version> <deploy.projectId>GCLOUD_CONFIG</deploy.projectId> </configuration> </plugin>
  1. In Cloud Shell, create an App Engine directory in Guestbook Frontend:
mkdir -p ~/guestbook-frontend/src/main/appengine
  1. In the Cloud Shell code editor, create a file named app.yaml in the ~/guestbook-frontend/src/main/appengine/ directory. This file is required to deploy the application to App Engine.

  2. Add the following code to the app.yaml file. Make sure to replace PROJECT_ID with your Project ID. You can use the command echo $PROJECT_ID in the Cloud Shell to retrieve it:

runtime: java env: flex runtime_config: operating_system: "ubuntu22" runtime_version: "21" manual_scaling: instances: 2 env_variables: SPRING_PROFILES_ACTIVE: cloud MESSAGES_ENDPOINT: https://guestbook-service-dot-PROJECT_ID.appspot.com/guestbookMessages resources: cpu: 1 memory_gb: 1 disk_size_gb: 10 handlers: - url: /.* script: this field is required, but ignored
  1. In Cloud Shell, change to the frontend application directory:
cd ~/guestbook-frontend
  1. Use Apache Maven to deploy the frontend application to App Engine. The deployment should take a few minutes:
mvn package appengine:deploy -DskipTests Note: You may need to run the command twice if there is a build error!

This command reports out success like the following example:

[INFO] GCLOUD: Deployed service [default] to [https://PROJECT_ID.appspot.com] [INFO] GCLOUD: [INFO] GCLOUD: You can stream logs from the command line by running: [INFO] GCLOUD: $ gcloud app logs tail -s default [INFO] GCLOUD: [INFO] GCLOUD: To view your application in the web browser run: [INFO] GCLOUD: $ gcloud app browse [INFO] ------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------ [INFO] Total time: 03:00 min [INFO] Finished at: 2018-12-08T10:30:09Z [INFO] ------------------------------------------------------
  1. Find the frontend URL:
gcloud app browse

This command reports a URL that links to your application's frontend:

Did not detect your browser. Go to this link to view your app: https://....appspot.com
  1. Click the link to open a browser tab to the frontend URL.

An error occurs because the backend isn't deployed yet:

Leave the tab open. You'll come back to this later.

Task 4. Deploy Guestbook Service

In this task, you will add the App Engine Plugin to guestbook-service's pom.xml.

  1. In the ~/guestbook-service/pom.xml file, add the following code at the end of the <plugin> section, immediately before the closing </plugins> tag:
<plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>appengine-maven-plugin</artifactId> <version>2.2.0</version> <configuration> <version>1</version> <deploy.projectId>GCLOUD_CONFIG</deploy.projectId> </configuration> </plugin>
  1. Create an App Engine directory in Guestbook Service:
mkdir -p ~/guestbook-service/src/main/appengine
  1. In the Cloud Shell code editor, create a file named app.yaml in the ~/guestbook-service/src/main/appengine/ directory. This file is required to deploy the application to App Engine.

  2. Add the following code to the app.yaml file:

runtime: java env: flex service: guestbook-service runtime_config: operating_system: "ubuntu22" runtime_version: "21" manual_scaling: instances: 2 env_variables: SPRING_PROFILES_ACTIVE: cloud resources: cpu: 1 memory_gb: 1 disk_size_gb: 10 handlers: - url: /.* script: this field is required, but ignored Note: This configuration uses manual scaling rather than automatic scaling. This is great if you want to have fine control over the number of application instances. However, in a production setting, you may want to use automatic scaling that can adapt dynamically to the load.
  1. In Cloud Shell, first change to your guestbook-service directory, then use Maven to deploy the application. This will take a few minutes:
cd ~/guestbook-service ./mvnw package appengine:deploy -DskipTests
  1. Find the deployed backend URL. Click the URL link for the backend guestbook service:
gcloud app browse -s guestbook-service

Links similar to the ones in the screenshot are displayed. You can use these URLs to list and inspect the contents of messages that have been posted to the application:

  1. Follow the href links to see the messages, for example: https://guestbook-service-dot-PROJECT.appspot.com/guestbookMessages, and see the sample message that is preloaded by the lab setup process:

  1. Lastly, switch to the tab showing the error generated by the frontend application and refresh the page. You should now see the correct user message posting interface that you are familiar with from earlier labs. You see the initial sample message that is preloaded by the lab setup process. Instead of running in Cloud Shell, the application is now running as two separate services on App Engine!

Task 5. Review

In this lab, you initialized App Engine and reconfigured an application to work with App Engine. You also configured the Cloud Runtime Configuration API to automatically provide the backend URL to the Frontend application. Finally, you deployed the application to App Engine.

End your lab

When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.

You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.

The number of stars indicates the following:

  • 1 star = Very dissatisfied
  • 2 stars = Dissatisfied
  • 3 stars = Neutral
  • 4 stars = Satisfied
  • 5 stars = Very satisfied

You can close the dialog box if you don't want to provide feedback.

For feedback, suggestions, or corrections, please use the Support tab.

Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

Previous Next

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

This content is not currently available

We will notify you via email when it becomes available

Great!

We will contact you via email if it becomes available

One lab at a time

Confirm to end all existing labs and start this one

Use private browsing to run the lab

Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
Preview