ææŠãã
ã¯ã©ã¹ã¿ãŒã§åäœããerlangã§å®éã®ãµãŒãã¹ãäœæããå¿ èŠããããŸãã ããã«ããµãŒãã¹ãæäŸãã人ã®ç掻ãç°¡çŽ åããå¿ èŠããããŸãã
èŠä»¶ïŒ
- ãµãŒãã¹ã«ã¯RESTfulã€ã³ã¿ãŒãã§ã€ã¹ããããŸãïŒãã¡ãã·ã§ããã«ã§ã¢ãã³ã§ãïŒ
- ãµãŒãã¹ã®ã¡ã€ã³èšå®ã¯ãæ確ãªæ§æãæã€å°ããªãã¡ã€ã«ã«ç§»åããå¿ èŠããããŸã
- ãµãŒãã¹ã¯ãªãã·ã§ã³ã®ã¢ã¯ã»ã¹ãã°ãæžã蟌ãå¿ èŠããããŸã
- ãµãŒãã¹ã¯upstartããèµ·åããå¿ èŠããããŸã
ç°¡åã«ããããã«ããµãŒãã¹ã¯ã«ãŠã³ã¿ãŒã«ãªããŸããããã«ãããåã¯ã©ã€ã¢ã³ãã¯ãªã¯ãšã¹ãããšã«å¢å ããæŽæ°ã«ãªããŸãïŒã«ãŠã³ã¿ãŒãåèµ·åããããŸã§äžæã§ãïŒã
ãã¯ãããžãŒ
æããã¡ãã·ã§ããã«ã§ã¢ãã³ãªãã®ãéžæããŠãã ããïŒ
- HTTPãµãŒããŒ- ã«ãŠããŒã€0.10.0
- ãã¬ãŒ- ã©ã¬ãŒ
- ãã«ãã·ã¹ãã -erlang.mk
- ãªãªãŒã¹ã³ã¬ã¯ã¿ãŒ-relx
建ç¯
ã«ãŠããŒã€ã¯ããŒãã«ãã³ã°ã¢ãããããã³ãã©ãŒã§ãªã¯ãšã¹ããåŠçããŸãããã³ãã©ãŒã¯ã«ãŠã³ã¿ãŒãåŒã³åºããã¯ã©ã€ã¢ã³ãã«å¿çããŠãã°ãšã³ããªãæžã蟌ã¿ãŸãã
ã«ãŠã³ã¿ãŒã¯ã°ããŒãã«ã«ç»é²ããããããã¯ã©ã¹ã¿ãŒã®ä»»æã®ããŒãããç°¡åã«ã¢ã¯ã»ã¹ã§ããŸãã
èµ·åæã«ãã«ãŠã³ã¿ã¯ãçµäºããªãå ŽåïŒã«ãŠã³ã¿ãå¥ã®ããŒãã«ãã§ã«ç»é²ãããŠããå ŽåïŒãç»é²ãè©Šè¡ããŸãããããè¡ãæ©äŒãåŸ ã¡ãŸãã
ã¢ããªã±ãŒã·ã§ã³ã®ã¹ã±ã«ãã³
ãã¹ãŠã®æšæºã«åŸã£ãŠOTPã¢ããªã±ãŒã·ã§ã³ãäœæããå¿ èŠããããŸãããæå°éã®åŽåã§è¡ããŸãã
ãããžã§ã¯ãã®erdicoãã£ã¬ã¯ããªãäœæãããã®äžã«git initãäœæããåãååã®ãããžã§ã¯ãã®ãªããžããªããerlang.mkãã¡ã€ã«ãããŠã³ããŒãããåçŽãªMakefileãäœæããŸãã
PROJECT = erdico ERLC_OPTS= "+{parse_transform, lager_transform}" DEPS = cowboy lager dep_cowboy = pkg://cowboy 0.10.0 dep_lager = https://github.com/basho/lager.git 2.0.3 include erlang.mk
Mac OS / BSDãŠãŒã¶ãŒ ïŒwgetãå¿ èŠã§ãã Linuxã§ã¯ãã©ãã§ãããã«äœ¿ããããã«ãªããŸããã
ã«ãŠããŒã€ã¯æåãªããã±ãŒãžãšããŠå«ãŸããŠããããšã«æ³šæããŠãã ããã erlang.mkãªããžããªã¯å°ããã§ãããããã§ãã
src / erdico.app.srcãã¡ã€ã«ã§ãã¢ããªã±ãŒã·ã§ã³ãèšè¿°ããŸãïŒãã¹ãŠã®ãã©ã¡ãŒã¿ãŒãå¿ èŠã§ããããããªããšãerlang.mkãŸãã¯relxãç ŽæããŸãïŒã
{application, erdico, [ {description, "Hello, Upstart distributed Erlang service"}, {id, "ErDiCo"}, {vsn, "0.1"}, {applications, [kernel, stdlib, lager, cowboy]}, % run-time dependencies {modules, []}, % here erlang.mk inserts all application modules, not added automatically, required by relx {mod, {erdico, []}}, % application callback module {registered, [erdico]} % required by relx ]}.
src / erdico.erlãã¡ã€ã«ãäœæããŸãããçŸæç¹ã§ã¯-moduleïŒerdicoïŒãã£ã¬ã¯ãã£ããé€ããŸãã ããã«ã¯äœãæžããªãã§ãã ããã
ãã®ç¶æ ã§ã¯ãmakeã¯äŸåé¢ä¿ãåçž®ãããæ€åºãããã¹ãŠãåéããå¿ èŠããããŸãã
ã¢ããªã±ãŒã·ã§ã³ã®èµ·åãã«ãŠããŒã€ããã³æãåçŽãªãªã¯ãšã¹ããã³ãã©ïŒ launcher ã handler ïŒ
ç°¡åã«ããããã«ããã¹ãŠã®å¶åŸ¡ã³ãŒãã1ã€ã®erdicoã¢ãžã¥ãŒã«ã«å ¥ããŠããŸãã çä¿¡è ã¯ããââã§4ã€ã®ã¢ãžã¥ãŒã«ãäœæã§ããæ®ãã®ãã¹ãŠã¯ãããžãã¯ãçªç¶é¡èã«éèªæã«ãªãããããã£ãŠå¥ã®ã¢ãžã¥ãŒã«ã«ãµããããããŒã¹ãäœæããŸãã
HTTPãµãŒããŒ
ããããã®æå°æ§æãå«ãŸããŠããŸãã ä»ã«äœãèµ·ãããã¯ã ããã¥ã¡ã³ãã«èšèŒãããŠããŸãã
start_cowboy() -> DefPath = {'_', erdico_handler, []}, % Catch-all path Host = {'_', [DefPath]}, % No virtualhosts Dispatch = cowboy_router:compile([Host]), Env = [{env, [{dispatch, Dispatch}]}], cowboy:start_http(?MODULE, 10, [{port, 2080}], Env).
ãªã¯ãšã¹ããã³ãã©ãŒ
ãããŸã§ã¯ãã¹ãŠãåå§çã§ãïŒ
-module(erdico_handler). -behavior(cowboy_http_handler). -export([init/3, handle/2, terminate/3]). init(_Type, Req, _Options) -> {ok, Req, nostate}. handle(Req, nostate) -> {ok, Replied} = cowboy_req:reply(200, [], <<"hello\n">>, Req), {ok, Replied, nostate}. terminate(_Reason, _Req, nostate) -> ok.
åéãå®è¡ã確èª
ãã«ãããã«ã¯ãäœæããã ãã§ãã
ãŸããäŸåé¢ä¿ã®ãããã£ã¬ã¯ããªãšãã¢ããªã±ãŒã·ã§ã³ã®ãã€ããªã®ãããã£ã¬ã¯ããªãæå®ããå¿ èŠããããŸãã
çãŸãã@ node1ïŒã/ erdico $ ERL_LIBS = deps erl -pa ebin -s erdico
Erlangã³ã³ãœãŒã«
Erlang / OTP 17 [erts-6.1] [source-d2a4c20] [64-bit] [async-threadsïŒ10] [hipe] [kernel-pollïŒfalse] Eshell V6.1ïŒ^ Gã§äžæ¢ïŒ 1> 15ïŒ01ïŒ14.486 [æ å ±]ã¢ããªã±ãŒã·ã§ã³ã©ã¬ãŒãããŒãnonode @ nohostã§éå§ãããŸãã 15ïŒ01ïŒ14.493 [æ å ±]ããŒãnonode @ nohostã§ã¢ããªã±ãŒã·ã§ã³ã©ã³ããéå§ãããŸãã 15ïŒ01ïŒ14.506 [æ å ±]ããŒãnonode @ nohostã§ã¢ããªã±ãŒã·ã§ã³æå·åãéå§ãããŸãã 15ïŒ01ïŒ14.506 [æ å ±]ã¢ããªã±ãŒã·ã§ã³cowlibãããŒãnonode @ nohostã§éå§ãããŸãã 15ïŒ01ïŒ14.513 [info]ããŒãnonode @ nohostã§ã¢ããªã±ãŒã·ã§ã³ã«ãŠããŒã€ãéå§ãããŸãã 15ïŒ01ïŒ14.530 [æ å ±]ã¢ããªã±ãŒã·ã§ã³erdicoã¯ããŒãnonode @ nohostã§éå§ãããŸãã 1>
ã©ã¬ãŒã§ãããäœããã®åœ¢ã§æ©èœãå§ããããšãããããŸãïŒã³ã³ãœãŒã«ã«å ããŠã圌ã¯ãã£ã¹ã¯ã«ãæžã蟌ã¿ãŸããïŒã
çãŸãã@ node2ïŒã$ curl node1ïŒ2080 ããã«ã¡ã¯
ã«ãŠã³ã¿ãŒ
ããŠãã¢ããªã±ãŒã·ã§ã³ãèµ·åããŠå®è¡ãããŸãã ãã®ååšã«æå³ãå ããæã§ãã
å®è£ ã®è©³çŽ°ã«ã€ããŠã¯è§ŠããŸãããã ããããèªãã§ãã ããã
ãã¢ã³ã¹ãã¬ãŒã·ã§ã³
ãããŸã§ãåããã¹ãnode1-e1 @ node1ããã³e2 @ node1ã§äž¡æ¹ã®ã¢ãŒã©ã³ããŒããèµ·åããŸããã ãããè¡ãã«ã¯ããµãŒããŒããã³ã°ããããŒããã³ãã³ãã©ã€ã³ããæ§æããŸãã
æåã®ã¡ã¢ã§ã¯ãã«ãŠã³ã¿ãŒã20ã«ã2çªç®ã«-1ã«å·»ãäžããŸããã¯ã©ã¹ã¿ãŒãã¢ã»ã³ãã«ãã2çªç®ã®ããŒãã®ã«ãŠã³ã¿ãŒã匷å¶çµäºããããšã確èªããŸãããã®åŸã2çªç®ã®ããŒãããã«ãŠã³ã¿ãŒãžã®ã¢ã¯ã»ã¹ãæåã®ã«ãŠã³ã¿ãŒãåŒã³åºããŸãã
e1 @ node1
çãŸãã@ node1ïŒã/ erdico $ ERL_LIBS = deps erl -pa ebin -s erdico -setcookie erdico -sname e1 -erdico port 2081 Erlang / OTP 17 [erts-6.1] [source-d2a4c20] [64-bit] [async-threadsïŒ10] [hipe] [kernel-pollïŒfalse] ............... ïŒe1 @ node1ïŒ2> erdico_counterïŒincïŒ10ïŒã {OKã20} ïŒe1 @ node1ïŒ3> 16ïŒ11ïŒ30.422 [info] globalïŒååã®ç«¶åã®çµäº{erdico_counterã<10869.102.0>} ïŒe1 @ node1ïŒ3> erdico_counterïŒincïŒïŒã {OKã22}
e2 @ node1
çãŸãã@ node1ïŒã/ erdico $ ERL_LIBS = deps erl -pa ebin -s erdico -setcookie erdico -sname e2 -erdico port 2082 Erlang / OTP 17 [erts-6.1] [source-d2a4c20] [64-bit] [async-threadsïŒ10] [hipe] [kernel-pollïŒfalse] .............. ïŒe2 @ node1ïŒ1> erdico_counterïŒincïŒïŒã {OKã1} ïŒe2 @ node1ïŒ2> net_admïŒpingïŒe1 @ node1ïŒã ãã³ãã³ ïŒe2 @ node1ïŒ3> 16ïŒ11ïŒ30.423 [ãšã©ãŒ]ã¹ãŒããŒãã€ã¶ãŒerdicoã¯ãerdico_counterã§åã«ãŠã³ã¿ãŒãéå§ããŸããïŒstart_linkïŒïŒat <0.102.0> exit with reason with killed reason in context child_terminated ïŒe2 @ node1ïŒ3> erdico_counterïŒincïŒïŒã {OKã21}
ã«ãŠããŒã€ãšã«ãŠã³ã¿ãŒ
ãŸããããã¯ç°¡åã§ãã
ããŸãããïŒ
çãŸãã@ node2ïŒã$ curl node1ïŒ2081 å€= 1 çãŸãã@ node2ïŒã$ curl node1ïŒ2082 å€= 2 çãŸãã@ node2ïŒã$ curl node1ïŒ2081 å€= 3 çãŸãã@ node2ïŒã$ curl node1ïŒ2082 å€= 4 çãŸãã@ node2ïŒã$ curl node1ïŒ2082 å€= 5 çãŸãã@ node2ïŒã$ curl node1ïŒ2081 å€= 6
æçš¿ã®ç°¡åãªéšåã¯çµãããŸãã ã
access.log
Lagerã¯ãErlangã§å¯äžã®ã©ã€ããã°ãã¬ãŒã ã¯ãŒã¯ã§ãã ããã«ãã圌ã¯ç涯ã®äŸãå«ãç°¡æœãªææžãæ¬ ããŠããŸãã å°ãªããšãRunetã«ãšã£ãŠããã®æçš¿ããã®ãããªäŸã«ãªãããšãé¡ã£ãŠããŸãã
ããã«ã ã«ãŠããŒã€ã® access.logãšã³ããªã§ã¯ãã€ã³ã¿ãŒãããã¯ããã»ã©å¯å€§ã§ã¯ãããŸããã ãã®æçš¿ã§ããããä¿®æ£ãããããšãé¡ã£ãŠããŸãã
ã©ã¬ãŒãã¬ãŒã¹
ã©ã¬ãŒæ§æã§ã¯ãã€ãã³ãã¯é倧床ã«å¿ããŠãã¡ã€ã«éã§åæ£ãããŸãã HTTPãµãŒããŒã®ãã°ãèšé²ããã«ã¯ãã€ãã³ããç¹å®ã®ãã°ã«æ瀺çã«éãå¿ èŠããããããããã¯ç§ãã¡ã«ã¯é©ããŠããŸããã ãã®ãããã©ã¬ãŒã«ã¯ãã¬ãŒã¹ãšåŒã°ããç¹å¥ãªåãå·ããããŸããããã䜿çšããŸãã
ãã®æç¹ã§ããã§ã«èšå®ãã¡ã€ã«ãå¿ èŠã§ãã
ããã§ãã¯ã©ãã·ã¥ãã°ããªãã€ã¬ã¯ãããå€ããå°ãªããéèŠãªã€ãã³ããå«ããã°ãäœæããã€ãã³ãã¡ã¿ããŒã¿ã«{tagãaccess}ãå«ãŸããŠããå Žåããã¬ãŒã¹ãéããŠã®ã¿æžã蟌ãŸããaccess.logã宣èšããŸãã ãã®ãã©ãŒãããã§ã¯ããã¹ãŠãå€ããå°ãªããæ確ã§ããè¡ã¯æååãšããŠæ¿å ¥ãããã¢ãã ã¯å¯Ÿå¿ããããŒã®ã¡ã¿ããŒã¿ããã®å€ã«çœ®ãæããããŸãïŒåŸã§äœ¿çšæ¹æ³ã説æããŸãïŒã
èšå®ããããã¹ãŠã®ãã°ã«ã€ããŠãããŒããŒã·ã§ã³ã¯æ·±å€ã«ãªã³ã«ãªãã5ã€ã®å€ããã¡ã€ã«ãä¿åãããŸãã ãã°ããŒããŒã·ã§ã³ã¯ç¡å¹ã§ãã
erdico.config
ãã¡ã€ã«å
šäœ
[ {lager, [ {crash_log, "logs/crash.log"}, {crash_log_size, 0}, {crash_log_date, "$D0"}, {crash_log_count, 5}, {error_logger_hwm, 20}, {async_threshold, 30}, {async_threshold_window, 10}, {handlers, [ {lager_file_backend, [{file, "logs/events.log"}, {level, notice}, {size, 0}, {date, "$D0"}, {count, 5}, {formatter, lager_default_formatter}, {formatter_config, [date, " ", time," [",severity,"] ",pid, " ", message, "\n"]}]}, {lager_file_backend, [{file, "logs/access.log"}, {level, none}, {size, 0}, {date, "$D0"}, {count, 5}, {formatter, lager_default_formatter}, {formatter_config, [date, " ", time," [",severity,"] ",pid, " ", peer, " \"", method, " ", url, "\" ", status, "\n"]}]} ]}, {traces, [ {{lager_file_backend, "logs/access.log"}, [{tag, access}], info} ]} ]} ].
å®è¡ã確èª
çµæïŒ
çãŸãã@ node1ïŒã/ erdico $ ERL_LIBS = deps erl -pa ebin -config erdico.config -s erdico -setcookie erdico -sname e1 -erdico port 2081 Erlang / OTP 17 [erts-6.1] [source-d2a4c20] [64-bit] [async-threadsïŒ10] [hipe] [kernel-pollïŒfalse] Eshell V6.1ïŒ^ Gã§äžæ¢ïŒ ïŒe1 @ node1ïŒ1> lagerïŒlogïŒnoticeã[{pidãselfïŒïŒ}]ã "helloãsãw"ã[worldã2.7]ïŒã ããã£ã ïŒe1 @ node1ïŒ3> lagerïŒlogïŒinfoã[{pidãselfïŒïŒ}ã{tagãaccess}ã{peerã "fake"}ã{statusã418}]ã ""ã[]ïŒã ããã£ã
çµæïŒ
çãŸãã@ node1ïŒã/ erdico $ cat logs / events.log 2014-06-28 17ïŒ22ïŒ43.994 [éç¥] <0.39.0> hello world 2.7 çãŸãã@ node1ïŒã/ erdico $ cat logs / access.log 2014-06-28 17ïŒ25ïŒ57.286 [info] <0.39.0> fake "Undefined Undefined" 418
ã«ãŠããŒã€onresponseããã¯
æ¢è£œã®ã³ãŒãã«é¢ããæ倧éã®äœæ¥ããã³ãããããšæããŸãã ãããã£ãŠãcowboy_reqïŒreply / 4ãåŒã³åºãåå Žæã«ãã®ã³ã°ãæ¿å ¥ãã代ããã«ãã«ãŠããŒã€èªäœã«ãã®ã³ã°ãæ¿å ¥ããŸãã ãã®ãããå€æããããã«ãçããžã®ããã¯ã®åœ¢ã§ç¹å¥ãªå ŽæãããããŸãã ããã¥ã¡ã³ãã¯ããªãã®åéã§ãã
ãé¡ã«ããœãªã¥ãŒã·ã§ã³ã¯ãã®ããã«èŠããæžã蟌ã¿ãŸã
è¯ããã°
çãŸãã@ node1ïŒã/ erdico $ cat logs / access.log 2014-06-28 17ïŒ54ïŒ44.429 [info] <0.103.0> 10.0.2.4 "GET httpïŒ// node1ïŒ2081 /" 200 2014-06-28 17ïŒ54ïŒ46.0ââ85 [info] <0.104.0> 10.0.2.4 "GET httpïŒ// node1ïŒ2081 /" 200
ãã³ããããã³ã°ããã¯
onresponseããã¯ã®ããã¥ã¡ã³ããèªãã 人ã¯ãäžèšã®ãœãªã¥ãŒã·ã§ã³ã§ã¯ããã°ãžã®æžã蟌ã¿åŸã«å³å¯ã«çããéä¿¡ãããããšããã§ã«æšæž¬ã§ããŸããã
ããã¯ãã¹ã¿ãã¯ãããã¬ãŒïŒããšãã°ããã£ã¹ã¯ãé ãïŒãå¿çæéãå¢ããããšãæå³ããŸãã
ãŸããããã¯ããªã¯ãšã¹ãã®åŠçæéããã°ã«æžã蟌ãããšã«ããå Žåããã®ã³ã°ã«è²»ããããæéã¯å«ãŸãããã¯ã©ã€ã¢ã³ãã®èŠ³ç¹ãšã¯å€§ããç°ãªãå¯èœæ§ãããããšãæå³ããŸãã
ãããã£ãŠãã¯ã©ã€ã¢ã³ãã«å¿çãéä¿¡ããåŸã«ãã°ãå³å¯ã«æžã蟌ãŸããããã«ãããäžåºŠããã¥ã¡ã³ãã確èªããŠããã¯ãããçŽããŸãã
ããæ£ç¢ºãªããã¯
access_log_hook(Status, Headers, Body, Req) -> {[{PeerAddr, _}, Method, Url], Req2} = lists:mapfoldl(fun get_req_prop/2, Req, [peer, method, url]), {ok, ReqReplied} = cowboy_req:reply(Status, Headers, Body, Req2), PeerStr = inet_parse:ntoa(PeerAddr), lager:info([{tag, access}, {peer, PeerStr}, {method, Method}, {url, Url}, {status, Status}], ""), ReqReplied. get_req_prop(Prop, Req) -> cowboy_req:Prop(Req).
åæå¯èœãªãã°
RPSã枬å®ããå Žåãåãªã¯ãšã¹ãã®ãã°ã«è¡ãæžã蟌ãŸãªãããã«ããå¿ èŠããããŸãã
èšå®ããã°ãäžèŠã§ããããšãæ瀺çã«ç€ºããŠããå Žåãããã¯ãçºçããªãããã«ããŸãã
ãã®ãããã®åŸãèµ·åã©ã€ã³ã«ã-erdico log_access falseããã©ã¡ãŒã¿ãŒãè¿œå ãããšããã°ãç¡å¹ã«ãªããŸãã
ãªãªãŒã¹ãšrelx
ãªãªãŒã¹ã¯ãããããErlangã§éçºããæ倧ã®èŠçã®1ã€ã§ãã relxã¯ããŠãŒã¶ãŒããã®çã¿ããæãããã«èšèšãããŠããŸãã ïŒãã¿ãã¬ïŒæ¬åœã§ã¯ãªãïŒ
ãã«ãããªãªãŒã¹ããã ã
ãã®ãã¡ã€ã«ã«å ¥åããåŸãmakeãåŒã³åºããšã_relãã£ã¬ã¯ããªã«ãªãªãŒã¹ããã«ããããŸãã
relx.config
{release, {erdico, "0.1"}, [erdico]}. {extended_start_script, true}.
æ¡åŒµãããèµ·åã¹ã¯ãªãããªãã§ã¯æåããŸããã§ããããåŸã§å¿ èŠã«ãªããŸãã
ãªãªãŒã¹ãéå§
çãŸãã@ node1ïŒã/ erdico $ _rel / erdico / bin / erdico console execïŒ/home/stolen/erdico/_rel/erdico/erts-6.1/bin/erlexec -boot /home/stolen/erdico/_rel/erdico/releases/0.1/erdico -env ERL_LIBS / home / stolen / erdico / _rel / erdico /ãªãªãŒã¹/ 0.1 / lib -config /home/stolen/erdico/_rel/erdico/releases/0.1/sys.config -args_file /home/stolen/erdico/_rel/erdico/releases/0.1/vm.args-console ã«ãŒãïŒ/ããŒã /çãŸãã/ erdico / _rel / erdico /ããŒã /çãŸãã/ãšã«ãã£ã³/ _rel /ãšã«ãã£ã³ Erlang / OTP 17 [erts-6.1] [source-d2a4c20] [64-bit] [async-threadsïŒ10] [hipe] [kernel-pollïŒfalse] 18ïŒ39ïŒ18.318 [æ å ±]ã¢ããªã±ãŒã·ã§ã³ã©ã¬ãŒãããŒããerdico@127.0.0.1ãã§éå§ãããŸãã 18ïŒ39ïŒ18.321 [æ å ±]ããŒããerdico@127.0.0.1ãã§ã¢ããªã±ãŒã·ã§ã³ã«ãŠããŒã€ãéå§ãããŸãã 18ïŒ39ïŒ18.343 [æ å ±]ã¢ããªã±ãŒã·ã§ã³erdicoã¯ããŒã 'erdico@127.0.0.1'ã§éå§ãããŸãã Eshell V6.1ïŒ^ Gã§äžæ¢ïŒ ïŒerdico@127.0.0.1ïŒ1>
ã芧ã®ãšããããªãªãŒã¹ã«ã¯é 延èšå®ã¯å«ãŸããŠããŸããã ãŸãããã®ããšã¯ãã¯ã©ã¹ã¿ãŒã§ã®äœæ¥ã«ã¯ããŸãé©ããªãããŒãã®ååãå ¥åããŸããã ãããããã³ãã®ä»ã®åé¡ãããã«è§£æ±ºããŸãã
ãªãªãŒã¹ã«é©åãªèšå®ãå«ãã
ãã®ããããªãªãŒã¹ãæ£ããããŒãåã§éå§ããèµ·åæã«ããŒããã¯ã©ã¹ã¿ãŒå§åŠ¹ã«æ¥ç¶ããå¿ èŠããããŸãã ãŸãããããã®èšå®ããã®ä»ã®èšå®ããæ¬ èœããŠããã³ã³ãããéžè±ããªãæ確ãªæ§æã§ãã¡ã€ã«ã«èšå®ã§ããããã«ããŸãã
ãŸã第äžã«ããã¹ãŠãããŒãã³ãŒãã§ãã
ã«ãŒãã«ãªãã·ã§ã³sync_nodes_optionalãšsync_nodes_timeoutã«æ³šæããŠãã ãã-äžç·ã«èµ·åæã«æå®ãããå§åŠ¹ã«ããŒããæ¥ç¶ããããããããã®å¿çã1ç§éåŸ ã¡ãŸãã ãã®2ç§éã¯ãã«ãŠã³ã¿ãŒã®globalïŒsyncïŒïŒã®åŒã³åºãããããã¯ãããéå§æã®äžå¿ èŠãªæ»äº¡ããªããªããŸãã
vm.argsã§ã¯ãæããã«ãä»ã®ãªãã·ã§ã³ãèšè¿°ã§ããŸãã ãã ãã-nameãŸãã¯-snameãæå®ããªãå ŽåããªãªãŒã¹ã¯éå§ãããŸããã
ããã§ããªãªãŒã¹ãå®å šã«2çªç®ã®ããŒãã«ã³ããŒããããšãã§ããã¯ã©ã¹ã¿ãŒã®èµ·ååŸã«éæ³ã®ããã«çµã¿ç«ãŠãããŸã-curlã«ãããã§ãã¯ã«åæ ŒããŸããã 2çªç®ã®ããŒãã®ã¢ãŒã©ã³ãã€ã³ã¹ããŒã«ãããŠããªãããšãéèŠã§ããã€ãŸãããªãªãŒã¹ã¯èªçµŠèªè¶³ã§ãã
ãªãªãŒã¹ã¹ã¯ãªããã«ããå€æ°ã®é瀺
relxãæäŸããåªããæ©èœã®1ã€ã¯ãå€æ°ã®æ¡åŒµã§ãã _rel / erdico / bin / erdicoãªãªãŒã¹èµ·åã¹ã¯ãªããã§RELX_REPLACE_OS_VARSãšããè¡ãèŠã€ãããšããããã©ã®ããã«çºçãããã確èªã§ããŸãã ãã¹ãŠãåçŽãªã®ã§ãæè»æ§ãããããŸããã
ãã©ã¡ãŒã¿ãŒåãããæ§æ
å§åŠ¹ã®ãªã¹ãããã©ã¡ãŒã¿ãŒåããŸãã
次ã®ããã«å®è¡ããŸãïŒ
{sync_nodes_optional, [${CLUSTERNODES}]}
次ã®ããã«å®è¡ããŸãïŒ
RELX_REPLACE_OS_VARS = 1 CLUSTERNODES = erdico @ node2 _rel / erdico / bin / erdicoã³ã³ãœãŒã«
1ã€ã®åé¡ïŒå€æ°ãå ¬éããªããšããªãªãŒã¹ãéå§ãããŸããã
ããã¯ïŒãªãªãŒã¹ã¹ã¯ãªããã«ããå€æ°ã®éé瀺
ãªãªãŒã¹ãé瀺ãšéé瀺ã®äž¡æ¹ã§éå§ããããã«ãç§ã¯ãã®ãããªããã¯ãæãã€ããŸããã ãšã«ããæ¡åŒµã¯upstartã¹ã¯ãªããã«éãããããã人éã®æ§æãåæã«èªã¿åããããããã³ã¡ã³ãå ã®ãã¹ãŠã®å€æ°ãé衚瀺ã«ããã³ã¡ã³ããå®æãããå€æ°ãè¿œå ããŸãã ãªãªãŒã¹ããã®ãŸãŸã§ããŸãã¯è¿é£ããŒãã®æ瀺ã䌎ã£ãŠå®è¡ã§ããããã -
RELX_REPLACE_OS_VARS = 1 CLUSTERNODES = "erdico @ node2ãerdico @ node1" NL = $ '\ n' _rel / erdico / bin / erdicoã³ã³ãœãŒã«
ã³ã³ãããã¯ïŒååã®é瀺ãšéè€
ãããã¯ã·ã§ã³ãšç«¶åããããšãªããæ±ããæã§ãªãªãŒã¹ãéå§ã§ããããã«ããŸãããã ãããè¡ãã«ã¯ãããŒãåããã©ã¡ãŒã¿ãŒåããå¿ èŠããããŸãã åæã«ããã©ã¡ãŒã¿åã«ãããããã«ãã«ããŒã ïŒFQDNãå«ãïŒãå ¥åããŸãã
äžæ¹ã§ã¯ãvm.argsãããŒãåãªãã§æ®ãããšã¯ã§ããŸããã äžæ¹ã以åã®ããã¯ã§ã¯ãæ§æã«è¡ãè¿œå ã§ããŸãããåé€ããããšã¯ã§ããŸããã 3çªç®-Erlangã«ããã€ãã®ååãä»ããå Žåã圌ã®éžæã¯ããŸãäºæž¬ã§ããŸããã
vm.argsã§ã¯ã-extraãã£ã¬ã¯ãã£ãããã©ã¡ãŒã¿ãŒã®å¥ã®ã»ã¯ã·ã§ã³ã«é²ã¿ãã«ãŒãã«ã«ãã£ãŠèªã¿åãããªãåŸã«æžã蟌ãŸãããã¹ãŠã®ãã®ãå€æããŸããã ããã䜿çšãããã®ã§ãã
ãã©ã¡ãŒã¿åãããéå§ã¯æ¬¡ã®ããã«ãªããŸãã
RELX_REPLACE_OS_VARS = 1 CLUSTERNODES = "'erdico@node2.example.net'ã 'erdico@node1.example.net'" FQDN = `hostname -f` NL = $ '\ n' _rel / erdico / bin / erdicoã³ã³ãœãŒã«
ãã«ãdebããã±ãŒãž
Debianã¯éçºè ã«å€§ããªèŠçãäžããŸãã çã¿ã¯debianãã£ã¬ã¯ããªå ã®ãã¡ã€ã«ã®æããå§ãŸãããããžã§ã¯ãã®ã«ãŒããdebianãã£ã¬ã¯ããªã®ä»£æ¿ã®å ŽæããŸãã¯åéãããããã±ãŒãžãæããããããã®ãã¹ãæå®ã§ããªãããšããç¶ããŸãã
åéãããããã±ãŒãžã¯ããããžã§ã¯ããœãŒã¹ããããã£ã¬ã¯ããªã®1ã¬ãã«äžã®ãã£ã¬ã¯ããªã«éä¿¡ãããããšãããã£ãŠããŸãã ãããã£ãŠããã®ãã¹ãŠã®æ±ç©ã¯æ·±ãåããããã¹ãã§ãã
upstart configã§ãããéåžžã«è²§åŒ±ãªã¹ã¯ãªããæ©èœããããããé©åãªç°å¢ãæºåããå¥ã®conf_erdico.shã¹ã¯ãªããã§éå§ã¹ã¯ãªãããã©ããããå¿ èŠããããŸããã
ã©ã¬ãŒã¯ãã·ã³ããªãã¯ãªã³ã¯ã®äžã«ãããã°ãæžã蟌ããªãããšãå€æããŸããïŒfilelibã®ç¹æ§ã«ãããensure_dir / 1ïŒã ãã®ããããã°ãžã®ãã¹ã眮ãæããããã«ãæ§æã«ããã¯ãæ¿å ¥ããå¿ èŠããããŸããã
å®éãå€éšã¹ã¯ãªããã¯ãšã«ããæžãããŠããã®ã§ãsedã䜿çšããŠæ§æå ã®ãã¹ãŠã®çœ®æãè¡ãããšã¯ãã§ã«å¯èœã§ããã ãã®ãŸãŸã«ããŠãããšãæŠå¿µå®èšŒããããŸãã
䜿çšããããããã³ã°ã®ç§cks
ïŒ å
šã³ããã ïŒ
- pkg / erdicoã¢ã»ã³ããªã®ãã£ã¬ã¯ããªãäœæãããŸãããã®ãã£ã¬ã¯ããªã«ã¯ããã¹ãŠã®gibletsãšè¿œå ãã¡ã€ã«ãå«ãdebianãã£ã¬ã¯ããªãé 眮ãããŸã
- æäžäœã®makefileã¯ãããã±ãŒãžãã£ã¬ã¯ããªå ã®makefileãåç §ããdebã¿ãŒã²ãããååŸããŸãã
- çŸåšã®ãªãªãŒã¹ããã«ãããããã«ãæäžäœã§ãã¹ãŠã®ïŒãã«ãïŒåŒã³åºããè¡ãããã®ããã±ãŒãžãã£ã¬ã¯ããªå ã®makefile
- ã¹ã¿ãŒãã¢ããã幞ãã«ããããã«ããã©ã¢ã°ã©ãŠã³ããã©ã¡ãŒã¿ãŒãã¹ã¿ãŒãã¢ããã¹ã¯ãªããã«äžããããŸãã åŸæ¥ã®initã䜿çšããå Žåããã©ã¡ãŒã¿ãŒstartãstopãpingã䜿çšã§ããŸã
- èµ·åã¹ã¯ãªããã¯ãæ§æãç·šéãããšãã«ãçæããããã¡ã€ã«ãå³å¯ã«å ã®ãã¡ã€ã«ã®é£ã«çœ®ãããã/ var / lib / erdico /ããã·ã³ããªãã¯ãªã³ã¯ãäœæããå¿ èŠããããŸãã
- ãã£ã³ãã®èšå®ã§å€æ°ãé瀺ããããã«ããã¯ã貌ãä»ãããšããproplists workã®æ©èœã䜿çšãããŸãã
- ã·ã§ã«ã®å©ããåããŠã/ etc / erdico.confã®ãã¹ããªã¹ãïŒFQDNïŒã¯ããŒãã®ãªã¹ãã«å±éãããŸãïŒåäžåŒçšç¬Šã§ãæ£ç¢ºã«ã¢ãã ãããããã«ïŒ
çµã¿ç«ãŠãã€ã³ã¹ããŒã«ãæ§æãèµ·åïŒ
æåã®ïŒã¢ã»ã³ããªïŒãã·ã³
çãŸãã@ node1ïŒã/ erdico $ make deb çãŸãã@ node1ïŒã/ erdico $ sudo dpkg -i pkg / erdico_0.1_amd64.deb çãŸãã@ node1ïŒã/ erdico $ scp pkg / erdico_0.1_amd64.deb node2ïŒ çãŸãã@ node1ïŒã/ erdico $ sudo vim /etc/erdico.confïŒCLUSTERHOSTS = "node1.example.net node2.example.net" çãŸãã@ node1ïŒã/ erdico $ sudo service erdico start
2å°ç®
çãŸãã@ node2ïŒã$ sudo dpkg -i erdico_0.1_amd64.deb çãŸãã@ node2ïŒã$ sudo vim /etc/erdico.confïŒCLUSTERHOSTS = "node1.example.net node2.example.net" çãŸãã@ node2ïŒã$ sudo service erdico start
ããŸãããïŒ
äž¡æ¹ã®ãã·ã³ãåèµ·åããåŸ
çãŸãã@ node1ïŒã$ curl node1ïŒ2080 å€= 1 çãŸãã@ node1ïŒã$ curl node2ïŒ2080 å€= 2 çãŸãã@ node1ïŒã$ curl node1ïŒ2080 å€= 3 çãŸãã@ node1ïŒã$ curl node2ïŒ2080 å€= 4 çãŸãã@ node1ïŒã$ tail -5 /var/log/erdico/access.log 2014-06-29 00ïŒ43ïŒ03.044 [info] <0.380.0> 10.0.2.4 "GET httpïŒ// node1ïŒ2080 /" 200 2014-06-29 00ïŒ54ïŒ34.563 [æ å ±] <0.424.0> 10.0.2.4 "GET httpïŒ// node1ïŒ2080 /" 200 2014-06-29 00ïŒ54ïŒ36.932 [æ å ±] <0.425.0> 10.0.2.4 "GET httpïŒ// node1ïŒ2080 /" 200 2014-06-29 00ïŒ56ïŒ10.709 [æ å ±] <0.383.0> 10.0.2.15 "GET httpïŒ// node1ïŒ2080 /" 200 2014-06-29 00ïŒ56ïŒ14.490 [æ å ±] <0.384.0> 10.0.2.15 "GET httpïŒ// node1ïŒ2080 /" 200
çŽæãããREST
ããã«ããã
ãã¢
çãŸãã@ node1ïŒã$ curl node1ïŒ2080 å€= 1 çãŸãã@ node1ïŒã$ curl node2ïŒ2080 å€= 2 çãŸãã@ node1ïŒã$ curl node1ïŒ2080 / inc / 400 å€= 402 çãŸãã@ node1ïŒã$ curl node2ïŒ2080 å€= 403 çãŸãã@ node1ïŒã$ curl node1ïŒ2080 å€= 404
é埳
人çã¯èŠçã§ãã
ã©ã¬ãŒã¯åªããŠããŸãããæ§æã®æè»æ§ã«æ¬ ããŠããŸãïŒããšãã°ãæ§æããšã«ã«ãŒããã£ã¬ã¯ããªãšããã©ã«ãã®ãã¡ã€ã«ãã°ãªãã·ã§ã³ãèšå®ãããªã©ïŒã
ã«ãŠããŒã€ã¯è¯ãã§ãããããã©ãŒãã³ã¹ãäœäžããªãããã«ããããã©ã®ããã«æ©èœããããç解ããå¿ èŠããããŸãã
Debianã¯åªããŠããŸããããã®ããã®ããã±ãŒãžã®ã¢ã»ã³ããªã¯ããã¥ãŒã¿ã³ãã®ãã¥ãŒã¿ã³ãã«ãã£ãŠãè¡ãããŸããã
Upstartã¯åªããŠããŸããããµãŒãã¹æ§æã§è¡ãã«ã¯ããŸãã«ãå°ãªããããããžãã¯ãè¿œå ã®ã¹ã¯ãªããã«è¿œå ããå¿ èŠããããŸãã
Erlangã¯ã圌ãç¥ããªã人ããµããŒãããããã«ã¢ããªã±ãŒã·ã§ã³ãæäŸããå¿ èŠããããŸã§ã¯è¯ãã§ãã
ã¢ãŒã©ã³ã«ã¯äŸåé¢ä¿ãããŒãžã£ãŒããããŸãããæ©èœããŸãããäŸåé¢ä¿ã®å°çã®åé¡ã¯è§£æ±ºãããŠããŸããã
Erlangã®ãªãªãŒã¹ãã«ãã¯ãããã»ã©ã§ã¯ãããŸããããäŸç¶ãšããŠèŠçã§ãã Relxã¯ã³ããããåŸ ã£ãŠããŸãããããã䜿çšããªããã°ãããã䜿çšããã®ã¯ãŸã äžäŸ¿ã§ãã ããã«ãäŸåé¢ä¿ã®ã©ããã«ã·ã³ããªãã¯ãªã³ã¯ã®ãµã€ã¯ã«ãŸãã¯ã¢ã»ã³ãã«ããããªãªãŒã¹ãããå Žåãããã¯ãããããªããŸãã
ãã®ã¢ããªã±ãŒã·ã§ã³ã§ä»ã«ã§ããããš
ãŸããã«ãŠã³ã¿ãŒã®è€è£œãå®è¡ã§ããŸãã ãã ããã¯ã©ã¹ã¿ãŒã®ãã¹ãŠã®ããŒãã«ååŒã³åºãã®éç¥ãéä¿¡ãããšãããã«ããã¯ãçºçããŸãã
第äºã«ãèšå®ã§æå®ããããã€ããŒãåžžã«pingããããã»ã¹ãè¿œå ã§ããŸãã ããããªããšãã¢ãŒã©ã³ã¯ãããã¯ãŒã¯ã®åæã«ã²ã©ãèããããŸããã
第äžã«ãã¹ããŒã¿ã¹ä»ãã®ãã³ãè¿œå ããŸãã ãã®ã¢ããªã±ãŒã·ã§ã³ãå®è¡ãããŠããã¯ã©ã¹ã¿ãŒã®ããŒããšããŠã£ã¶ãŒããçŸåšå®è¡ãããŠããããŒãã衚瀺ããŸãã
4çªç®ã«ããã¹ã¿ãŒãçŸåšé 眮ãããŠãããã¹ããããããŒã«æå®ããŸãã ååã«ã¹ããŒããªã¯ã©ã€ã¢ã³ãã¯ãããŒãéã§ãã©ãã£ãã¯ãé§åããªãããã«ã次åããã«ããã«è¡ãããšãã§ããŸãã
第5ã«ãèšå®ãããã¹ãŠã®ããã¯ãåé€ããsedãšåœŒã®å人ãšã®ãã¹ãŠã®çœ®æãè¡ããŸãã
第6ã«ãå¥ã®ãããžã§ã¯ãã§ã«ãŠããŒã€ã©ã¬ãŒãã³ãã«ã®onresponseããã¯ãé 眮ãããã©ãŒãããã¢ãã ãèŠæ±ããããã£å€ã«èªåçã«å€æããæ¹æ³ãåŠç¿ã§ããŸãã ããã«ãåŠçæéããªã¯ãšã¹ããªã©ã®ãã©ãã£ãã¯ãªã©ãããããçš®é¡ã®ã¡ããªãã¯ãæŽçã§ããŸãã
7çªç®ã«ã log4erlã調ã¹ãŸã ã
8çªç®ã«ã epmãåŠã³ãŸã ã æ倧ã®ã¿ã¹ã¯ãšããŠãäŸåé¢ä¿ãããŒãžã£ãŒãšåéãäœããŸãããã