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.
The instructor will be sharing with you temporary accounts with existing projects that are already setup so you do not need to worry about enabling billing or any cost associated with running this codelab. Note that all these accounts will be disabled soon after the codelab is over.
Once you have received a temporary username / password to login from the instructor, log into Google Cloud Console: https://console.cloud.google.com/.
Here's what you should see once logged in :
Note the project ID you were assigned ( "codelab-test003
" in the screenshot above). It will be referred to later in this codelab as PROJECT_ID
.
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. 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):
Click the "Start Cloud Shell" button:
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>
Cloud Shell also sets some environment variables by default which may be useful as you run future commands.
echo $GOOGLE_CLOUD_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 your PROJECT_ID
? Check out what ID you used in the setup steps or look it up in the console dashboard:
IMPORTANT: Finally, set the default zone and project configuration:
gcloud config set compute/zone us-central1-f
You can choose a variety of different zones. Learn more in the Regions & Zones documentation.
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:
Once the Spring Boot CLI is installed, you can initialize and bootstrap a new Helloworld web application:
$ curl https://start.spring.io/starter.tgz \ -d dependencies=web -d baseDir=helloworld | tar -xzvf -
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 the Web Code Editor by clicking Launch code editor from the Cloud Shell menu.
Once the code editor is open, find the file helloworld/src/main/java/com/example/demo/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:
src/main/java/com/example/demo/DemoApplication.java
package com.example; ... // Add the import import org.springframework.web.bind.annotation.*; @SpringBootApplication public class DemoApplication { ... } // Add the controller @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 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.
You learned how to build and launch a new Java web-based application directly from Cloud Shell.
This work is licensed under a Creative Commons Attribution 2.0 Generic License.