Spring Hello MongoDB Sample Application

markpollack edited this page Apr 15, 2011 · 9 revisions

Getting started with Cloud Foundry and Spring MongoDB

The sample application contains a single Spring MVC controller and view that creates a 'Person' object in the database for each page view and show the list of people in a list. You can also delete the people stored in the database.

You can see it running on Cloud Foundry http://hello-spring-mongodb.cloudfoundry.com/

This guide uses the command line interface (CLI) to deploy the "Spring Hello MongoDB" sample application that is hosted in this github repository.

Prerequisites

Please see the Prerequisites for Sample Applications page for more information.

Download sample application

Clone the git repository git@github.com:SpringSource/cloudfoundry-samples.git or via https using https://(user-name-here)@github.com/SpringSource/cloudfoundry-samples.git

>git clone git@github.com:SpringSource/cloudfoundry-samples.git

Cloning into cloudfoundry-samples...
remote: Counting objects: 3214, done.
remote: Compressing objects: 100% (2526/2526), done.
remote: Total 3214 (delta 662), reused 2419 (delta 408)
Receiving objects: 100% (3214/3214), 8.35 MiB | 1.41 MiB/s, done.
Resolving deltas: 100% (662/662), done.

Build sample application

>cd cloudfoundry-samples\hello-spring-mongodb

>mvn package

[INFO] Building war: L:\projects\cloudfoundry-samples\hello-spring-mongodb\target\hello-spring-mongodb.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------

Deploy sample application

The following is the transcript from running the 'push' command to deploy the application. You need to enter 'n' to deploy from a different directory.

Enter 'target' for the deployment path. For the application name we are going to use "hello-spring-mongodb" but you should change it to something different.

Hit 'enter' to accept default answers up until you are asked 'Would you like to bind any services to 'hello-spring-mongodb'. Then enter 'Y' and select the mongo service and name it 'mongodb-hello'

>vmc push --no-start
Would you like to deploy from the current directory? [Yn]: n
Please enter in the deployment path: target
Application Name: hello-spring-mongodb
Application Deployed URL: 'hello-spring-mongodb.cloudfoundry.com'?
The system cannot find the path specified.
Detected a Java SpringSource Spring Application, is this correct? [Yn]: y
Memory Reservation [Default:512M] (64M, 128M, 256M, 512M, 1G or 2G)
Creating Application: OK
Would you like to bind any services to 'hello-spring-mongodb'? [yN]: y
The following system services are available::
   1. mongodb
   2. mysql
   3. redis
Please select one you wish to provision: 1
Specify the name of the service [mongodb-9023b]: mongodb-hello
Creating Service: OK
Binding Service: OK
Uploading Application:
The system cannot find the path specified.
Checking for available resources: OK
Processing resources: OK
Packing application: The system cannot find the path specified.
  OK
Uploading (2K): OK
Push Status: OK

Now if you list the applications you will see the newly created one

>vmc apps

+----------------------+----+---------+---------------------------------------+---------------+
| Application          | #  | Health  | URLS                                  | Services      |
+----------------------+----+---------+---------------------------------------+---------------+
| hello-spring-mongodb | 1  | STOPPED | hello-spring-mongodb.cloudfoundry.com | mongodb-hello |
+----------------------+----+---------+---------------------------------------+---------------+    

Start your application

You can now start you app

>vmc start hello-spring-mongodb

Staging Application: OK
Starting Application: OK

and query for its status

>vmc apps

+----------------------+----+---------+---------------------------------------+---------------+
| Application          | #  | Health  | URLS                                  | Services      |
+----------------------+----+---------+---------------------------------------+---------------+
| hello-spring-mongodb | 1  | RUNNING | hello-spring-mongodb.cloudfoundry.com | mongodb-hello |
+----------------------+----+---------+---------------------------------------+---------------+

View your application on the web

Now browse to the URL .cloudfoundry.com, in this case http://hello-spring-mongodb.cloudfoundry.com

You will see a web page that lists a single 'Person' object in a bulleted list. Each time you refresh the page a new person is created.

The controller code that does this is simply

Random generator = new Random();
Person p = new Person("Joe Cloud-" + generator.nextInt(100), generator.nextInt(100));
mongoTemplate.save(p);

If you have problems deploying.

Refer to the Getting Started FAQ for more information