Combining Heroku and Dropwizard: My Own Personal Staging Environment
27 Jan 2013
Think what you may about Heroku, they do offer a free tier and it’s trivially simple to use it as your own personal staging environment. In this post, I’m going to outline what it takes to take a simple Dropwizard service and get it running in Heroku. For anyone looking for a bit of depth, this example will also include database access (Heroku also gives you a free PostgreSQL database) and touch on using Dropwizard’s Liquibase integration for database migrations.
For the record, I am not particularly for nor against Heroku as a platform. I believe it’s a great service that makes it almost too easy to get off the ground and into the continuous deployment frame of mind. I simply lack the experience using it at scale, opting instead for AWS, to comment on it’s applicability across all stages of an application.
Maybe I’m cheap (Heroku’s a few more dollars than the equivalent in AWS) or maybe I just really really enjoy (the pain of?) managing Chef and setting up my own VPCs.
Getting Started
First off, let’s get a simple Dropwizard project up and running.
$ git clone git://github.com/ajordens/dropwizard-example-groovy.git
Make sure it compiles and runs locally.
$ mvn clean install
$ java -jar my-example-service/target/my-example-service-0.1-SNAPSHOT.jar server my-example-service/config.yml
Lastly, create a simple PostgreSQL database and test database migrations.
$ createdb myexampleDB
$ java -jar my-example-service/target/my-example-service-0.1-SNAPSHOT.jar db migrate my-example-service/config.yml
Assuming everything worked successfully, you’ll now have a service capable of serving html views and json data as well as a simple database.
Setting up Heroku
If you have not already signed up for a free Heroku account, do so now.
You will also need to install the appropriate Heroku Toolbelt release for your operating system.
$ heroku login
Enter your Heroku credentials.
Email: example-app@xyz
Password (typing will be hidden):
Authentication successful.
$ heroku create example-app
Creating example-app… done, stack is cedar
http://example-app.herokuapp.com/ | git@heroku.com:example-app.git
Git remote heroku added
Almost done, all that’s left now is to get the Heroku database connection properties.
Update config-heroku.yml with the values listed in the PostgreSQL add-on section of your application (login to heroku.com)
Pushing Code to Heroku
$ git push heroku master
$ heroku ps:scale web=1
After pushing code to Heroku, it’s completely normal to see your maven build occur immediately.
When the build finishes, the app will be accessible via http://app-name.herokuapp.com
$ heroku open
This works in part due to the magical Procfile that I snuck into the dropwizard-example-groovy project.
The Procfile simply tells Heroku how to start the web application and because Dropwizard is a completely self-contained jar, it runs in Heroku using the same java -jar syntax that you would use to run locally.
Almost magical.
Migrating Databases on Heroku
Managing your Heroku databases is almost as simple as if they were running locally.
$ java -jar my-example-service/target/my-example-service-0.1-SNAPSHOT.jar db migrate my-example-service/config-heroku.yml
Want to drop everything.
$ java -jar my-example-service/target/my-example-service-0.1-SNAPSHOT.jar db drop-all –confirm-delete-everything my-example-service/config-heroku.yml
Using Git Flow?
If you’re accustomed to using git-flow, you will frequently want to push feature branches to Heroku.
It’s simple.
$ git push heroku feature/BRANCH_NAME:master
This will allow you to test and deploy a non-master branch in your staging environment.
Have further questions? I’ve been running Dropwizard apps in Heroku for awhile now, ping me via Twitter.