Deploying a Spring Boot application on Google Cloud Compute Engine (VM)

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


1.  A little bit of Background

Google Cloud Compute Engine

Secure and customizable compute service that lets you create and run virtual machines on Google’s infrastructure. 

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-hello-world. Here I selected the Maven project - language Java 11 - Spring Boot 2.7.8 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-hello-world) file and downloads the project. Then, Extract the Zip file. 

Then, import the project on your favourite IDE.


Final Project directory:


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.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-boot-hello-world</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-hello-world</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>
</plugins>
</build>

</project>


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!";
}
}


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);
}

}


3. Create a New Repo and Upload Files on GitHub

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

Then, create a new repository "spring-boot-hello-world". 

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. Create a new Virtual Machine

Sign into the Google console at https://console.cloud.google.com

Search for "Compute Engine" like below image,

Click on the "Compute Engine", You will be taken to a page like the below image,

Then click on the "CREATE INSTANCE" button.

Then enter VM name as "vm-spring-boot-app" and enable "Allow HTTPS traffic" like below image.

Next, click on "CREATE" button.

You will be taken to a page like the below image,


SSH into the VM by clicking on the "SSH" button.


6. Update the Ubuntu software packages

sudo apt-get update

7. Installing the Java 11 on Ubuntu VM

sudo apt-get install openjdk-11-jdk

8. Installing the Maven on Ubuntu VM

sudo apt install maven

9. Installing the Git on Ubuntu VM

sudo apt install git

10. Clone the Spring App from Github

git clone https://github.com/knowledgefactory4u/spring-boot-hello-world.git

11. Change the directory to spring-boot-hello-world

cd spring-boot-hello-world

12. Building the Spring Boot App

mvn install

13. Change the directory to the target

cd target

14. Run the Spring Boot App as the background process in the VM

nohup java -jar spring-boot-hello-world-0.0.1-SNAPSHOT.jar

15. Expose 8080 port on the VM 

Next, click on "View network details" button.

You will be taken to a page like the below image, click on "default-http-server" button.


You will be taken to a page like the below image. Then, click on the "Edit" button.


Next, Add the 8080 port to the TCP and click the Save button to make the changes.


You will be taken to a page like the below image,


Copy the "External IP ranges"

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