Build and launch a Spring Boot Java app from Cloud Shell

Cloud Shell is a browser-based command-line tool that allows you to access Google Cloud resources. Cloud Shell makes it easy to manage your Cloud Console projects and resources without having to install the Cloud SDK and other tools on your system.

You'll use Cloud Shell to build and launch a Spring Boot app without ever leaving your browser. To do so, you'll use the sample code from Building an App with Spring Boot.

Prerequisites

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

What you'll do

  • Use Cloud Shell.
  • Create a simple Spring Boot app in Cloud Shell.
  • Edit the app using the code editor in Cloud Shell.
  • Launch the app from Cloud Shell.

What you'll need

Self-paced environment setup

  1. If you don't already have a Google Account, then you must create one. Sign into Cloud Console and create a new project.

870a3cbd6541ee86.png

Screenshot from 2016-02-10 12:45:26.png

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

  1. Next, you need to enable billing in the Cloud Console to use Google Cloud resources. Running through the 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.

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

Cloud Shell

This Debian-based virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory and runs in 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).

  1. To activate Cloud Shell from the Cloud Console, simply click Activate Cloud Shell FO8DjOjOwp7E_AgllbzMOCqv_SfH_lLUWN9tCFIMPwa5VZTiOY6qzPMcOUva-9jDf31uRWMLr3cWpByhh-MWFxNFka7hUxqGiuZlTQDBzJGAu6nimCRIy5adfbJpf49hB_wvafIUSw (it should only take a few moments to provision and connect to the environment).

PE_JeA9fwUGvNb-_SWAgKPgMFbmN-J7iPILKxDXJCjKJmvUlf1yVa1xFYYYK9gns1HcotuA9wl9fw2qoJU086LLY1LqnQM0f-FXaHXlRQPX3Wv3uTxCgZN-Nlxz8-otZ6VLlQVNdxQ

Screen Shot 2017-06-14 at 10.13.43 PM.png

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

gcloud auth list

Command output

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)
gcloud config list project

Command output

[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 your PROJECT_ID? Check out what ID you used in the setup steps or look it up in the Cloud Console dashboard:

C-g7EL2Qa_3wZNljH4_WtymHrRniD_sBQM2ONLh6ZeEG1CZt7P0ZRCSr7zVKW9HGAcXye2XfcaVRiW6BX-Xy93QDKeXpMxm_0SzFdBo3P1A8Dwh0Abrtpp3F7sppadwXsOVtXKyIOQ

Cloud Shell also sets some environment variables by default, which may be useful as you run future commands.

echo $GOOGLE_CLOUD_PROJECT

Command output

<PROJECT_ID>
  1. Finally, set the default zone and project configuration.
gcloud config set compute/zone us-central1-f

You can choose a variety of different zones. For more information, see Regions & Zones.

After the Spring Boot command-line tool is installed, you can initialize and bootstrap a new "Hello, World" web app.

$ curl https://start.spring.io/starter.tgz \
  -d dependencies=web -d baseDir=helloworld | tar -xzvf -

That creates a new directory with a new Maven project, along with Maven's pom.xml, a Maven wrapper, and an app entry point.

  1. Open the code editor by clicking Launch editor 26e896774198a7b9.pngin the Cloud Shell menu.
  2. After the editor opens, find the helloworld/src/main/java/com/example/demo/DemoApplication.java file.

f26aa4faf9b35058.png

  1. After the code opens, 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!";
        }
}
  1. Save the file!
  1. You can start the Spring Boot app normally with the Spring Boot plugin.
$ cd $HOME/helloworld
$ ./mvnw -DskipTests spring-boot:run

ab9da340cbea6fe4.png

  1. After the app starts, click Web Preview 396bfd51f55afb5d.png in the Cloud Shell toolbar, then click Preview on port 8080.

2c79a5762def7494.png

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

175ef4dbcf3afcf3.png

You learned to build and launch a new Spring Boot Java web app directly from Cloud Shell.

Learn more