http://blog.csdn.net/erlib/article/details/40743687

情景:

设计一个图书管理系统,需求:

1. 基本的增删查改功能;

2. 支持多节点备份(其中一个节点挂了进,对外接口不影响)。

方案一:

Erlang 代码如下:https://gist.github.com/zhongwencool/28f7db8d52134b082f97

启动shell:

   erl -name cloud_server@127.0.0.1 -pa "../ebin/" -setcookie best -run cloud_server start_link
erl -name server0@127.0.0.1 -pa "../ebin/" -run deal_book_server -extra server0@127.0.0.1
erl -name server1@127.0.0.1 -pa "../ebin/" -run deal_book_server -extra server1@127.0.0.1
   ....... 
   erl -name server9@127.0.0.1 -pa "../ebin/" -run deal_book_server -extra server9@127.0.0.1 

Tip: 实现的关键在于:每个节点起来后会自动连接到Cloud Center Node 上,并在每15scheck一下与Cloud的连接,Cloud时刻保证最新了节点连接状态数据,并定时广播给连上来的所有节点。

如果你自己写完这个例子,就会对节点互接有更深认识。

方案二:

使用Mnesia的分布特性:

1. 首先我们来实现一个简单的1+1(一个数据处理节点 + 一个备份节点)

 
   -module(db_sync).
-author("zhongwencool@gmail.com"). %% API
-export([create_schema/0, create_table/0,i/0]).
-export([add_account/3,del_account/1,read_account/1]).
   -record(account, {id = 0, name = "", phone = 138000001}).

   create_schema() –>
