Introducing Spring Roo, Part 4
Rapid application development in cloud with Spring Roo and Cloud Foundry
This content is part # of # in the series: Introducing Spring Roo, Part 4
This content is part of the series:Introducing Spring Roo, Part 4
Stay tuned for additional content in this series.
In Part 1 and Part 2 of this series on Spring Roo, we built a full-fledged enterprise application using Spring Roo. At that time, I planned to write an article on Spring Roo and Google App Engine Platform as a Service (PaaS) integration. Spring Roo provides support for building web applications that can be deployed on Google App Engine, but there were issues and limitations making it difficult to deploy the application that we previously built. I decided to postpone that article until Spring Roo/Google App integration support improves. This article introduces Cloud Foundry (open platform as a service) and how to use it to deploy a sample application. Before discussing Cloud Foundry, let's take a moment and talk about platform as a service.
What is platform as a service?
Platform as a service (PaaS) is one of the layers of Cloud Computing. Other layers include software as a service (SaaS) and infrastructure as a service (IaaS). PaaS sits between SaaS and IaaS such that it provides abstraction over infrastructure, and provides a computing platform and a solution stack.
Figure 1. Platform as a service: The strategic center of cloud-computing architecture
PaaS is suitable for both business and professional developers. Business developers who don't write code can use platforms like WaveMaker to develop their applications with drag and drop components, and professional developers can use platforms like Google App Engine to build and deploy web applications. PaaS provides the facilities to support the end-to-end life cycle of building and delivering web applications and services. Some of the major PaaS providers are Google App Engine for Java™ and Python, Microsoft® Azure for .Net and Java, and Force.com.
Until 2010, PaaS had not grown at the same rate as SaaS and IaaS, possibly due to the limitations imposed by existing PaaS solutions. Let's take a look at issues with existing PaaS, which next generation PaaS platforms such as Cloud Foundry should resolve.
Issues with existing PaaS Solutions
- Vendor lock-in (lack of cloud portability): No option was given to a developer or an organization to move from one cloud to another. For example, if a developer chose Google App Engine, then they must remain with Google App Engine unless the application is deleted using the Google App Engine console and the existing application is modified according to the new cloud provider. The application must then be deployed there.
- Deployment to public cloud only: Most of the existing PaaS solutions target only public cloud, which results in many enterprises not moving to PaaS. There are numerous enterprises that will never move to the public cloud, and they were given no choice to set up private cloud on their infrastructure or use a combination of private and public cloud.
- Stagnant mindset: Developers have to change their mindset for building applications on the public cloud. Previously, Google App Engine did not provide the option of relational databases. The application had to be coded against BigTable, which requires a change in mindset before building begins, and there is a learning curve associated with it. The developer can take an application WAR file and deploy to the cloud without any modifications.
- Support for diluted frameworks: Currently, Java programmers use frameworks, and less than 100 percent framework support introduces difficulty when writing applications. Using Google App Engine to build a simple application using any common framework like Spring or Struts required a few hacks before using that framework.
- Tied to a particular environment: The existing PaaS offering is tied to particular environments such as Java and Python using BigTable as back end for Google App Engine, .Net and Java using SQL Azure database for Microsoft Azure, or Java for Amazon Beanstalk. Using any of these PaaS requires survival in that environment only.
- Not open-source: No existing PaaS solutions are open-source, making it difficult for developers to fork the code and add support for new features to share with the community. A compelling element of Cloud Foundry is its open-source nature, allowing developers outside the parent organization a major role in its success.
2011 – The year of PaaS
Gartner said that 2011 will be the year of platform as a service. This will be due to large numbers of new PaaS offerings being introduced by leading enterprise software vendors. Some PaaS offerings launched this year are Cloud Foundry by VMWare, OpenShift by RedHat, CloudBees, and DotCloud. (See Related topics for more information on all of these.) The new PaaS offerings are not restricted to a single language or framework. Now PaaS is truly becoming a platform to support building applications using multiple languages and frameworks. PaaS offerings like Cloud Foundry have gone to the next level in a way that also supports multiple clouds, which we discuss later in this article. These changes will also force existing PaaS solutions to become multi-framework. The following focuses on Cloud Foundry, as the rest of the PaaS offerings are not within the scope of this article.
What is Cloud Foundry?
In April 2011, VMWare unveiled an open platform as a service called Cloud Foundry. Cloud Foundry is a multi-language, multi-framework, multi-application service, and multi-cloud open-source platform. It is trying to resolve all of the previously listed issues for the existing PaaS solutions. Developers or organizations can use Cloud Foundry as the deployment destination, that is, to deploy to a public cloud offered by Cloud Foundry, or it can be used as a layer allowing deployment to a choice of cloud, both public and private. The three key characteristics of Cloud Foundry are:
- Choice at all layers: In a single line, Cloud Foundry
is a PaaS-providing option. The choices given to developers are not
limited to programming languages, but exist at all the layers
(services and cloud). Cloud Foundry is best depicted by the diagram in
Figure 2. Cloud Foundry choices
In Figure 2 you can see choices at all three vertices of a triangle. The first vertex is for languages/runtime and frameworks. Currently, Cloud Foundry support Java, Groovy, Scala, Ruby, and Node.js languages. The supported frameworks for these languages are Rails3, Grails, Node, Sinatra, Lift, and Spring. The second vertex corresponds to application services with services available out of the box such as MySQL (Relational Database), Redis (Key value datastore), and MongoDB (document datastore). Support for RabbitMQ and other services will be coming soon. Cloud Foundry is giving developers the choice to work with SQL or NoSQL databases. Those limited to MySQL and Java do not have to learn anything new, and may use existing knowledge to build an application or deploy an existing application. The third vertex shows choices of clouds. Cloud Foundry can be deployed on a public cloud like the one available currently on cloudfoundry.com or by deploying Cloud Foundry on IaaS like Amazon EC2. You can also download and deploy Cloud Foundry on your organization's private cloud, or a developer can install Cloud Foundry on a laptop to use as a micro cloud or developer cloud.
- Open source: Cloud Foundry is an open source project available on Github (see Related topics). Developers can fork the Cloud Foundry project, add support for new features or fix existing issues, and contribute back to the project. One company called ActiveState has built up its own cloud platform for Python and Perl on Cloud Foundry (see Related topics). This is only possible because Cloud Foundry is an open source project and developers can add more services or language support to it.
- Cloud portability: As Cloud Foundry is an open source project, developers or organizations have liberty to run Cloud Foundry wherever they wish. RightScale showed that Cloud Foundry can run on Amazon EC2 (see Related topics). Those not wishing to use the default Cloud Foundry public cloud can switch to a different cloud offering by Cloud Foundry.
Getting started with Cloud Foundry
Cloud Foundry public cloud is offered as a free service at http://www.cloudfoundry.com/. The service is currently in beta. Those without Cloud Foundry credentials will have to register for an account before beginning with Cloud Foundry public cloud. It usually takes a couple of days to receive the Cloud Foundry credentials. The public cloud is currently running on VMWare vSphere cloud operating system. To get started before receiving the credentials, set up a cloud on an individual machine by downloading and installing Cloud Foundry.
Once the Cloud Foundry credentials are received, there are three ways to interact with it. All of the clients interact with REST API exposed by Cloud Foundry. These REST web services are simple json payload over HTTP.
- VMC Command-line client: VMC is a ruby gem providing access to Cloud Foundry through command-line. It is used to deploy applications created using all languages. Download it from http://rubygems.org/gems/vmc.
- Spring Source Tool Suite (Spring Eclipse IDE or STS): STTS is the second mode to connect to Cloud Foundry. STS is an Eclipse-based IDE which comes bundled with many Spring goodies. To learn more about STS, refer to an excellent refcard published on dzone. Installation of a plugin is necessary to access Cloud Foundry. From within the IDE, you can do all build, test, and deploy to a cloud. That is really cool! Read more about it from a blog by SpringSource team.
- Spring Roo Cloud Foundry add-on: The application can be deployed from within Roo shell using Spring Roo Cloud Foundry add-on. For Roo shell aficionados, it means we can build and deploy from within Roo shell. Another advantage offered by Roo shell is tab completion. Add a few letters to the command, press tab, and Roo shell completes the command.
Now let's discuss how Java developers can access Cloud Foundry through Spring Roo. We will deploy the application created during the first two articles to Cloud Foundry.
Spring Roo Cloud Foundry integration
Spring Roo provides support for Cloud Foundry in the form of an add-on. Add-ons are a mechanism by which Spring Roo provides support for new features.
To deploy your first application with Spring Roo on Cloud Foundry follow these steps:
- If Spring Roo is not installed on your machine, please refer to Part 1 article to get started with Spring Roo. I am using the latest version of Spring Roo 1.1.5.RELEASE.
- Download the source code of the conference application, see Download. This is the same application developed in the first two articles. This is currently using HSQLDB as database. We will port this application to MySQL database later.
Deploy simple HSQLDB application on Cloud Foundry
When the development environment is ready, deploy the conference application. The downloaded conference application uses HSQLDB as its database. To deploy this application to Cloud Foundry, follow these steps:
- Unpack the conference.zip to a suitable location.
- From the command-line, go to the location where conference.zip was unpacked.
- Build the project by typing mvn clean install on the command-line.
- Once in the conference directory, fire the Roo command to load the shell.
- Before deploying the application on Cloud Foundry, install the Cloud
Foundry add-on, which will give the commands to perform operations on
Cloud Foundry. To install the Cloud Foundry add-on, type the following
commands on Roo shell:
roo> pgp automatic trust roo> addon install bundle --bundleSymbolicName org.springframework.roo.addon.cloud.foundry;1.1.5.RELEASE
The first command tells Spring Roo to automatically trust all the pgp keys so that all the required add-ons are installed without needing to trust individual trust keys. The second command installs the 1.1.5 RELEASE version Cloud Foundry add-on. This command will take couple of minutes to complete, so please remain patient.
- After the add-on is installed, logging in to Cloud Foundry is
necessary before operations can be performed. To log in to Cloud
Foundry, type the following command on Roo shell:
roo> cloud foundry login --email email@example.com --password ****** --cloudControllerUrl http://api.cloudfoundry.com
The cloud foundry login command takes three options – email, password, and cloudControllerUrl. Out of these, email and password are mandatory. The cloudControllerUrl value defaults to Cloud Foundry public cloud at http://api.cloudfoundry.com. To deploy to micro cloud or cloud anywhere else, it is necessary to point to that URL.
- To view all the commands available to access Cloud Foundry, write
cloud foundry and press tab to will see all the commands.
roo> cloud foundry <Press Tab> cloud foundry bind cloud foundry clear cloud foundry create cloud foundry delete cloud foundry deploy cloud foundry files cloud foundry info cloud foundry list cloud foundry login cloud foundry map cloud foundry restart cloud foundry start cloud foundry stop cloud foundry unbind cloud foundry unmap cloud foundry update cloud foundry view
- To view the information associated with your Cloud Foundry instance,
type the info command as shown below. This command shows information
about memory usage, number of applications deployed on Cloud Foundry,
and number of services running. It also shows the maximum alloted
values, that is, maximum memory is 2GB.
roo> cloud foundry info VMware's Cloud Application Platform For support visit firstname.lastname@example.org Target: http://api.cloudfoundry.com (0.999) User: email@example.com Usage: Memory (0MB of 2048MB total) Services (0 of 16 total) Apps (0 of 20 total)
- Next, deploy the application by typing cloud foundry deploy command.
This command requires two required attributes appName and path. The
appName attribute represents the name of the application, and path
attribute represents the path to the WAR file. It is not necessary to
enter the path. Simply press tab after path and Spring Roo will find
that for you. There are three non-mandatory attributes – instances,
memory, and urls. By default, only one instance of the application is
launched, but deployment of multiple instances of the application is
possible by specifying it using the instances attribute. The default
memory allocated to an instance in 256MB. To change this attribute,
use the memory attribute at the time of deployment. The URLs attribute
can be used to specify the URL to be mapped to the application. By
default the URL will be <Application Name>.cloudfoundry.com,
that is, conference.cloudfoundry.com for our application. Type the
following command to deploy the conference application
roo> cloud foundry deploy --appName conference --path /target/conference-0.1.0.BUILD-SNAPSHOT.war
The path attribute might have value CREATE, which will depend on whether the project was built using Maven. If the project is already built, as in my case, you will see the path of war. Otherwise, you will see CREATE, which will first build the project and then deploy the application.
- The deployed application is not yet started. Start the application
using start command as shown below.
cloud foundry start app --appName conference
Switching to production: Replace HSQLDB with MySQL database
It is easy to switch from HSQLDB for a local environment to MySQL database in production. First, change the persistence store from HSQLDB from MySQL using Roo shell, and then create a MySQL service using Cloud Foundry add-on and bind the service to the application. It is not necessary to change the code; the only change that will happen will be in configuration. It isn't even necessary to have MySQL database on the machine as test is done directly in cloud. Cloud Foundry feels as light as developing on a local developer machine.
- Open the Roo shell and change the persistence store from HSQDB to
MySQL by typing the following command on the shell.
persistence setup --database MYSQL --provider HIBERNATE
This command will update the pom.xml, database.properties, persistence.xml to point to MySQL related configuration.
- Open another command-line and execute the mvn clean install -Dmaven.test.skip=true in the root folder. Please note that I am skipping tests because I don't have MySQL database for my conference application.
- After the build is successful, the application is ready to be
deployed. Before deploying, however, please delete the application so
that we can create a MySQL service for our application and re-deploy a
new application. It is not necessary to delete the application each
time you make changes as you can also use the update command to update
cloud foundry delete app --appName conference
- Now we will deploy our newly created war which has MySQL specific
cloud foundry deploy --appName conference --path /target/conference-0.1.0.BUILD- SNAPSHOT.war --memory 512MB
- The above command deployed the application but the application
instance has not just started. So before we start the application we
need to create a MySQL service and bind it to our service. To create a
new service we will use the Cloud Foundry create service command and
will give it the serviceName and serviceType. The serviceType can be
MySQL, MongoDB, or Redis.
cloud foundry create service --serviceName conference-db --serviceType mysql
After creating the service we can bind the service by using bind service command. The bind service command makes sure that MySQL datasource dependency is satisfied in the cloud. This is also called dependency injection in the cloud.
cloud foundry bind service --appName conference --serviceName conference-db
We can also avoid the bind service command by specifying the appName attribute of create service command.
cloud foundry create service --serviceName conference-db --serviceType mysql --appName conference
- Now you can start the conference application using the start command.
cloud foundry start app --appName conference
- Finally, view the list of applications and services by executing the
Cloud Foundry list apps and list services commands as shown below.
roo> cloud foundry list apps ================================= Applications ================================= Name Status Instances Services URLs ---- ------ --------- -------- ---- conference STARTED 1 conference-db conference.cloudfoundry.com roo> cloud foundry list services ================== System Services =================== Service Version Description ------- ------- ----------- mongodb 1.8 MongoDB NoSQL store redis 2.2 Redis key-value store service mysql 5.1 MySQL database service = Provisioned Services = Name Service ---- ------- conference-db mysql
In this article, we defined Cloud Foundry and how it is different from other PaaS offerings. Cloud Foundry integration with Spring Roo provides a rapid application development environment for developers to build and deploy Spring applications from the Roo shell. This article discussed creating a MySQL service and binding it to an application.
In future articles, we will look at Cloud Foundry other services such as Redis and MongoDB.
- Be sure to read the rest of this Spring Roo series:
- Part 1: Building from source
- Part 2: Developing an application with Spring Roo
- Part 3: Developing Spring Roo add-ons
- Part 5: Write advanced and wrapper Spring Roo add-ons
- Part 6: Develop Spring MVC and GWT apps using Spring Roo 1.2 and deploy them on Cloud Foundry
- Part 7: Develop Spring MongoDB Applications using Spring Roo
- Get access to the public cloud by registering at the Cloud Foundry website.
- RightScale created an all-in-one server template that launches Cloud Foundry in one server on Amazon EC2.
- Find out more about Spring Roo on the project website
- Cloud Foundry is available through Github.