Spring Boot is the talk of the town when it comes to microservice architectures. But implementing a service is just one part of the story. To truly leverage the power of a microservice architecture, organizations have to embrace the DevOps mindset to get from committed code to deployed applications in minutes. This is where services like Heroku – a cloud platform as a service (PaaS) – come into play. In this blog post I will show how to set up and deploy a Spring Boot application from GitHub to Heroku using Heroku’s build in Postgres as a data store.
The spring-boot-heroku-demo application
To show how to deploy a Spring Boot application to Heroku, I’ve implemented a very simple demo application. You can find the code in my GitHub account. The application uses Spring Data JPA to show all entries in a single table and lets users add new entries to the existing data. The application is already deployed at Heroku and can be tried out at spring-boot-heroku-demo.herokuapp.com (not that I’m only using the free plan, so it may take some time for the application to start up when you visit it the first time). In the remainder of this blog post I will show you how to get your very own fork of this application running on your Heroku account.
Creating a new application at Heroku
First of all you need to create a new application at Heroku using the Heroku Dashboard. Just insert a name, choose the Region where the application should be hosted and click “Create App”.
Creating an application in region Europe
After that, you need to tell Heroku what it should deploy. You can configure this in the “Deploy” tab of the app configuration. The most straightforward way to deploy to Heroku is integration with GitHub. If you don’t happen to use GitHub, there is plenty of documention about how to deploy from different sources. For this example, you can simply fork my demo application at GitHub.
Connecting a Heroku application with a GitHub repository
After enabling GitHub integration at Heroku, you can connect your Heroku app with the cloned repository at GitHub. Furthermore you can choose the branch to deploy from, automatic deployments, integration with CI servers, and review applications for pull requests. For this example we will only enable automatic deployments from the master branch.
Connecting Heroku app with GitHub repository
Once you’ve connected Heroku with your GitHub repository, you can trigger a manual deployment using the “Deploy Branch” button. Wait until the build process has completed and your application will be running at http://<name-of-your-app>.herokuapp.com/.
Setting up a database connection to Heroku’s Postgres database
After the initial deployment, Spring Boot will automatically discover which persistence store to use. Since we’ve added no special configuration it will use the an inmemory HyperSQL database. This is because of the dependency to
org.hsqldb:hsqldb defined in the pom.xml. For this reason all data will be lost after the application is redeployed. To really persist data, we need to connect the application with a database that will store the data beyond application restarts. Heroku will automatically setup a Postgres database when a new app is created, which we can use. You can see all your databases using the database panel.
To configure the database connection at Heroku we will use Spring Boots ability to map environment variables to configuration properties. Our application needs the following properties to be set:
Furthermore we should set
spring.jpa.hibernate.ddl-auto=update, so that Hibernate takes care of creating and updating the database for us. When defining properties as environment variables for Spring Boot, they have to be defined in snake case. Translating the property names to environment variable names gives us:
Environment variables can be set in the settings panel of your Heroku app.
The values for the database connection can be retrieved from the details of your Heroku database.
Connection Details of a Heroku Postgres Database
Now add Config Vars with the following values to your Heroku app:
where <Host>, <Port>, <Database>, <User> and <Password> can all be retrieved from the database panel at Heroku. That’s it! After you have deployed the application the next time, it will use the persistent Postgres database provided by Heroku and no data will be lost.