Spring Boot on Google Compute Engine

Author: Brian Nettles | Date Created: May 18, 2018 | Visits: 213

I am writing this article for a couple of reasons.

1. There are many articles found online which tell how to deploy a spring boot application on Google Application Engine.
2. Deploying Spring Boot on Google Compute Engine on a Debian 8 machine gave me some problems. I had to find a work around.

Honestly, Spring Boot is the most beautiful framework to ever grace the world in Java development. It is so easy to use. If I never have to pick up another framework again, the complaints will not come from me. Even the deployment of Spring Boot web applications is very simple. Simply package the maven based application with the jar in the pom.xml file with like this - jar, invoke mvn build, and execute the jar file with java -jar myspringbootjarname.jar. Then watch your application startup with the built in tomcat server loading the app on port 8080.

Deploying the Spring Boot application is also very simple on a typical linux machine. Place the jar file on your server, create a symlink such as

sudo ln -s /mydirectory/appname.jar /etc/init.d/appname
Then start your service:

service myapp start

sudo /etc/init.d/myapp start
Only problem was, that on my Debian 8 instance in Google Compute Engine, it did not work. I got some garbage messages on the startup. So I tried just invoking spring boot by the command line a different way.

java -jar myapp.jar
It all seemed to start up but failed again not being able to find the JSP files.

So I tried installing Tomcat. These should work.

sudo apt-get update
sudo apt-get install unzip
sudo apt-get install openjdk-8-jdk
sudo apt-get install tomcat8 tomcat8-admin tomcat8-examples tomcat8-docs
Another failure. Logs showed various error issues. Missing zip library - which I added. Missing classes....

So I tried again with a new instance. This time, I got it all working. With all the good in GCP, sometimes we hit a snag. Maybe I am the problem, but I will assume not.

Solution - Spring Boot Web Application on Debian 8 in Google Compute Engine

1. Install Java.
2. Download the tar file of the Tomcat application.
3. Install a spring boot war file onto Tomcat (Don't use ROOT in the webapps directory).
4. Ensure app works on port 8080.
5. Install Apache2.
6. Proxy your Apache2 to the Tomcat instance of your application. (ie. http://localhost:8080/myapp/).

One other thing, you will also need to prepare your Spring Boot application to run on an external Tomcat server. Some fanstastic instructions are found here:

Here are all the commands to do these items. Some of these were unexpected to me and required internet searches in various other locations to find them.

Not meaning to state the obvious, but run these each one at a time, and fix "/mydirectory/" to the directory you choose to install Tomcat in.

sudo apt-get update
sudo apt-get install unzip
sudo apt-get install openjdk-8-jdk
sudo apt-get install haveged #tomcat is slow without this
sudo wget http://mirror.metrocast.net/apache/tomcat/tomcat-8/v8.5.31/bin/apache-tomcat-8.5.31.tar.gz
sudo chmod 744 apache-tomcat-8.5.31.tar.gz
sudo mkdir /mydirectory/
sudo mv apache-tomcat-8.5.31.tar.gz /mydirectory/
sudo cd /mydirectory/
sudo tar -xvf /mydirectory/apache-tomcat-8.5.31.tar.gz
sudo /mydirectory/apache-tomcat-8.5.31/bin/./startup.sh
sudo netstat -lptu # test that the application is running on port 8080
sudo apt-get upgrade openssl
sudo apt-get install apache2 -y
sudo a2ensite default-ssl
sudo a2enmod ssl
sudo a2enmod rewrite
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
Additionally, you need to make changes to the Apache configuration file for specifying the proxy.

sudo vi /etc/apache2/sites-available/000-default.conf
Now add the proxy information. Your file should include the following with your VirtualHost body.

ProxyRequests off
ProxyPreserveHost on

<Proxy *>
Order deny,allow
Allow from all

<Location / >
ProxyPass http://localhost:8080/myapp/
ProxyPassReverse http://localhost:8080/myapp/

RewriteEngine On
With your proxy settings in place in Apache2, you should be able to start it up and see the application running.

sudo service apache2 restart
Let's not forget the fact that we will still need to write the startup script so that the server will always start Tomcat and then start Apache upon server restart. Now access the application using the server external ip address.

Have fun building your web applications using the fantastic Spring Boot framework.

Trisummit Technologies
6421 N. Lena Place
Tucson, AZ 85741
© 2018 All Rights Reserved