Saturday, May 5, 2012

Deploy Spring MVC application to cloud foundry

In my previous post I've described how to deploy existing spring mvc application to heroku cloud. I've attended cloud foundry open tour in Portland since then. I'll write up here what it takes to deploy a very simple, spring-mvc web application to cloud foundry platform.

Cloud foundry has several interesting features which distinguish it from other cloud offerings, such as micro cloud which allows one to run a cloud instance on a local box. Additionally cloud foundry platform is not limited to vmware offerings - it can be installed, and run in internally hosted data centers. The best part, for me at least, is that it currently provides very generous free offerings for devs  which I'm about to tap to deploy my dummy app to vmware hosted implementation. Time permitting I can build some features into the application to explore some freely available services such as mongo, solr, etc.. in the future.

Getting started with cloud foundry
Getting started is fairly simple. Personally, the single wrinkle in the process was the fact that I had to wait about 24 hours to be approved and granted access to the platform following registration process. Here are getting started steps
1. Register for a free account here
2. Cloud foundry comes with excellent command line tool support (vmc). The installation procedure varies between the OS's. Follow instructions specific to the OS here to install, configure vmc and log into cloud foundry environment.
Once the tool is installed and configured it can be used to manage applications in cloud foundry platform. To see full list of supported commands type vmc -help in terminal prompt after installation.

Installing application in the cloud foundry
I was deploying a very simple spring-mvc web application built with maven. The application is available for download on github. I listed detailed steps on how to run application locally in one of my previous blogs. Here are the steps to deploy the application to cloud foundry platform.

1. Verify application with maven command
       mvn clean verify
2. cd into application target directory NOTE:Personally I found it a bit non standard to have to cd into the target directory, maybe, because I'm used to work with maven where all the commands are executed on the app root level

3. Install application with vmc push command
       vmc push
4. Respond to several questions at prompts:
      Would you like to deploy from the current directory? [Yn]: Y
Define application name (I made it to match url for simplicity)
      Application Name: pio-spring-inter-sample
Next prompt is application url. make sure that there are no periods (.) in url as it didn't work for me - this might be fixed by now though
Application Deployed URL: ''? 
Detected a Java SpringSource Spring Application, is this correct? [Yn]: Y
Define is the memory reservation for the application. I think the free offering cap at 2Gb. This is a very small app thus 256M is more than enough (I'll save the rest of capacity for cooler stuff that will come later).
Memory Reservation [Default:512M] (64M, 128M, 256M, 512M, 1G or 2G) 256M
Creating Application: OK
Enter N on services prompt as the application doesn't require services (at least for now).
Would you like to bind any services to 'pio-spring-inter-sample'? [yN]: N
Once the last prompt has been entered vmc will list all steps it performs to deploy and start the application
Uploading Application:
  Checking for available resources: OK
  Processing resources: OK
  Packing application: OK
  Uploading (5K): OK   
Push Status: OK
Staging Application: OK                                                         
Starting Application: OK
Once this process completes the application is accessible under the url provided in one of the steps before. In my case it is

Maven plugin support

There is a maven plugin available which allows to deploy the application as a part of maven build. I will not go to details describing it's usage as it's been described in this blog post. With the help of maven plugin applying changes can be a part of a successful maven build cycle which updates, restarts application as a part of successful build.
   mvn clean verify cf:update cf:restart