http://blog.csdn.net/erlib/article/details/40743687
Scene:
Design a book Management system, needs:
1. Basic additions and deletions function;
2. Support Multi-node backup (one of the nodes is hung up, external interface does not affect).
Programme one:
The Erlang code is as follows: HTTPS://GIST.GITHUB.COM/ZHONGWENCOOL/28F7DB8D52134B082F97
To start the shell:
[email protected] -pa ". /ebin/"-setcookie best-run cloud_server start_link [email protected][email protected] [email protected] [email protected]
[email protected] [email protected]
TIP: The key to the implementation is that each node is automatically connected to cloud Center node and connected to the cloud every 15scheck, and the cloud is always up-to-date with the latest node connection status data, and broadcasts it to all nodes connected.
If you write this example yourself, you will have a deeper understanding of how nodes are interconnected.
Scenario Two:
Use the distribution characteristics of the Mnesia:
1. First we will implement a simple plus (a data processing node + a backup node)
-module (db_sync). -author ("[email protected]"). Percent percent 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 (' [email protected] '), 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)}] ). Percent View Database state 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 in Xterm 1:
> Erl erl-sname one-mnesia dir "one"
1.2 In Xterm 2:
> Erl-sname two-mnesia dir "i" > Db_sync:create_schema ().
1.3 Start Mnesia in one shell, two shells, respectively
> Mnesia:start ().
1.4 Creating an Account table in any node
> db_sync:create_table ().
Here the Account table is one, two nodes shared, you can add a data on the node, on node two to query this data, for the user: This is completely transparent!!
1.5 Test:
Add data to the one node:
Query data on both nodes:
2. After the node one is hung up, how do I sync the data from both to node one?
Once the node one restarts, the database tables can be re-built once, and if the data has changed on node-I during a hang-off to restart, you can also use Mnesia:add_table_copy to synchronize data.
believe that if you master the Mnesia these 2 features, you can achieve a more concise distribution system than the scheme!
Look at the interesting points found in the Mnesia documentation:
Do you think that Mnesia only use a key (you can certainly use complex match expressions to implement complex query conditions), but because the match efficiency is low, all if you frequently use, is not recommended, here you should look at this function: You can add an index OH ! "True welfare"
Add_table_index (Tab, Attrname), {aborted, R} | {Atomic, OK}
Table indices can and should is used whenever the user wants to frequently use some other field than the key field to look Up Records. If This and the field has a index associated with it, the these lookups can occur in constant time and space. For example, if we application wishes to use the name field of Accountto efficiently find all account with a specific Nam E, it might is a good idea to has an index on the Namefield. This can is accomplished with the following call:
Mnesia:add_table_index (account, name).
Indices don't come free, they occupy space which are proportional to the size of the table. They also cause insertions into the table to execute slightly slower.
It's better to do this than make another table to match the ID and name.
Personally feel that the source code of Mnesia is really good, worthy of intensive reading.
[Erlang] Mnesia Distributed Applications