Tuesday, March 5, 2013

Publishing Java to Heroku Cloud Service

Heroku is a jvm-based cloud service. They have scaling appliations and many of the features you expect to see in the cloud. The big thing for me is that it is simple setup, simple to understand, simple billing and lastly - simple to deploy to. Since I am very big on minimalism - Heroku is a huge win in the jvm based cloud arena.

You actually publish to the Heroku Cloud using git with just a handful of commands once you get the initial setup out of the way. Since the modern jvm culture loves git, using git to publish is very natural for jvm development.

Prerequisites


  1. Your project must be built via mvn. This can be done outside your ide of choice so won’t impact any notion of project files your ide may have.
  2. You need git. git is a version control system and has versions for all the major operating systems..  If you are running linux you probably already have it. If you need to install it, go here. http://git-scm.com/downloads
  3. You need a heroku account. http://heroku.com



Setup

  1. Install heroku Toolbelt to get Foreman, the Heroku command line client.
    1. https://toolbelt.heroku.com/
  2. Create a .Procfile.
    1. This file contains the command you want Heroku to execute immediately after the deploy is complete. So this is your java -jar command. Be sure to include any required parameters. This file has a single line:
      1. web: java -jar target/sebsApi-1.0.jar server configuration.yaml
    2. Heroku uses the Celadon Ceadar stack to manage jvm polyglot apps. That’s what the ‘web:’ flag is. Find more info here: https://devcenter.heroku.com/articles/cedar
    3. This file should be in your application’s root along side the pom.
  3. Create a system.properties file.
    1. This file is a a typical java properties file that describes unique config information for your app. This is where you can specify the java version:
      1. java.runtime.version=1.7
    2. This file should be in your application’s root, along side the pom.
  4. Store the app in locally in Git (if not done already).
    1. git init
    2. git add .
    3. git commit -m “some message”
  5. Add the Git remote for Heroku
    1. If you do not already have a heroku app, you need to create it.
      1. heroku create
      2. git remote -v
    2. If you already have the app created (available in the Heroku app dashboard) and you need to add a your Git local to the Heroku remote:
      1. heroku git:remote -a <app name>

Publish


  1. git push heroku master
  2. If you are new to git, be sure to commit before pushing. Any changes to local files must be committed to the local git repository before they can be pushed to the remote.

Verify

  1. Scale up a single web process (Dyno).
  2. Heroku calls their processes Dynos. You must have at least one running for your app to be available.
    1. heroku ps:scale web=1
  3. Check the status of the running Dyno:
    1. heroku ps
  4. View the website in a browser:
    1. heroku open

Other useful infos

Viewing the logs: heroku logs

Heroku assigns a port to the app. I don’t know the logic behind how it selects the port, but it is likely that it will be different every time you deploy. This makes sense when you think about scaling and multiple Dynos to support your app. So you will need to pass in an environment variable for the port as parameter in the Procfile if your app supports it:

In my case, for DropWizard:
web: java $JAVA_OPTS -Ddw.http.port=$PORT -Ddw.http.adminPort=$PORT -jar -jar target/sebsApi-1.0.jar server configuration.yaml






References

https://devcenter.heroku.com/articles/java#deploy-your-application-to-heroku
https://devcenter.heroku.com/articles/cedar
http://gary-rowe.com/agilestack/2012/10/09/how-to-deploy-a-dropwizard-project-to-heroku/