Google Cloud Shell is a browser-based command line tool to access Google Cloud Platform resources. Cloud Shell makes it really easy to manage your Cloud Platform Console projects and resources without having to install the Google Cloud SDK and other tools on your system. With Cloud Shell, the Cloud SDK gcloud command and tools you need to build a Java application, such as the JVM, Maven, and Gradle.

In this lab, you will learn about how to build and launch an Java web-application created with Spring Boot from Google Cloud Shell — without ever leaving the browser.

This tutorial uses the sample code from the Spring Boot Getting Started guide.

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

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 Developers 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. This Debian-based virtual machine is loaded with all the development tools you'll need (docker, gcloud, kubectl and more), it offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. This means that all you will need for this codelab is a browser (yes, it works on a Chromebook).

To activate Google Cloud Shell, from the developer console simply click the button on the top right-hand side (it should only take a few moments to provision and connect to the environment):

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
Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)
$ gcloud config list project
[core]
project = <PROJECT_ID>

If for some reason the project is not set, simply issue the following command :

$ gcloud config set project <PROJECT_ID>

Looking for you PROJECT_ID? Check out what ID you used in the setup steps or look it up in the console dashboard :

Start Cloud Shell

Navigate to the the Google Cloud Console from another browser tab/window, to https://console.cloud.google.com. Use the login credential given to you by the lab proctor.

You will do all of the work from the Google Cloud Shell, a command line environment running in the Cloud. This Debian-based virtual machine is loaded with all the development tools you'll need (gcloud, git and others) and offers a persistent 5GB home directory. Open the Google Cloud Shell by clicking on the icon on the top right of the screen:

Google Cloud Shell has both Java 7 and Java 8 installed. It uses Java 7 by default. Let's switch to use Java 8 instead. In the Cloud Shell, use update-alternative command to change the default Java version (make sure you select the java-8-openjdk option by typing "2"):

$ sudo update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).
  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-openjdk-amd64/bin/javac   ...
  1            /usr/lib/jvm/java-7-openjdk-amd64/bin/javac   ...
  2            /usr/lib/jvm/java-8-openjdk-amd64/bin/javac   ...
Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/bin/javac to provide /usr/bin/javac (javac) in manual mode

$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).
  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   ...
  1            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   ...
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   ...
Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode

For the lab, we'll build something from scratch. First, install the basic command line utilities that you'll need to initialize a Spring Boot Java application.

First, install SDK manager - this can help you install Spring Boot CLI with ease:

$ curl -s "https://get.sdkman.io" | bash

Then, run the command the initialize the environmental variables:

$ source "$HOME/.sdkman/bin/sdkman-init.sh"

Finally, install the Spring Boot CLI utility:

$ sdk install springboot

Once the Spring Boot CLI is installed, you can initialize and bootstrap a new Helloworld web application:

$ spring init --dependencies=web helloworld

This will create a new directory with a new Maven project, along with Maven's pom.xml, a Maven wrapper, as well as an application entrypoint.

Open Eclipse Orion by clicking Files and then Launch code editor from the Cloud Shell menu.

Once the code editor is open, find the file helloworld/src/main/java/com/example/DemoApplication.java.

Once the code is open, create a new RESTful controller to respond "Hello". In the DemoApplication.java file, add a new Helloworld class definition in addition to the current one:

package com.example;

import ...;
...

import org.springframework.web.bind.annotation.*;

@SpringBootApplication
public class DemoApplication {
...
}

@RestController
class Helloworld {
        @GetMapping("/")
        public String greet() {
                return "Hello!";
        }
}

Don't forget to save the file!

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

$ cd $HOME/helloworld
$ ./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.

In this step, you set up a simple Spring Boot Java web application and ran it directly from within Cloud Shell.

You learned how to build and launch a new Java web-based application directly from Cloud Shell.

Learn More

License

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