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”.
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.
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:
spring.datasource.url spring.datasource.user spring.datasource.password spring.datasource.driver-class-name spring.jpa.database-platform
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:
SPRING_DATASOURCE_URL SPRING_DATASOURCE_USER SPRING_DATASOURCE_PASSWORD SPRING_DATASOURCE_DIRVER-CLASS-NAME SPRING_JPA_HIBERNATE_DDL-AUTO SPRING_JPA_DATABASE-PLATFORM
Now add Config Vars with the following values to your Heroku app:
SPRING_DATASOURCE_URL=jdbc:postgresql://<Host>:<Port>/<Database> SPRING_DATASOURCE_USER=<User> SPRING_DATASOURCE_PASSWORD=<Password> SPRING_DATASOURCE_DIRVER-CLASS-NAME=org.postgresql.Driver SPRING_JPA_HIBERNATE_DDL-AUTO=update SPRING_JPA_DATABASE-PLATFORM=org.hibernate.dialect.PostgreSQLDialect
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.