Deploy your Spring Boot project on the cloud for FREE

Ryan Yu Liu
5 min readAug 7, 2017

Perhaps you have read about how easy it is to develop a new java app in Spring Boot, or you are to create a new repository on GitHub to showcase your development chops, or maybe you are new to Java and just want to learn. You are itching to get things start, and you want the world to see the amazing app you are going to build.

A few years ago this may have been a time consuming effort. With the advent of freemium offering from PaaS companies, you can get your shining new app up and running with most of the checkmarks of production readiness, all for $0.

While this guide is Java/Spring Boot focused, many of the resource included, such as Heroku, New Relic, and Travis CI support most of the popular programming languages.

Source Control

Github, ‘nuff said (or GitLab, and bitbucket if you want to keep your project private).

Hosting

One of the easiest way to host your app now is Heroku, which supports all the major languages (sorry Rust lovers). Heroku makes things easy by allowing you to deploy directly through git push. You can even setup Heroku to automatically monitor a particular branch of your GitHub repo to trigger auto-deployment.

For Spring Boot projects using Maven or Gradle, there is basically zero setup after you have signed-up for an account and created a new application. Simply git push to the Heroku remote and your application will be running and accessible to the entire world, with https too. Heroku is smart enough to detect the language you are using and provide default parameters to get it running. If you need more control, include a Procfile in root of the repo. This will allow you to customize how your app will run. For example:

web: java -Dserver.port=$PORT $JAVA_OPTS target/my-app.jar

will tell Heroku to run my-app.jar as java web application.

Configuration

In addition, Heroku provides an easy way to externalize configuration, such as database credentials and api keys, that shouldn’t be committed to your open source project. Under the Settings tab of your app, you will find a section called Config Variables, which will be passed to your application as JVM params (and environment variables for other programming languages as well)

In there, you will be able to define any spring-data common configurations for your application to pick up.

CLI tools

Heroku also offers a toolbelt full of useful commands, such as “heroku logs” and “heroku restart” to help you manage your application.

Database

Depending on your choice of persistence layer, there are many PaaS vendor that provides a free tier to get you started. Heroku itself offers Postgres, Redis and Kafka to suit your needs. For a recent project, I had a chance to try out Atlas, MongoDB’s new cloud offering of their eponymous database. Once you signed up for an account, setup your database and obtain connection uri/credentials, simply add them to Heroku’s Config Variables page for Spring Boot to pick up.

Continuous Integration

Being a seasoned developer that you are, or maybe you have just read from a blog somewhere that it is a good idea, you want to setup continuous integration for your app. This is especially important when you have a few friends working with you on the project. Continuous integration helps to ensure that the code that gets merged into your master (read production) branch is of a good enough quality (as long as you also write tests).

Travis CI is just the thing for you. It will automatically trigger a build from your Maven or Gradle project for each PR that’s created in your GitHub repo. If you want to be extra careful, and I recommend that you do, you can configure your GitHub repo to not allow merges into the master branch unless build has passed. To do so, click on Settings tab on the repo, click on Branches, and add the master branch as a Protected Branch. You will see Travis listed there as a check required before merging.

Test database

If you wrote integration tests (again you should, and Spring Boot makes this easy), Travis CI offers a great number of database options that could spin up to support your tests. To do this, include a .travis.yml file under the repo root, with something like this:

language: java
jdk:
— oraclejdk8
services: mongodb

Monitoring

As your application gets more and more complex, sometimes things break, or certain endpoints becomes slow, and you want to find out why. New Relic, an application performance monitoring service, or APM, is just what you need.

Heroku shines again by offering an easy way to integration with New Relic, which also offers a free tier. Get start by adding the New Relic Add-On to your app. Once that’s done, follow the link to jump over to New Relic for setup instructions. The first choice you have make is the language of application, and since we are using Spring Boot, Java it is.

The instructions for setting up Java offers you an option through Maven/Gradle or manual setup. Having gone down the Maven route, I can report that it does not work all that well with Spring Boot since it requires adding New Relic jar to the artifact and unpacking it after deployment. The manual route is by far the path of less resistance. To get started, download and unzip the New Relic agent under the repo root, then create or edit your Heroku Procfile to use the jar

web: java -Dserver.port=$PORT $JAVA_OPTS -javaagent:newrelic/newrelic.jar -jar target/my-app.jar

Conclusion

Hopefully, with these steps, your new project will become a huge success. If not, you can be comforted by the thought that your hobby app is more mature and stable than 90% of the enterprise applications that out there. Happy coding.

--

--