Blogs

SpringSource Blog

Using Postgres on Cloud Foundry

Thomas Risberg

When the new open source Platform-as-a-Service (PaaS) offering Cloud Foundry from VMware launched earlier this year, it included a relational database service powered by MySQL along with the NOSQL options of MongoDB and Redis. One of the promises of the Open PaaS is to provide choice both in languages and frameworks you can develop with and in the database services that are available to use. We now have a new relational database service using PostgreSQL available. This is great since we can now choose between the two most popular open source relational databases. PostgreSQL is is a very robust and reliable database that has been around for a long time so it definitely has been battle tested.

vFabric Postgres on Cloud Foundry

The new PostgreSQL service is an excellent option for our relational database needs. PostgreSQL was originally developed and optimized to run on physical machines. Now that we are running our databases in the cloud it makes sense to optimize them for that environment. This is exactly what VMware has done for Postgres by creating a version that is optimized for the virtualized cloud environment. To power the PostgreSQL service on cloudfoundry.com the Cloud Foundry team is using vFabric Postgres 9.0, a vSphere-optimized version of Postgres. The vFabric Postgres product is part of the recently announced vFabric Data Director, which is a new database provisioning and operations solution designed to deliver a Database-as-a-Service model for the enterprise. The first database supported on Data Director is vFabric Postgres.

So, is there any difference between vFabric Postgres and regular PostgreSQL from a developers perspective? No, they are functionally identical. You use the same JDBC driver and SQL syntax. The changes made are internal and related to delivering the elasticity and performance required for the cloud.

Building a Book Shelf sample app with Roo

To get started using PostgreSQL on Cloud Foundry you first need an account on Cloud Foundry. Once that is taken care of we can start developing our first database application. The fastest way to write a Spring application is by using Spring Roo which is Spring’s rapid application development tool for Java developers. This of course means that you need to install Roo and the Cloud Foundry add-on.

Now that we have all the prerequisites in place we can get started. First create a directory for the application and open the Roo shell. Once that is opened we can create our project.

roo> project --topLevelPackage org.springsource.data.demo.bookshelf

Now we are ready to configure the persistence options for our Roo app. I'll select Hibernate as the JPA provider and Postgres as the database. There is no need to provide any custom connection properties since we will be running this application in Cloud Foundry. The connection details are automatically managed for us.

org.springsource.data.demo.bookshelf roo> persistence setup --provider HIBERNATE --database POSTGRES

Next we need to create our Entity class for this brief example. I'll create a Book class that will be part of my new BookShelf application. I'm just going to create the Book class for now and will add the Author and any other classes some time in the future.

org.springsource.data.demo.bookshelf roo> entity --class ~.domain.Book
~.domain.Book roo> field string --fieldName title --sizeMax 200
~.domain.Book roo> field string --fieldName isbn --sizeMax 20
~.domain.Book roo> field date --fieldName published --type java.util.Date
~.domain.Book roo> field number --fieldName price --type java.math.BigDecimal

Once this is taken care of we are ready to create the web application with a controller for our Book class.

~.domain.Book roo> controller all --package ~.web

We are done, pretty painless if you ask me. Now we need to package everything up, connect to Cloud Foundry and deploy the app.

~.web roo> perform package
~.web roo> cloud foundry login
~.web roo> cloud foundry deploy --appName bookshelf --path /target/bookshelf-0.1.0.BUILD-SNAPSHOT.war

Once the deploy completes we should be able to list our current set of applications

~.web roo> cloud foundry list apps

================================================ Applications ================================================

Name                      Status      Instances     Services             URLs
----                      ------      ---------     --------             ----
bookshelf                 STOPPED     1                                  bookshelf.cloudfoundry.com

Connecting the app to PostgreSQL on Cloud Foundry

Now we get to the fun part. As you saw above, the application is in a stopped status. We can't start it now since we have not created our database and bound it to the application yet. So let's do that now, but first let's see what data services are available.