net_kernel:connect('two@E7D4C9EFE9C405'),
io:format("Self:~w,Connect Nodes:~w",[node(),nodes()]),
mnesia:create_schema([node()|nodes()]). create_table() –>
mnesia:create_table(account,
[{disc_copies,[node()|nodes()]},
{attributes,
record_info(fields, account)}]
). %%查看数据库状态
i() –>
mnesia:system_info(). add_account(ID, Name, Phone) –>
mnesia:transaction(
fun() –>
mnesia:write(#account{id = ID, name = Name, phone = Phone})
end).
del_account(ID) –>
mnesia:transaction(
fun() –>
mnesia:delete({account, ID})
end).
read_account(ID) –>
mnesia:transaction(
fun() –>
mnesia:read({account, ID})
end).

1.1 在xterm 1中:

 > erl erl -sname one -mnesia dir "one"

1.2 在xterm 2中:

 > erl -sname two -mnesia dir "two"
> db_sync:create_schema().

1.3 分别在one shell ,two shell中启动mnesia

 > mnesia:start().

1.4 在任意节点中创建account表

> db_sync:create_table().

这里的account表就是one , two 节点所共享的了,你可以在节点one上增加一个数据,在节点two上查询这个数据,对于用户来说:这完全是透明的!!

1.5 Test:

one 节点上增加数据:

two 节点上查询数据:

2. 节点one挂了后,重启怎么把从two数据同步到节点one?

节点one重启后把数据库表重新建一次就可以啦,如果数据在one挂掉至重启过程中在节点two上发生了变化了,也可以使用mnesia:add_table_copy来做到数据同步。

相信如果掌握了mnesia这2个特性,就可以实现比方案一更加简洁的分布系统啦!


看Mnesia文档里发现一个有意思的点:

是不是觉得mnesia只有使用一个key(当然你可以使用复杂的match表达式来做实现复杂查询条件),但因为match的效率比较低,所有如果你频繁的使用,是不推荐的,这里你应该看看下面这个函数:可以增加一个index哦!【真福利】

add_table_index(Tab, AttrName) -> {aborted, R} | {atomic, ok}

Table indices can and should be used whenever the user wants to frequently use some other field than the key field to look up records. If this other field has an index associated with it, these lookups can occur in constant time and space. For example, if our application wishes to use the name field of accountto efficiently find all account with a specific name, it might be a good idea to have an index on the namefield. This can be accomplished with the following call:

mnesia:add_table_index(account, name).

Indices do not come free, they occupy space which is proportional to the size of the table. They also cause insertions into the table to execute slightly slower.

这样做总比再做一张表来对应Id和Name好多了。
 
个人觉得Mnesia的源码写得真好,值得精读.
           

[Erlang]Mnesia分布式应用的更多相关文章

  1. [Erlang12] Mnesia分布式应用

    [Erl_Question12] Mnesia分布式应用 情景: 设计一个图书管理系统,需求: 1. 基本的增删查改功能; 2. 支持多节点备份(其中一个节点挂了进,对外接口不影响). 方案一: Er ...

  2. erlang mnesia 数据库实现SQL查询

    Mnesia是一个分布式数据库管理系统,适合于电信和其它需要持续运行和具备软实时特性的Erlang应用,越来越受关注和使用,但是目前Mnesia资料却不多,很多都只有官方的用户指南.下面的内容将着重说 ...

  3. erlang mnesia数据库设置主键自增

    Mnesia是erlang/otp自带的分布式数据库管理系统.mnesia配合erlang的实现近乎理想,但在实际使用当中差强人意,总会有一些不足.mnesia数据表没有主键自增的功能,但在mnesi ...

  4. erlang mnesia 数据库查询

    Mnesia是一个分布式数据库管理系统,适合于电信和其它需要持续运行和具备软实时特性的Erlang应用,越来越受关注和使用,但是目前Mnesia资料却不多,很多都只有官方的用户指南.下面的内容将着重说 ...

  5. erlang mnesia数据库简单应用

    mnesia是erlang自带的分布式数据库,基于ets和dets实现的.mnesia兼顾了dets的持久性和ets的高性能,可以自动在多个erlang节点间同步数据库.最关键的是,mnesia实现了 ...

  6. [erlang] mnesia

    原文地址: http://www.cnblogs.com/bluefrog/archive/2012/05/16/2504625.html 本来是项目合作的,可是你却一而再再而三的使用这招,我处理愤怒 ...

  7. erlang 分布式数据库Mnesia 实现及应用

    先推荐一篇:mnesia源码分析(yufeng)   - linear hash   ETS/DETS/mnesia 都使用了linear hash算法 http://en.wikipedia.org ...

  8. 一位Erlang程序猿的自白

    12.00 Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style De ...

  9. [转]Erlang不能错过的盛宴

    Erlang不能错过的盛宴 (快步进入Erlang的世界) 作者:成立涛 (litaocheng@gmail.com) 作为程序员,我们曾经闻听很多“业界动态”,“技术革新”,曾经接触很多“高手箴言” ...

随机推荐

  1. rhel5安装 oracle10

    readhat 安装11gr2文档 需要注意的地方:必须关掉的 1,防火墙:2,SElinux . root 用户运行  setup  命令可关防火墙与SElinux 修改网络配置文件,一定要重启此文 ...

  2. amazeui学习笔记--css(基本样式2)--基础设置Base

    amazeui学习笔记--css(基本样式2)--基础设置Base 一.总结 1.盒子模型:外margin,内padding,这里的内外指的边框 2.border-box:Amaze UI 将所有元素 ...

  3. oled的一套stm32实验2(自己的实验)

    stm32与OLED屏接口的引脚介绍: CS————GPIOD3: RST————GPIOD4: DC—————GPIOD5: D0——————GPIOD6: D1——————GPIOD7; 上是我参 ...

  4. Altium Designer如何删除以布的线

  5. HDU 5237 Base64

    Base64 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...

  6. orabbix自定义监控oracle

    前提:安装orabbix 好后能正常运行, 检验条件(1). 最新数据有数据  (2).图形有显示 (3).日志不报错 /opt/orabbix/logs/orabbix.log   添加方法: 1. ...

  7. async和await在项目中的应用

    Async基础知识: async函数是ES7标准引入的语法,基于Generator函数实现的,也就是说是Generator函数的语法糖.什么是Generator函数?(留个坑) 返回值是Promise ...

  8. Vertx简介

    今天看了一篇很不错的关于Vertx的简介,转载下. 原文链接:http://www.csdn.net/article/2015-12-21/2826533?utm_source=tuicool& ...

  9. Oracle以系统管理员的方式登录失败

    解决方法: 因为SYS是在数据库之外的超级管理员,所以我们在登录的时候输入sys后在输入命令:password as sysdba 就可以!例如:输入口令: m1234 as sysdba 参考文章 ...

  10. [Angular] Angular Global Keyboard Handling With EventManager

    If we want to add global event handler, we can use 'EventManager' from '@angular/platform-broswer'. ...