代码改变世界

高性能服务器架构之路-MongoDB(一):MongoDB快速入门(java版)(草稿)

2010-10-12 23:34  姜 萌@cnblogs  阅读(2370)  评论(0编辑  收藏  举报

最近想通了,辞了职,原本谈好的新公司offer也给推掉,义无反顾的和朋友开始做自己的产品,反正明年毕业,失败了也没什么损失。主做应用服务器的客串android/windows phone客户端,计划未来iphone(当然赚到票票再说)。

简单拟定了下产品的文档,下面是整个服务器的架构示意图:

imageimage

对于社交类型的系统,数据库的I/O注定会成为一大瓶颈,单点数据库体系肯定应付不了如此大的访问量,像facebook,twitter,foursquare之类的社交型平台都有自己的一套方案来解决数据库瓶颈问题。一开始我在持久化框架的选取上我的注意力放在了iBatis和hibernate上,虽然表面上hibernate用起来简单,但是若想将其性能发挥到极致还是很有难度的,iBatis的sql mapping半自动化方案反而使性能优化更加灵活。就在考察这两个框架本身的性能以及与缓存、分布式的应用过程中无意间发现了mongodb这个东西,早在一年前就在jdon上看到过板桥先生关于nosql替代关系型数据库的言论,不过当时才疏学浅一直不明白什么是key-value型的数据库,正好这次有机会领教一下传说中的nosql。查阅了官方的文档和网上的讨论以及morphia-0.95这个支持mongodb的orm框架后,我决定在这个项目中大胆尝试采用mongodb+morphia做持久层。

首先认识下mongodb,从http://www.mongodb.org/display/DOCS/Home下载到对应的版本(建议下载64位版本,这样可以突破32位系统的单文件2GB限制),解压后在bin中既是mongodb的所有程序文件(很绿色吧~~)

image

为方便使用,写一个脚本的方便以后启动mongodb服务器和客户端控制台。

server.cmd

@echo off
cd bin
mongod -port 27017 -dbpath "d:\mongodb\dbdata" run

client.cmd

@echo off
cd bin
mongo 127.0.0.1:27017


 

 

mongodb与普通的关系型数据库类似有Database的概念,但没有表的概念,取而代之的是collection,没有row的概念,取而代之的是document,事物支持比较弱。

使用mongodb api操作数据库示例

……

DBAddress dbServer_2 = new DBAddress("localhost:27017");
DBAddress dbServer_1 = new DBAddress("localhost:27018");
Mongo Mongo = new Mongo(dbServer_1, dbServer_2);
if(Mongo.authenticate("dokhell", "unikey"))
        throw DbAuthenticateException;

Set<String> names = Mongo.getDB("bestpro").getCollectionNames();
        DBCursor cursor = Mongo.getDB("bestpro").getCollection("bestpro").find();
        for(DBObject obj : cursor) {
            System.out.println(obj.get("a"));
        }
        DB db = Mongo.getDB("bestpro");
        Collection coll = db.getCollection("bestpro");

……

当然,我们还需要一套orm来简化我们的数据库访问机制。这里有一个开源的orm框架morphia对mongdb提供良好的支持。

//操作聚合实体与值对象

Morphia morphia = new Morphia();
        DBAddress dbAddr = new DBAddress("localhost:27017");
        Mongo m = new Mongo(dbAddr);
        BlogDao dao = new BlogDao(m, morphia, "bestpro");
        dao.deleteByQuery(dao.createQuery().field("title").equal("this is next blog222"));
       
        //System.out.println(dao.createQuery().limit(2).asList().get(1).getTitle());
        ArticalEntity blog = new ArticalEntity();
        //blog.setTitle("this is my fault222!`");
        //dao.save(blog);
        List<Comment> comments = new ArrayList<Comment>();
        Comment comment1 = new Comment();
        comment1.setName("comment1222");
        comment1.setContent("this is the comment's content");
       
        Comment comment2 = new Comment();
        comment2.setName("comment1222");
        comment2.setContent("this is the comment's content");
        comments.add(comment2);
        blog.setComments(comments);
       
        ArticalEntity nextBlog = new ArticalEntity();
        nextBlog.setTitle("this is next blog222");
        blog.setNextBlog(nextBlog);
        //InsertOrUpdate
        dao.save(nextBlog);//关联的聚合实体需要在被包含的实体持久化之前持久化
        dao.save(blog);
       
        blog.setTitle("应该能够能支持中文的,因为全是二进制数据");
        dao.save(blog);
        //QueryAndUpdate(special update form)
        //UpdateOperations ops = dao.createUpdateOperations().set("a", "2");
        //dao.update(dao.createQuery(), ops);
        //query
        QueryResults<ArticalEntity> entities = dao.find();
        System.out.println(entities.asList().get(0).getTitle());
        //pager
        System.out.println(dao.createQuery().offset(0).limit(1).asList().get(0).getTitle());

跨collection操作

Morphia morphia = new Morphia();
        DBAddress dbAddr = new DBAddress("127.0.0.1:27017");
        Mongo m = new Mongo(dbAddr);
        BlogDao blogDao = new BlogDao(m, morphia, "bestpro");
        PersonDao personDao = new PersonDao(m, morphia, "bestpro");
        PersonEntity person = new PersonEntity();
        person.setName("my name is ……");
        ArticalEntity artical = new ArticalEntity();
        artical.setTitle("this is a title");
        artical.setPerson(person);
        personDao.save(person);
        blogDao.save(artical);

相关资源链接:

mongodb quick start

http://www.mongodb.org/display/DOCS/Quickstart

mongodb驱动jar包+morphia的jar包下载:

https://files.cnblogs.com/wJiang/mongo_dev.rar