~.web roo> cloud foundry list services

=================== System Services ====================

Service        Version     Description
-------        -------     -----------
rabbitmq       2.4         RabbitMQ messaging service
mongodb        1.8         MongoDB NoSQL store
redis          2.2         Redis key-value store service
postgresql     9.0         PostgreSQL database service (vFabric)
mysql          5.1         MySQL database service

Great, we do have PostgreSQL available as an option. So, let's create a database service instance, bind it to the app and start the app.

~.web roo> cloud foundry create service --serviceName books --serviceType postgresql
~.web roo> cloud foundry bind service --serviceName books --appName bookshelf
~.web roo> cloud foundry start app --appName bookshelf

Let's see what we get when visiting http://bookshelf.cloudfoundry.com

bookshelf screen

So the app is up and running and we can add and view the books on our bookshelf.

More about PostgreSQL on Cloud Foundry

We saw in the example above that we can connect our applications to PostgreSQL. But how do we know if we are running against a regular PostgreSQL install or vFabric Postgres? One way of determining this is to examine the output of the version() function. It typically says something like "PostgreSQL 9.0.4 on x86_64-pc-linux-gnu, compiled by GCC gcc-4.4.real (Ubuntu 4.4.3-4ubuntu5) 4.4.3, 64-bit". Since the Postgres database running on cloudfoundry.com is based on vFabric Postgres 9.0 we actually see a return value saying "[PostgreSQL 9.0.4 ] vPostgres 1.0 release-v build". Let's see if we can easily add a page to our web app to display some database information. I'll create a DatabaseInfo controller that I can modify.

~.web roo> controller class ~.web.DatabaseInfoController

This creates a controller (bookshelf/src/main/java/org/springsource/data/demo/bookshelf/web/DatabaseInfoController.java) that I've added a DataSource and some database info retrieval code to in the index method.

@RequestMapping("/databaseinfo/**")
@Controller
public class DatabaseInfoController {

   @Autowired
   DataSource dataSource;

    @RequestMapping
    public void get(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    }

    @RequestMapping(method = RequestMethod.POST, value = "{id}")
    public void post(@PathVariable Long id, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    }

    @RequestMapping
    public String index(ModelMap modelMap) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    	String userInfo = jdbcTemplate.queryForObject("select user", String.class);
    	String urlInfo = "?";
    	if (dataSource instanceof BasicDataSource) {
    	    urlInfo = ((BasicDataSource) dataSource).getUrl();
	}
	String versionInfo = jdbcTemplate.queryForObject("select version()", String.class);
        modelMap.put("userInfo", userInfo);
        modelMap.put("urlInfo", urlInfo);
        modelMap.put("versionInfo", versionInfo);
        return "databaseinfo/index";
    }
}

There is also a JSP file (bookshelf/src/main/webapp/WEB-INF/views/databaseinfo/index.jspx) that is used by this controller and that needs to have some code added.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" version="2.0">
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <jsp:output omit-xml-declaration="yes"/>
  <spring:message code="label_databaseinfo_index" htmlEscape="false" var="title"/>
  <util:panel id="title" title="${title}">
    <spring:message code="application_name" htmlEscape="false" var="app_name"/>
    <h3>
      <spring:message arguments="${app_name}" code="welcome_titlepane"/>
    </h3>
    <p>  The database user is ${userInfo}. </p>
    <p>  The dataSource URL is ${urlInfo}. </p>
    <p>  The database version is ${versionInfo}. </p>
  </util:panel>
</div>

Now we can build and deploy the modified application to the cloud.

~.web roo> perform package
~.web roo> cloud foundry deploy --appName bookshelf --path /target/bookshelf-0.1.0.BUILD-SNAPSHOT.war

Accessing the application we can see a new link to the Database Info Controller View that returns the following page:

bookshelf db info

As you can see we are using vFabric Postgres in our cloud deployed app.

