Deploying a Spring Boot application on Google Cloud App Engine

In this section, we will learn how to deploy a Spring Boot application in Google Cloud App Engine.


1.  A little bit of Background

GCP App Engine

App Engine is a fully managed, serverless platform for developing and hosting web applications at scale. You can choose from several popular languages, libraries, and frameworks to develop your apps, and then let App Engine take care of provisioning servers and scaling your app instances based on demand.

Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can "just run". 


2. Creating a simple spring boot web application

First, open the Spring initializr https://start.spring.io/ 

Then, Provide the Group and Artifact name. We have provided Group name com.knf.dev.demo and Artifact spring-boot-gcp-helloworld. Here I selected the Maven project - language Java 11 - Spring Boot 2.7.9 and add Spring web dependency.


Then, click on the Generate button. When we click on the Generate button, it starts packing the project in a .zip(spring-boot-gcp-helloworld) file and downloads the project. Then, Extract the Zip file. 

Then, import the project on your favourite IDE.

Final Project directory:


In order to make the deployment to App Engine easier, we include Google Cloud App Engine Apache Maven Plugin to our pom.xml.
<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>

Complete pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<
modelVersion>4.0.0</modelVersion>
<
parent>
<
groupId>org.springframework.boot</groupId>
<
artifactId>spring-boot-starter-parent</artifactId>
<
version>2.7.9</version>
<
relativePath/> <!-- lookup parent from repository -->
</parent>
<
groupId>com.knf.dev.demo</groupId>
<
artifactId>spring-boot-gcp-helloworld</artifactId>
<
version>0.0.1-SNAPSHOT</version>
<
name>spring-boot-gcp-helloworld</name>
<
description>Demo project for Spring Boot</description>
<
properties>
<
java.version>11</java.version>
</
properties>
<
dependencies>
<
dependency>
<
groupId>org.springframework.boot</groupId>
<
artifactId>spring-boot-starter-web</artifactId>
</
dependency>

<
dependency>
<
groupId>org.springframework.boot</groupId>
<
artifactId>spring-boot-starter-test</artifactId>
<
scope>test</scope>
</
dependency>
</
dependencies>

<
build>
<
plugins>
<
plugin>
<
groupId>org.springframework.boot</groupId>
<
artifactId>spring-boot-maven-plugin</artifactId>
</
plugin>
<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>

app.yaml

To deploy the application to App Engine standard environment, you must create an appengine folder in the main directory: src/main/appengine/ and create an app.yaml file inside the appengine folder and add the following content.
runtime: java11
instance_class: F1

Create Hello Controller

package com.knf.dev.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@GetMapping("/hello")
public String getMessage()
{
return "Hello World!";
}
}
  • Spring @RestController annotation is used to create RESTful web services using Spring MVC. Spring RestController takes care of mapping request data to the defined request handler method. Once response body is generated from the handler method, it converts it to JSON response.
  • @GetMapping annotation for mapping HTTP GET requests onto specific handler methods.

Application.java

package com.knf.dev.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
Application is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.


3. Create a New Repository and Upload Files on GitHub

First, sign in to Github https://github.com/ 

Then, create a new repository "spring-boot-gcp-helloworld". 

Then, upload the source code from your local machine to the newly created Github repo.



4. Create a GCP Project

We will need to create or select a GCP project that we’ll use to deploy to. 
First, Sign into the Google console at https://console.cloud.google.com.
You can create a new project by first selecting the project dropdown in the top left and selecting "New Project".


Next, specify your GCP Project name and Project ID.

Then 
Click on the "CREATE" button.


5. Deploy the application to App Engine

Start the Google Cloud Shell in the browser.
Button to activate cloud shell is marked in the below image.

Next, clone the git repository:
git clone https://github.com/knowledgefactory4u/spring-boot-gcp-helloworld.git

When creating an application in App Engine, we need to choose a region where the application will be running. The list of regions can be shown by executing the command:

gcloud app regions list

We will choose region us-west4 and create our AppEngine app:

gcloud app create --region us-west4


Change the directory to spring-boot-gcp-helloworld.

cd spring-boot-gcp-helloworld

From within your git repository directory, you can deploy your application by executing below command.
mvn -DskipTests package appengine:deploy
If everything goes fine, then you will see the following similar output in your cloud shell:

Copy the URL of the deployed service. 


6. Verify the API using postman

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete