Spring Boot: Launch your application in Google Cloud Platform (GCP)

Table of contents
Reading Time: 4 minutes

Google Cloud Platform provides a cloud computing services that run the Spring Boot application in the cloud environment in much simpler and seamlessly manner. In this blog we will talk about Google Cloud Platform and Spring boot and how users can deploy spring boot application in App Engine Google Cloud Platform.

Google Cloud Platform (GCP):

Google Cloud Platform is offered by Google, is a suite of cloud computing services that runs on the same infrastructure that Google uses internally for its end-user products, such as Google Search, Gmail, Google Drive, and YouTube. Google App Engine lets app developers build scalable web and mobile back ends in any programming language on a fully managed serverless platform.

Java Spring Boot:

Java Spring Framework (Spring Framework) is a popular, open source, enterprise-level framework for creating standalone, production-grade applications that run on the Java Virtual Machine (JVM).

Java Spring Boot (Spring Boot) is a tool that makes developing web application and microservices with Spring Framework faster and easier through three core capabilities:

  1. Autoconfiguration
  2. An opinionated approach to configuration
  3. The ability to create standalone applications

These features work together to provide you with a tool that allows you to set up a Spring-based application with minimal configuration and setup.

Step for spring boot application deployment:

For deploying spring boot application user should follow following steps:

Step 1. Setup up project on google cloud console http://console.cloud.google.com/

Cloud Shell

User should activate Cloud Shell (Command line environment running in Google Cloud)

Step to active cloud shell:

  1. From the Cloud Console, click Activate Cloud Shell .

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

  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].

Step 2. 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 Initializer.

$ curl https://start.spring.io/starter.tgz \

-d bootVersion=2.3.0.RELEASE \

-d dependencies=web \

-d baseDir=gae-standard-example | tar -xzvf –

$ cd gae-standard-example

Step 3. 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 looks file following:

<?xml version=”1.0″ encoding=”UTF-8″?>
<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; …>
  …
  <build>
    <plugins>
      …
      <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>appengine-maven-plugin</artifactId>
        <version>2.2.0</version>
        <configuration>
          <version>1</version>
          <projectId>GCLOUD_CONFIG</projectId>
        </configuration>
      </plugin>
      …
    </plugins>
  </build>
</project>

Step 4. 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:

$ cat src/main/appengine/app.yaml

Output:

runtime: java11
instance_class: F1

Step 5. Add a controller

Add a new controller that returns “hello Knoldus!” in DemoApplication.java.

$ cat 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 HelloKnoldusController {
  @GetMapping(“/”)
  public String hello() {
    return “hello knoldus!”;
  }
}

Step 6. Locally run the app

  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  in the Cloud Shell toolbar and select Preview on port 8080.

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

Step 7. 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

Output:

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 following command:

$ 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&gt;.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]

Congratulations! You have successfully written and deployed your first demo app to GCP.

Reference:

[1]. https://cloud.google.com/java/docs/reference/spring

[2]. https://developers.google.com/learn/pathways/springboot-google-cloud

[3]. https://en.wikipedia.org/wiki/Google_Cloud_Platform

[4]. https://github.com/GoogleCloudPlatform/spring-cloud-gcp/tree/main/spring-cloud-gcp-samples