4 程式設計技術
4.1抽象並出發
eg:thesis_server1.erl
-module(thesis_server1).
-export([start/3, stop/1, rpc/2]).
start(name, f, state) ->
register(name, spawn(fun()->loop(name, f, state) end)).
loop(name, f, state) ->
receive
stop -> void;
-> = f(query, state),
pid ! ,
loop(name, f, state1)
end.
stop(name) -> name ! stop.
rpc(name, query) ->
name ! ,
receive
-> reply
end.
thesis_vshlr1.erl
-module(thesis_vshlr1).
-export([start/0, stop/0, handle_event/2, i_am_at/2,find/1]).
-import(thesis_server1,[start/3,stop/1,rpc/2]).
-import(dict, [new/0, store/3, find/2]).
start() -> start(vshlr, fun handle_event/2, new()).
stop() ->stop(vshlr).
i_am_at(who, where) ->
rpc(vshlr, ).
find(who) ->
rpc(vshlr, ).
handle_event(, dict) ->
;handle_event(, dict) ->
. 呼叫
1>thesis_vshlr1:start().
true
2>thesis_vshlr1:find("joe").
error
3>thesis_vshlr1:i_am_at("joe","sics").
ack4>thesis_vshlr1:find"joe").
4.1.1 乙個可容錯的客戶伺服器模型
增加catch,可容錯,其它同上
eg:thesis_server2.erl
-module(thesis_server2).
-export([start/3, stop/1, rpc/2]).
start(name, f, state) ->
register(name, spawn(fun()->loop(name, f, state) end)).
loop(name, f, state) ->
receive
stop -> void;
->
case (catch f(query, state)) of
->
log(name, query, why),
from ! ,
loop(name, f, state);
->
log(name, ok, reply),
from ! ,
loop(name, f, state1)
end
end.
stop(name) -> name ! stop.
rpc(name, query) ->
name ! ,
receive
-> exit(rpc);
-> reply
after 1000 ->
exit(timeout)
end.
log(name, query, why) ->
io:format("server ~p is ~p by ~p~n",[name, query, why]).
thesis_vshlr2.erl
-module(thesis_vshlr2).
-export([start/0, stop/0, handle_event/2, i_am_at/2,find/1]).
-import(thesis_server2,[start/3,stop/1,rpc/2]).
-import(dict, [new/0, store/3, find/2]).
start() -> start(vshlr, fun handle_event/2, new()).
stop() ->stop(vshlr).
i_am_at(who, where) ->
rpc(vshlr, ).
find(who) ->
rpc(vshlr, ).
handle_event(, dict) ->
;handle_event(, dict) ->
1/0;
handle_event(, dict) ->
. 1>thesis_vshlr2:start().
true
2>thesis_vshlr2:find("myerror").
server vshlr is by ,
]}crase
eg:thesis_server3.erl
熱替換程式,(還有問題,沒成功)
-module(thesis_server3).
-export([start/3, stop/1, rpc/2,swap_code/2]).
start(name, f, state) ->
register(name, spawn(fun()->loop(name, f, state) end)).
loop(name, f, state) ->
receive
stop -> void;
} ->
from ! ,
loop(name, fnew,state);
->
case (catch f(query, state)) of
->
log(name, query, why),
from ! ,
loop(name, f, state);
->
log(name, ok, reply),
from ! ,
loop(name, f, state1)
end
end.
stop(name) -> name ! stop.
swap_code(name, f) ->
rpc(name, ).
rpc(name, query) ->
name ! ,
receive
-> exit(rpc);
-> reply
after 1000 ->
exit(timeout)
end.
log(name, query, why) ->
io:format("server ~p is ~p by ~p~n",[name, query, why]).
thesis_vshlr3.erl
-module(thesis_vshlr3).
-export([start/0, stop/0, handle_event/2, i_am_at/2,find/1]).
-import(thesis_server3,[start/3,stop/1,rpc/2,swap_code/2]).
-import(dict, [new/0, store/3, find/2]).
start() -> start(vshlr, fun handle_event/2, new()).
stop() ->stop(vshlr).
i_am_at(who, where) ->
rpc(vshlr, ).
find(who) ->
rpc(vshlr, ).
handle_event(, dict) ->
;handle_event(, dict) ->
. 1> thesis_server3:swap_code(thesis_vshlr1, fun(i,j)->thesis_vshlr1:handle_event(i,j) end).
使用redis構建可靠分布式鎖
關於分布式鎖的概念,具體實現方式,直接參閱下面兩個帖子,這裡就不多介紹了。分布式鎖的多種實現方式 分布式鎖總結 對於分布式鎖的幾種實現方式的優劣,這裡再列舉下 1.資料庫實現方式 優點 易理解 缺點 運算元據庫消耗較大,效能較低。為了處理一些異常,會使得整個方案越來越複雜 2.快取實現方式 優點 效...
redLock可靠的分布式鎖
本文是對 martin kleppmann 的文章 how to do distributed locking 部分內容的翻譯和總結,上次寫 redlock 的原因就是看到了 martin 的這篇文章,寫得很好,特此翻譯和總結。感興趣的同學可以翻看原文,相信會收穫良多。開篇作者認為現在 redis ...
Jenkins的分布式構建
1.架構 jenkins的master sl e分布式架構可以解決單點構建任務多 負載較高 效能不足的問題。2.原理 對於jenkins的分布式框架,可以簡單的理解為server client模式,在jenkins中是master sl e,也就是乙個主的server叫master,其他的子clie...