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
Spring Boot
2. Creating a simple spring boot web application
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
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