Clojure on CloudFoundry

In this article, I will show you how to develop and deploy clojure web application on CloudFoundry. As you may know, CloudFoundry is an opensource PaaS backed by VMWare. Java, Ruby and Nodejs are officially supported. As a JVM language, clojure is born to be also available on this platform, although it’s not listed.

CloudFoundry accepts a .war file for Java web application deployment. So you don’t need the ring-jetty-adaptor and a procfile as heroku requires. To help your development and deployment, I strongly recommend the lein-ring plugin:

  :dev-dependencies [[lein-ring "0.5.4"]]

CloudFoundry provides backend services like mysql, redis, mongodb and more. The connection information are stored as environment variables. Here you can find a subset of them.

Take mongodb as example, connection information (host, port, username and password) are encoded as JSON, stored in environment variables. You can get them with this function:

(defn mongo-config [key]
  (if-let [services (System/getenv "VCAP_SERVICES")]
    (let [services-dict (json/read-json services false)]
      (-> services-dict
        (get "mongodb-1.8")
        first
        (get "credentials")
        (get key)))))

In the server bootstrap function, create the mongodb connection:

(defn app-init []
  (def db-conn (make-connection
                 (or (mongo-config "db") "lazypress")
                   :host (or (mongo-config "hostname") "localhost")
                   :port (or (mongo-config "port") 27017)))
  (when-not (nil? (mongo-config "username"))
    (authenticate db-conn
      (mongo-config "username")
      (mongo-config "password")))

By adding check for nil, local databased is also supported. This is pretty convenience for local development. These environment variables are consistent on all cloudfoundry application, so it’s possible to deploy the application on multiple accounts without any changes.

Then you can add your web stuff just like standard clojure web development. (If you are using compojure, place your static files under resource/public.)

Finally, package it. (Suppose your application is named as “lazypress”)

lein ring uberwar lazypress.war

Use the vmc tool to deploy it:

vmc update lazypress

For more usage about the vnc tool, you can read this article.

So you have finished deploying your clojure web application to cloudfoundry.

Backed by spring and vmware, cloudfoundry is more Java-friendly than other PaaS like heroku. You don’t have to start a Java process by yourself (“lein run” isn’t a graceful way to start your app in product environment). And you don’t have to worry about your web container settings (configure jetty with limited options via ring-jetty-adaptor). All you have to do is package the application as a portable war file, which you can deploy to tomcat, glassfish, and also cloudfoundry. The vmc tool could detect you war file and handle it correctly.

About these ads

一条回应 to “Clojure on CloudFoundry”

  1. Andreas Says:

    Thanks for the write-up!

    I also really like your pyclj project! Looks very promising. Please keep up the good work!

    Greetings,
    Andreas

发表评论

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 / 更改 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s


加关注

每发布一篇新博文的同时向您的邮箱发送备份。

%d bloggers like this: