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.
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.
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.
Oh no! my application stopped working.
Yes, this is the problem. You can solve this problem by executing the command in background.
1. First of all, you need to find the pid(process id) of the running application based on the port.
2965 is your processId.
2. Then you need to kill the process by using this command.
Now you can think how this is painful. Hence to resolve this issue you can use PM2.
If you have come so far, then I think you have done great job. Congrats!
Still you have any doubt feel free to comment.
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.xxxFirst 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 nanoNow 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:runGreat, 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:runNow 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.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:8080You will get output as
2965 is your processId.
2. Then you need to kill the process by using this command.
kill -9 29653. 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 pm2I 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.
A very helpful article. Much appreciated
ReplyDeleteNyc article ..
ReplyDeleteNice bro
ReplyDelete
ReplyDeleteThanks for sharing such a good and informative content to all of us
GCP Online Training
Google Cloud Platform Training In Hyderabad
Google Cloud Platform Training
Google Cloud Platform Training Online
wow this is really for everyone.keep sharing blog daily.thanks for sharinf your post
ReplyDeleteFull Stack Training in Chennai | Certification | Online Training Course | Full Stack Training in Bangalore | Certification | Online Training Course | Full Stack Training in Hyderabad | Certification | Online Training Course | Full Stack Training in Pune | Certification | Online Training Course | Full Stack Training | Certification | Full Stack Online Training Course
good
ReplyDeleteVery 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
ReplyDeletespring boot full course
spring boot online training hyderabad
Very informative. Now I got an idea about this topic. thanks for the content.
ReplyDeleteNeuromorphic Computing Applications
What is Neuromorphic Computing
I visited multiple blοgs but this site is actually marvelous. Alѕo νisіt my web blοg RobloxPlayer.Exe
ReplyDelete