Tuesday, January 28, 2020

How to deploy Spring Boot app in Google Cloud Compute Engine with embedded tomcat?

Now a days deploying Spring Boot application is quite easy in Cloud Platform. A lot of articles already have published on the web but unfortunately I have not seen a single article written in this topic. Hence I planned to write a blog in this topic. You will get a lot of article on deployment of Spring Boot application in Google Cloud App Engine but not on Compute Engine. Hence be ready for Compute Engine.

How to deploy Spring Boot app in Google Cloud Compute Engine with embedded tomcat?


First Let's understand the difference between App Engine & Compute Engine.
App Engine is a Platform-as-a-Service. It means that you simply deploy your code, and the platform does everything else for you. For example, if your app becomes very successful, App Engine will automatically create more instances to handle the increased volume.

Compute Engine is an Infrastructure-as-a-Service. You have to create and configure your own virtual machine instances. It gives you more flexibility and generally costs much less than App Engine. The drawback is that you have to manage your app and virtual machines yourself.

I will recommend to use Ubuntu operating system if you are using Compute Engine and install the required software like Java, Git, Maven etc. You can visit here to know How to setup Google Cloud Compute Engine for Spring Boot project?  Let's come to our main topic.


Open the server terminal by going to Compute Engine -> VM Instances -> SSH or you can open from local terminal as well if you have configured your ssh key in your machine. To open/connect locally use this command.
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.

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

2. Then you need to kill the process by using this command.
kill -9 2965
3. 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
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

9 Responses to "How to deploy Spring Boot app in Google Cloud Compute Engine with embedded tomcat?"

  1. A very helpful article. Much appreciated

    ReplyDelete
  2. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up. spring boot certification course
    spring boot full course
    spring boot online training hyderabad

    ReplyDelete
  3. Very informative. Now I got an idea about this topic. thanks for the content.
    Neuromorphic Computing Applications
    What is Neuromorphic Computing

    ReplyDelete
  4. I visited multiple blοgs but this site is actually marvelous. Alѕo νisіt my web blοg RobloxPlayer.Exe

    ReplyDelete