Cloudfoundry and MongoDB NoSQL sample application

Introduction: MongoDb NoSQL

Download the source code here.

As lot of you folks, I also had a question, what is cloud computing? I started googling and downloaded few tools and played with them and understood few concepts.

In this section I will be discussing about one of the key concepts of Cloud computing – Platform as a Service a.k.a PaaS a.k.a Cloud Platform. Basically a Cloud Platform provide development support in a local environment, and deployment into remote environment and the cloud platform will “introspect” to which webserver/which database the application has to run. Let me illustrate this with a diagram

MongoDb NoSQL

There are few leading players in this space Salesforce.com (Heroku, Force.com) and Microsoft (Azure). Recently VMware entered into this space with their own Cloudfoundry. Coming from Java background this is a good tool to understand the details of a Cloud Platform. It is well integrated with STS IDE and it is bit buggy, but there are workarounds.

This section discusses about,

  1. How Cloudfoundry Cloud Platform supports, WebServer and database
  2. How Cloudfoundry Cloud Platform helps in Database seeding
  3. Deploying to Cloud platform
  4.  ‘glue’ to inform the Cloud Platform about Webserver and Database
  5.  ‘glue’ to generate the dbschema and the Seed data

Details

I will walk you thru a simple example using Spring MVC and MongoDB, where you do a basic CRUD operation on person table. MongoDB is a Document based database, which is used to store large amount of data, this also has Map/Reduce capabilities similar to Hadoop.

To quickly start on this,

Cloudfoundry support:

WebServer and database support: Cloudfoundry Supports SpringSource tc Server, it also supports Jetty if used with Maven. Database side,It supports MySQL, vPostgres and MongoDB. It has the ability to introspect spring context file and understand which type of database application supports if you have a bean with type “org.apache.commons.dbcp.BasicDataSource” and it can bind it to the respective database. If it is MongoDB, it needs a mongo-db-factory as shown in this example.

Database seed data population: Typically if you have “jdbc:initialize-database” configuration in your application Cloudfoundry will execute that script in its bound database.

Configuration in the application:

Deploying to Cloudfoundry: There are 2 ways of deploying the code to Cloudfoundry, command line and STS IDE

If you want to deploy and Run the application using VMC command line, you need to do the following,

Move to the target folder
vmc target http://api.{instancename}.cloudfoundry.me

vmc push

give the application name as ‘spring-mongodb’

Bind to ‘mongodb’

Save configuration

Now open the browser and type ‘http://spring-mongodb.{instancename}.cloudfoundry.me/’

If you want to deploy and run the application from STS IDE, you need to do the following,

  • For setting up your STS to work with Cloudfoundry refer this link.
  • Import the maven project into you STS
  • Create a new Cloudfoundry Server and add the spring-mongodb application, and publish the application war to the Cloudfoundry host and see your changes.
  • You can also access the Remote System for error logs and files as mentioned in the SpringSource Blog mentioned above

Glue to inform the Cloudfoundry about the database:

The key changes you have to make in your application to work with Cloudfoundry,

  1. Maven changes
<!-- CloudFoundry -->

  org.cloudfoundry
  cloudfoundry-runtime
  ${org.cloudfoundry-version}

  1. Mongodb configuration

Glue to generate the dbschema and the Seed data in Cloudfoundry:

You can create a Bean called InitService with a method init, and add all the seed data that is needed for the application as below,

public class InitService {
 private MongoTemplate mongoTemplate;
 public MongoTemplate getMongoTemplate() {
   return mongoTemplate;
 }
 public void setMongoTemplate(MongoTemplate mongoTemplate) {
  this.mongoTemplate = mongoTemplate;
 }
private void init() {
Person p = new Person ();
 p.setId(UUID.randomUUID().toString());
 p.setFirstName("John");
 p.setAge(25);
 mongoTemplate.save(p);
 p = new Person ();
 p.setId(UUID.randomUUID().toString());
 p.setFirstName("Jane");
 p.setAge(20);
 mongoTemplate.save(p);
 ..
}

In the bean context, define a bean as below,


Other than these changes, everything else is same as any other Spring MVC application.

Way forward, you can get the Cloudfoundry samples.  Using Git utlitiy you can clone this in your local machine. There is a simple hello-spring-mysql sample; you can quickly understand how MySQL based application works using this example.

About these ads

One thought on “Cloudfoundry and MongoDB NoSQL sample application

  1. Ranbir Oberoi

    SpringOne 2012 is a one-of-a-kind conference for application developers, solution architects, web operations and IT teams who develop business applications, create multi-device aware web applications, design cloud architectures, and manage high performance infrastructure.
    For more details about the event visit: http://bit.ly/Ungkqd
    SpringOne Developer Conference is happening in India for the first time in Bangalore and Hyderabad in the month of December, 2012. The sessions are specifically tailored for developers using the hugely popular open source Spring technologies and Tomcat.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s