Wednesday, January 29, 2020

How to setup Google Cloud Compute Engine for Spring Boot project?

You should have the Google account to for this. You will get $300 credit for 12 months for Google Cloud computing. In this article there are 2 parts. 1st will will tell you the billing part information and 2nd it will explain how to setup the project. So let's follow some steps to achieve this goal.

PART 1 : Setup Billing Information

Step 1: Search Google Cloud in Google or directly go to https://cloud.google.com/ . You need to setup the Cloud Platform. Click on Continue.
How to setup Google Cloud Compute Engine for Spring Boot project?

Step 2: Fill the required form.
javaaltaf How to setup Google Cloud Compute Engine for Spring Boot project?

It will also ask for the payment details.
card details google cloud compute engine
Here Payment method is required. You need to give your debit/credit card details. You need to pay 1 Rupee to the GOOGLE CLOUD INDIA. Now click on START MY FREE TRIAL. After completion this you will get this info.
transaction completion compute engine

Step 3: Next step is to verify the your payment details. Whether you are the payment owner or not.
payment verification google cloud compute engine
For verification, they will ask your first/last name and need to upload one government id proof and debit/credit photo. It might take some hours for verification. After verifying you will get information like this.
payment verification completion google cloud compute engine
After completing the verification, you will get free access for 1 year. Now you can perform the required task for google cloud computing.



PART 2: Setup the execution environment

Step 1: Open Google Cloud Console and go to Compute Engine -> VM Instances

google cloud compute engine vm instance


Now Create an instance and configure your machine. Based on your Machine Type, they will charge you money. Here you can configure your Operating System, Hard Disk size etc.
machine setup google cloud compute engine

After creating VM Instance, your machine will be visible with ip address.
machine info google cloud compute engine
Step 2: Connect your machine by clicking on SSH or by clicking drop down menu Open in browser window. It will open in new window. You will see the black screen with username & machine name. This is your machine terminal. Any operation if you want perform, you can do only with this terminal.
google cloud compute engine terminal

You can open this terminal from your local terminal/command prompt but before you need to set your public ssh key into metadata of Google Cloud. To set this open Compute Engine -> Metadata
google cloud compute engine metadata
To generate ssh key, open your local terminal and type
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" 
It will ask Enter file in which to save the key:. You can type the file name on which you want to save. If you want default file name(id_rsa) then just press Enter.
Again it will ask Enter passphrase (empty for no passphrase):. You can use password or if you don't want then just hit Enter 2 times. File will be saved into /home/username/.ssh/filename.pub. Go to the Home location and press Ctrl+H to see the hidden files. .ssh directory will be hidden. Go to inside the ssh directory and open the file id_rsa.pub with any of your favorite editor and copy the complete text and paste into Metadata SSH Keys. Click on Edit
google cloud compute engine metadata ssh keys
then Click on Add Item
google cloud compute engine metadata ssh keys
Now here paste your copied ssh key then Save
google cloud compute engine metadata ssh key add item
Finally, open your local terminal and connect with your host machine ip address. 
ssh username@34.93.xxx.xxx
First time it will ask yes/no question. press yes and hit Enter. Congrats! you have connected. Now you need to install the required software like Java, Git, Maven  etc.
Install Java : sudo apt-get install openjdk-8-jdk
Git : sudo apt-get install git
Maven : sudo apt install maven
Curl : sudo apt install curl
Nano(Editor) : sudo apt-get install nano
Now you will have to install PM2 which is a process manager for the JavaScript runtime Node.js. I know you have a question What is the need of Node in Java environment? You have good question which will be answered below.

Step 3: Clone an Spring Boot project from your Github repository. If you don't have you can clone from my Github repository. Now execute your project. But how? You can execute by using maven command.
mvn spring-boot:run
Great, you have deployed your Spring Boot application into Google Cloud Compute Engine but what will happen if you close your terminal? :) :) :)
Oh no! my application stopped working.

Yes, this is the problem. You can solve this problem by executing the command in background.
setsid mvn spring-boot:run
Now even you close the terminal, your application will work but what will happen if you need to restart the server? You will get this error
Web server failed to start. Port 8080 was already in use.
mvn spring-boot:run termnal gcp compute engine
You can solve this problem as well. What you need to do is
A. First of all, you need to find the pid(process id) of the running application based on the port.
lsof -i:8080
You will get output as
lsof -i:8080
2965 is your processId.

B. Then you need to kill the process by using this command.
kill -9 2965
C. And again you need to start the server by using setsid command.

Now you can think how this is painful. Hence to resolve this issue you can use PM2.
curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt-get install nodejs
sudo npm install -g pm2

Step 4: I think you have already cloned the project from Github. Then go to the project directory by using cd command
git clone https://github.com/altafjava/spring-boot-test.git
cd spring-boot-test/
You will have to create one .sh file which will be used by the PM2 to restart the server. In your .sh file you need to write the command which is used to execute the spring boot project that is nothing but mvn spring-boot:run
echo "mvn spring-boot:run">server.sh
chmod +x server.sh
Finally, restart the server
pm2 start server.sh
You can even check the logs on runtime by using
pm2 logs

If you have come so far, then I think you have done great job. Congrats!
Still you have any doubt feel free to comment.

Share this

1 Response to "How to setup Google Cloud Compute Engine for Spring Boot project?"