Can I run vFabric Postgres in my own server or private cloud?

Yes, you can get vFabric Postgres for development or production. You will also need the companion product vFabric Data Director to manage the database instances. vFabric Data Director is a software solution that enables you to provide Database-as-a-Service (DBaaS) for your cloud. It is designed for the cloud environment and is capable of managing thousands of databases while offering application developers self-service database management.

See Jignesh Shah's blog for more info on this new product.

 

Similar Posts

Share this Post
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • LinkedIn
  • Slashdot
  • Technorati
  • TwitThis
 

24 responses


  1. Doe the vFabric PostgreSQL installation support PostGIS (http://postgis.refractions.net)?


  2. Great work, … I'm also very interested in PostGIS extension support – is it available for now or is it planned to be supported in near future?


  3. Great work there,Very much interested to see more and detailed extension support on PostGIS. http://webinarkings.com/


  4. Quality products

    good work.http://www.crusher.so


  5. Noch Mehr zum Thema wäre super.


  6. Your point of view very creative, very exciting article.


  7. Your point of view very creative, very exciting article.


  8. Thanks for writing this post.I think it'll be helpful to me


  9. Your point of view very creative, very exciting article.


  10. Tom,

    Is the Cloud Foundry addon available for Roo 1.2? It is not in the default install for 1.2.M1.

    Regards,
    Gordon Dickens


  11. Nike produces a wide range of sports equipment. Their first products were track running shoes. They currently also make shoes, jerseys, shorts, baselayers, etc. for a wide range of sports including track and field, basketball,ice hockey, tennis, association football (soccer), lacrosse, basketball, and cricket. Nike Air Max is a line of shoes first released by Nike, Inc. in 1987. The most recent additions to their line are the Nike 6.0, Nike NYX, andNike SB shoes, designed for skateboarding. Nike has recently introduced cricket shoes, called Air Zoom Yorker, designed to be 30% lighter than their competitors'.[16] In 2008, Nike introduced the Air Jordan XX3, a high-performance basketball shoe designed with the environment in mind.


  12. Thank you for nice article.


  13. We have been looking at cloud foundry and PostGIS is our limitation as well.


  14. I was able to get it working using the dev build from GitHub. I posted a blog at: http://gordondickens.com/wordpress/2011/09/23/rocket-to-the-cloud-fast-with-roo/ that might be helpful.

    Regards,
    Gordon Dickens


  15. Nice tutorial.

    I've followed it but using STS and the VMC command line instead of Roo.

    In any case, I have a doubt, how is the database created? I mean, how can I manage the database? (mainly, creation of tables)

    I deployed the application with "hibernate.hbm2ddl.auto" to "create" within the persistence.xml file, and then re-deploy with "validate" value, but I don't think it should a good idea for production environments. Besides, I don't know how to add more tables/columns/whatever I need when the application grows.

    Thanks, and good job with Cloud Foundry.


  16. Excellent site. Lots of useful info here. I'm sending it to several pals ans also sharing in delicious. And naturally, thanks in your sweat!


  17. It's actually a cool and helpful piece of information. I am glad that you shared this helpful information with us. Please stay us informed like this. Thank you for sharing.


  18. |


  19. |


  20. |


  21. Can you explain how to bind to the DB service using the STS environment?


  22. Meron,

    If you have added CloudFoundry to your servers, you double click on the server. 1. Deploy your app, 2. Select services from the "application tab". 3. Drag it to the "Application Services" panel on the right.

    HTH,
    Gordon Dickens
    @gdickens


  23. Thanks for the reply Gordon. Did that already with my app.
    I'm probably missing here something…
    I'm using PostgreSQL, can you tell me how do I get the "PostgreSQL database service"? I don't see it in the services list on the application tab.


  24. That is odd, I would definitely reach out on http://support.cloudfoundry.com/home.

5 trackbacks

Leave a Reply