How we use MongoDB in the cloud05 Aug 2014 by Franziskus Domig
At teamgeist.io we love the cloud and use various services and solutions to adapt and scale quickly. We develop our back end application with Java and use MongoDB as our main storage solution. So we needed a application hosting and storage solution in the cloud. With this article I am going to describe what helped us to quickly develop a great product with the support of MongoSoup - the first German-based MongoDB cloud hosting solution.
- Register and create your App at cloudControl or heroku.
- Enable the cloudControl MongoSoup add-on or heroku MongoSoup add-on
- Integrate the specific MongoDB driver for your programming language.
- Develop your application.
- Deploy the application to your platform.
Choose cloud hosting
When we first started developing we thought about hosting our application as well as our database ourselves. Our server with our expertise, what can go wrong? Well, possible downtime, 24/7 support etc. All the things you do not want to deal with. So we choose the cloud and decided to host our application at cloudControl which gives us basically the same functionality as heroku but with a German speaking support.
The cloudControl Quickstart documents the necessary steps for setting up an account and start with your first application.
Heroku has a Getting Started with Java tutorial which explains the first steps for a Java application on their platform.
Follow the steps for your preferred platform to install the client application and then continue with the next steps in this article.
Your App with the MongoSoup add-on
We create a new (Java) application (container) at cloudControl and add the smallest (free) MongoSoup add-on to your container with following commands:
The same thing can be done with heroku with the following commands:
If you need more storage space, automatic backups and premium support you can later upgrade to a bigger MongoSoup plan with ease. You might even get a dedicated solution just for you.
Integrate MongoDB driver
Depending on your build system you have to integrate MongoDB driver dependency. We are using Gradle as our build tool, however, the cloudControl and heroku buildpack are configured to use Maven. If you want to use Gradle you have to provide a custom Gradle buildpack which will work for both, cloudControl and heroku. The configuration for gradle is the following:
Alternatively you could use Maven with the following configuration:
At the time of writing this article the MongoDB Java Driver was available in version 2.12.13. Please check if there is a new version available on Maven Central.
Develop your application
Now you can develop your application that uses MongoDB as the storage engine. cloudControl and heroku, both set the environment variable
MONGOSOUP_URL to get the connection URL string to access the MongoDB database provided by MongoSoup.
1String uriString = System.getenv("MONGOSOUP_URL"); 2MongoClientURI uri = new MongoClientURI(uriString); 3MongoClient mongo = new MongoClient(uri); 4DB db = mongo.getDB(uri.getDatabase()); 5 6DBCollection collection = db.getCollection("testCollection"); 7 8BasicDBObject doc = new BasicDBObject("hello", "MongoSoup"); 9collection.insert(doc);
We at teamgeist.io are using Spring Data MongoDB to get a POJO centric model for interacting with the MongoDB collections and to write Repositories for accessing data in the database.
Deploy to your platform
Deployment with cloudControl is very straight forward and is basically just a git push. Just use the following commands to deploy and publish your application:
Deploying to heroku is even simpler and can be done with the following command:
Et voilà. Your very first (cloud) application that is using MongoDB hosted in the cloud. Pretty simple and easy, though, this “Hello MongoSoup” example might be not what you will end up.
The real power will unveil as soon as scaling and availability is important. However, you see that starting your project in the cloud with MongoSoup is very simple and can be achieved with very little effort.