Skip to content
Advertisement

How to deploy Spring Boot application in Google Cloud Compute Engine?

I am a new to Google Cloud platform. I want to deploy my Spring Boot project in Compute Engine because deploying in App Engine costs more than the Compute Engine. There are a lot of videos/articles are available in YouTube/Websites for deploying in App Engine but I did not find any tutorial on deploying Spring Boot app in Compute Engine.

Advertisement

Answer

Here is the very good blog written on How to deploy Spring Boot app in Google Cloud Compute Engine with embedded tomcat?. I am just briefing here.

If you want to use an embedded tomcat server then PM2 is the best tool for managing the deployment in Compute Engine. PM2 is a process manager for the JavaScript runtime Node.js. Actually without PM2 also you can deploy Spring Boot app directly by executing command mvn spring--boot:run but the problem is when you exit the terminal then your server will also go down. Here you can use excute the command in background by using setsid mvn spring-boot:run. This will execute your Spring Boot app in background but when you need to restart the server then you will the error like Web server failed to start. Port 8080 was already in use

enter image description here

Hence you need to kill the existing running app by finding what is the processId. This is somehow a headache. Hence I will also recommend you to use PM2. To install PM2 you can use these commands in ubuntu.

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

If you want test your Spring Boot application, you can clone or pull the project from your Github repository or you can clone the spring-boot-test project from here.

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 by using the command pm2 restart server. You can even check the logs on runtime by using pm2 logs.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement