//ã¬ãªã¬ãªã»ã¬ãªã¬ã€
ãç§ã¯å¥Žé·ãvyæãå§ããŸããã 圌ãã¯äºåã«ãã¹ãŠãç¥ã£ãŠããŸãã 圌ãã«ã¯åŒ·ã信念ããããŸããã
//ãŠãŒãžã³ã»ã·ã¥ã¯ã«ãã®åã«åºã¥ããããŒã¯ã»ã¶ã«ããã®æ ç»ããã«ã»ã¶ã»ãã©ãŽã³ã
æ°å¹Žãš1æ¥ãéããã®ã§ãæ°ãããããžã§ã¯ããå§ãã人ããèãããèªãã ãããããšã¯ãããŸããã ãREST APIãšMVCã®ãµãŒããŒãšã³ãžã³ã䜿ã£ãŠéä¿¡ããŠãã ãã ã ã æåã¯ããããã®åèªã«ã¯1ã€ã®ãœãŒã¹ãããããã¹ãŠã®ã¹ãã¢ã«é ä¿¡ãããæ¬ããæ€çŽ¢ãšã³ãžã³ã®æäžéšã®ã©ããã«éçºè ããŸã³ãåããèšäºããããšèããŸããã 圌ããRESTãšMVCã«ãã£ãŠç解ããŠããããšã圌ãããèŠã€ãããªããããªãã¯å¿ã«å·ã€ããããŸãã MVCã§ãã¹ãŠãæ確ã«ãªã£ãŠããŸããããã«ã€ããŠã¯é·ãéæžããŸããããäœãå€ãã£ãŠããŸãããæªåããŠããã ãã§ããGoogleImagesã«ãmvcããšå ¥åããã ãã®äŸ¡å€ããããŸãã RESTã«ã€ããŠã¯ã圌ãã¯æ¬¡ã®ããã«çããŸãïŒãŸãããã©ãŠã¶ã®GUIãšã¢ãã€ã«ã¢ããªã±ãŒã·ã§ã³ãããµãŒããŒã¡ãœãããåŒã³åºãå¿ èŠããããŸãïŒããšãã°ã setUserCityïŒuserIdãcityIdïŒãŸãã¯calculateMatrixïŒdataïŒãŸãã¯startVideoConverterïŒoptionsãsourceãdestinationïŒéãè² è·ãšRESTã¢ãŒããã¯ãã£ããã¹ãŠã解決ããŸãã 次ã«ããŸã æ£ããä¿¡ä»°ã§çããŠããŠãæŠãã«ç±å¿ã§ããã®äžçã§äœãèµ·ãã£ãŠããããæ£ç¢ºã«ç¥ã£ãŠãã人ã ã®ããã«ããã§ã«ç®ãäžžããªã£ãŠãã質åãããŸãã ããã§ãç§ãã¡ãããªããšäžç·ã«ããéæºå°ã§ã çšèªã®å€§æšäºãæ€èšããããšãã§ããŸãã
MVCã«é¢ãã質å
æåã®è³ªåïŒ ãMVCãšã¯ïŒãã®å³ãæããŠãã ããã ç§ãåŸããã®ïŒ
MVPã«ãšã£ãŠã¯ããã®ãããªç解ãšã¹ããŒã ã§ãååã§ãããçãããšããããŸããã
2çªç®ã®è³ªåïŒãŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹çšã«èšèšããããã®ãã¿ãŒã³ããµãŒããŒã«ã©ã®ããã«é©çšã§ããŸããïŒ
ããã«ãã¹ãŠã®å¯èœãªçãããªã¹ãããããšã¯ã§ããŸããã ããããæ£ãããã®ã¯éåžžã«ãŸãã§ããããã®ãããªãã®ã§ãïŒMVCãµãŒããŒã§ã¯ããŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ããµãŒããŒã«å®å šã«å®è£ ãããŠããå Žåã«ã®ã¿å¯èœã§ãã ãã©ãŠã¶ã³ãŒãã¯ãããŸãããã€ã³ã¿ãŒãã§ã€ã¹ã¯ãµãŒããŒäžã§çæããããã©ãŠã¶ã¯åã«ããã衚瀺ããŸãã
3çªç®ã®è³ªåïŒ MVCãšRESTãã©ã®ããã«çµã¿åãããŸãããïŒ
ç§ã¯æµæããããšã¯ã§ããŸãããç§ã¯äžè¬çãªãªãã·ã§ã³ãå ¬éããŸãïŒ
- ãŸããç§ãã¡ã¯MVCãRESTã䜿çšããŠå®è£ ããŠããŸã
- ãŸããMVCã䜿çšããŠRESTãå®è£ ããŸãã
ããã«ã€ããŠã³ã¡ã³ãããããšã¯ã§ããŸããã çšèªã®å€§æšäºãæ±ã£ãŠããããšã¯å®å šã«æããã§ãããããã«ã€ããŠã¯åŸã§è©³ãã説æããŸãã
RESTã«é¢ãã質å
æåã®è³ªåïŒãŸããç§ã®èŠªæãªã人ããããã®setUserCityãcalculateMatrixãstartVideoConverterãRESTã«å®è£ ããã«ã¯ã©ãããã°ããã§ããïŒ
ç§ã®çãïŒRESTã®ãã€ã³ãã¯ããã¡ã€ã«ããšã«åºæã®URLãæã¡ãGETãPUTãPOSTãDELETEãšããHTTPã¡ãœããã®ã¿ããã®ãã¡ã€ã«ã§çæã§ããããšã§ãã ãããã£ãŠãããããã¹ãŠã®setUserCityãHTTP POST / user / idã«çœ®ãæããŠãã·ãªã¢ã«åããããŠãŒã¶ãŒå šäœãããã«éä¿¡ããŠãã ããã ãŸãã¯ãå¥ã®ãªãã·ã§ã³ãšããŠããŠãŒã¶ãŒãã©ã¡ãŒã¿ããšã«åå¥ã®URLãå¿ èŠã§ãã æããã«ãHTTPã¡ãœãããä»ããŠåŒã³åºãããCRUDã¡ãœããïŒäœæãèªã¿åããæŽæ°ãåé€ïŒã¯ãããŒã¿ããŒã¹å ã®ãã¡ã€ã«ãŸãã¯ãªããžã§ã¯ããæäœããã®ã«ååã§ãã ããšãã°ããã©ãŒã ãšãã©ãŒã ãå ¥åããã«ã¯ãããããããŒãã«ã«ä¿åããããã«åé€ããŠç·šéããŸãã ããããææ°ã®ã¢ããªã±ãŒã·ã§ã³ããã®ãããªåå§çãªèª²é¡ã«æžããããšã¯ã§ããŸããã ãããŠãã»ãšãã©ã®å Žåãéçºè ã¯RESTã«ãã£ãŠãã®æ£å察-RPC ïŒãªã¢ãŒãããã·ãŒãžã£ã®åŒã³åºãïŒãCRUDã ãã§ãªãå¿ èŠãªæ°ã®ã¡ãœãããäœæã§ããå Žåãããã³1ã€ã®URLãã©ã®ãŠãŒã¶ãŒã«å¿ããŠå®å šã«ç°ãªãåçãæäŸã§ããããç解ããŸã圌ã¯ãã©ã®ãããªãã©ã¡ãŒã¿ãŒã§ãã©ã®ãããªé åºã§åŒã°ããŸããã ããã¯ãŸã£ããRESTã§ã¯ãããŸãããããšãã°ãHabrã¯http://habrahabr.ru/tracker/ã§ç§ãã¡ããããã«ç°ãªããªã¹ããæäŸããŠãããããã¯GoogleãFacebookãGithubãªã©ã®ã©ãã«ã§ããããŸãã ãããã£ãŠã誀解ãæãRESTã¯ãŒããåé€ããAPIãå®è¡ããã°å®äºã§ãã
2çªç®ã®è³ªåïŒããããŸãããããªãã«ãšã£ãŠRESTãšã¯äœã§ããïŒ
ããã§ã®æèŠã®ç¯å²ã¯MVCã»ã©åºç¯ã§ã¯ãããŸãããã çšèªã®å€§æšäºã¯ããã§ãïŒäžèšã§ç€ºããããã«ãRESTã¯RESTãšããŠãŸã£ããç解ãããŠããŸããããRESTã«é¢ããç¬æçãªæç®ãç 究ããRESTããµããŒããããã¬ãŒã ã¯ãŒã¯ãæ¢ããŠããã®çµæãRPCãšRESTã®ãã€ããªãããç»å ŽããŸãã ãããŠææªãªããšã«ãããã¯ãã¹ãŠç¡æèã«èµ·ãããŸãã
3çªç®ã®è³ªåïŒ RESTãã©ã®çšåºŠæ£ç¢ºã«è² è·ã軜æžãããã説æã§ããŸããïŒ
ç§ã®çãïŒããã¯STATElessã§ãã ãããã¯äœãæå³ãããªããµãŒããŒäžã®ç¶æ ãæŸæ£ããå¿ èŠãããã®ãââ-以äžã§èª¬æããŸãã
STATElessã®æå³ã¯äœã§ãã
ã¹ã±ãŒãªã³ã°ããå Žåã
æ§ç³åšæ代åŸæã®ããã°ã©ããŒã«å¹žçŠã蚪ãã
ç§ã®ããã°ã©ãã³ã°ã®äžç芳ãçºå±ããŠããé ïŒ1997-2002ïŒãäž»æµã¯3å±€ã¢ããªã±ãŒã·ã§ã³ã¢ãŒããã¯ã㣠ïŒãŠã£ã³ããŠã¯ã©ã€ã¢ã³ããã¢ããªã±ãŒã·ã§ã³ãµãŒããŒãDBMSïŒã§ããã ãŸããã¯ã©ã€ã¢ã³ãã¯RPCãä»ããŠã¢ããªã±ãŒã·ã§ã³ãµãŒããŒãšããåãããŸããã ãã¹ãŠãSTATEfulã§ããããå€ãã®ãã·ã³ã§ã¢ããªã±ãŒã·ã§ã³ãµãŒããŒã®ã¹ã±ãŒãªã³ã°ãå¿ èŠãšãããããªã¿ã¹ã¯ã¯ãããŸããã§ããã ãã®ããã2002幎ãã2012幎ãŸã§é·å¹Žã«ããã£ãŠãRESTãšSTATElessã«èããè² è·ã®é«ãã·ã¹ãã åãã®æ¬æ ŒçãªSTATEfulããã°ã©ãã³ã°ãšRPCã倢èŠãŠããŸããã çŸåšãã»ãšãã©ã®èšèªãšãã©ãããã©ãŒã ã«Node.jsãPythonã®TwistedãRuby Event Machineãªã©ã®ç¬èªã®ã€ãã³ãã«ãŒããœãªã¥ãŒã·ã§ã³ãããå Žåãç§ã¯å®å šã«å¹žããªäººã®ããã«æããSTATElessãå¿ èŠãªçç±ãã REST APIããã³MVCã®ãµãŒããŒãšã³ãžã³ã䜿çšããŠããããåãããŠã¿ãŸããã ã ã
ç¶æ ã®ããè² è·ã®é«ãã¢ããªã±ãŒã·ã§ã³ãµãŒããŒãäœæããããšãå¯èœã«ãªã£ããããããã»ã¹ã®å¯¿åœãé·ããªãããã¹ãŠã®ããŒã¿æ§é ãã¡ã¢ãªã«æ®ããããŒã¿ããŒã¹æ¥ç¶ããã£ãã·ã¥ãã¿ã€ããŒãæ®ããã¡ã¢ãªã«è€éãªã¢ãã«ãå±éããŠã1ãæéããã«çœ®ãããšãã§ããŸãã ãŸãããã®ã¢ãã«ã䜿çšãããšãã¯ã©ã€ã¢ã³ãéšåã¯APIãä»ããŠå¯Ÿè©±ã§ããŸãã ãããŠä»ãå°ãç®è¡ã§ãããŠãŒã¶ãŒç¶æ ã32Kbã§ã16GBã®ã¡ã¢ãªããããšæ³åããŠãã ãããããã¯524288ïŒ50äžäººä»¥äžïŒãŠãŒã¶ãŒã«ååã§ãã
解決ãã¹ãå¯äžã®ããšã¯ã1ã€ã®ã»ãã·ã§ã³ã«é¢é£ãããã¹ãŠã®ãªã¯ãšã¹ããåãããã»ã¹ã«éä¿¡ããæ¹æ³ã§ãã ã€ãŸããã»ãã·ã§ã³ããµãŒããŒãšããã»ã¹ã«ãåºå®ãããå¿ èŠããããŸãïŒããã»ã¹ã¯å€ãã®ãµãŒããŒã®ããããã«ååšã§ããŸãïŒã ãã®ããã«ã IPã¹ãã£ããã³ã° ïŒip-stickyïŒããã³cookieïŒcookie-stickyïŒãå®è£ ããããŒããŠã§ã¢ããã³ãœãããŠã§ã¢ãã©ã³ãµãŒããããŸãã ãã¡ãããåããã»ã¹ã¯ããã®ãããªèå¥åãæããªããã¹ãŠã®ãªã¯ãšã¹ãã«ããã®èå¥åãä»ããCookieã§ããŒã¯ããå¿ èŠããããŸãã èå¥åãããå Žåã¯ã¹ãã£ããã³ã°ãæ©èœãããªãå Žåã¯ã©ãŠã³ãããã³ãŸãã¯ããè€éãªãã©ã³ã·ã³ã°ã¢ã«ãŽãªãºã ãæ©èœããŸãã
ãããŠãMVCã§ã¯ãŸã å®äºããŠããŸãã
MVCã¯å šå¡ã«ãã¢ãã«ããã¥ãŒãã³ã³ãããŒã©ãŒãåé¢ããããšããããŠãããå¿ èŠãªå Žæãšå¿ èŠã§ãªãå Žæãèããã«æããããšãæããŸããã ããããåã³ã³ããŒãã³ãã«é¢ããå ±éã®ç解ã¯ãŸã 確ç«ãããŠããŸããã ã¢ãã«ã«ãã©ã¡ãŒã¿ãŒã®ã¿ãæ®ããã¡ãœããã¯ã³ã³ãããŒã©ãŒã«è»¢éãããŸãããäžéšã®ã¢ãã«ã§ã¯ããŒã¿ããŒã¹ããèªåèªèº«ãä¿åããã³åŸ©å ããæ¹æ³ãç¥ã£ãŠããŸãããä»ã®äººã¯ãã®ã¢ãããŒãã®ççãªå察è ã§ãããããŒã¿ããŒã¹ã«ã¢ã¯ã»ã¹ããããã«å¥ã®ããŒã¿ã¢ã¯ã»ã¹ã¬ã€ã€ãŒãå²ãåœãŠãŸãã ãããã«ããã çšèªçãªçœå®³ã«å¯ŸããŠäœããããå¿ èŠããããŸãã
ç§åŠããã³æè¡ã§äœ¿çšããã®ãæ £ç¿ã§ããããã ãã¢ãã«ããšããèšèã䜿çšããããšãææ¡ããŸããã€ãŸããå®éã®ã·ã¹ãã ããã³/ãŸãã¯ããã»ã¹ã®ç°¡ç¥åãããè¡šçŸã§ã ã ã¢ãã«ã«ã¯ãã©ã¡ãŒã¿ãšã¡ãœãããå«ãŸããããšããããŸãã ãã ãããããã®ã¡ãœããã¯å®çŸ©ã«é©åããå¿ èŠããããŸãã ããšãã°ãCRUDã¡ãœããã¯å®éã®ãªããžã§ã¯ãã®ã¢ãã«ã®äžéšã«ããããšã¯ã§ããŸãããå®éã®ãªããžã§ã¯ãã¯ããèªäœãäœæãŸãã¯åé€ã§ããªãããã§ãã CRUDã¯ã¢ãã«ã®äžéšã§ã¯ãªããã¹ãã¬ãŒãžã¬ã€ã€ãŒAPIã®äžéšã§ãããæç»ã¡ãœããã¯ã°ã©ãã£ãã¯ã¹ã¬ã€ã€ãŒAPIã®äžéšã§ãã ã¢ãã«ã¯ããã®ã¹ãã¬ãŒãžããããã¯ãŒã¯äžã®äŒéãããã³ã«ããã©ãŠã¶ã®ã¬ã³ããªã³ã°æ©èœãªã©ã«ã€ããŠäœãç¥ããªãã¯ãã§ãã ã¢ãã«ã®ã¡ãœããã®è¯ãäŸã¯æ¬¡ã®ãšããã§ããSteppingMotor.setSpeedïŒxïŒã¢ãã«ãç©çããã€ã¹ãã©ã€ããŒã§ããå ŽåããŸãã¯ç©çããã€ã¹ã®ã¢ãã«åã«äœ¿çšãããå Žåã å¥ã®è¯ãäŸã¯ãEquation.calculateRootsïŒïŒã®ãããªæ°åŠã¢ãã«ã§ãã 3çªç®ã®äŸã¯æ å ±ã¢ãã«ã§ããããšãã°ãPatient.assignBedïŒbedIdïŒã§ãã
ããã¥ãŒããšãã³ã³ãããŒã©ãŒããšããèšèã¯å¯èœãªéãé¿ããããŸãããããã¯ãåã ã®éçºè ã®é ã®äžã§ã¯ãå®å šã«äºæž¬äžå¯èœã§å¥åŠãªæå³ãæã€ããšãã§ããããã§ãã 代ããã«ãããã³ãã¬ãŒããããã³ã³ãããŒã«ãïŒãŸãã¯ããŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ã³ã³ãããŒã«ãïŒãããªã¯ãšã¹ãã«ãŒã¿ãŒããªã©ã®ããå ·äœçãªæŠå¿µã䜿çšããããšããå§ãããŸãã
æ§ç³åšæ代åŸæã®ã¢ããªã±ãŒã·ã§ã³éçºã®ãã³ã
ã€ãã³ãæåã®ãœãªã¥ãŒã·ã§ã³ã®ãã¡ãç§ã¯å人çã«Node.jsã䜿çšããŠããŸããããããã¯æ®éçãªãã®ã«ã§ãããšæããŸãã
1. RPCããã³STATEfulã®ååã«åŸã£ãŠã¯ã©ã€ã¢ã³ããšãµãŒããŒïŒãã©ãŠã¶ãŒã¢ããªã±ãŒã·ã§ã³ãšãããã¯ãŒã¯APIïŒãåé¢ããã»ãã·ã§ã³ãããã»ã¹ã«æ¥çããŸãã
2. RAMã䜿çšãã絶ããããŒã¿ããŒã¹ã«ã¢ã¯ã»ã¹ããªãã§ãã ããã STATEfulã¯ãé«éã«ãŒãã¢ããªã±ãŒã·ã§ã³ãäœæãã絶奜ã®æ©äŒã§ããããã¯ãã€ãã³ãã«ãŒããã¬ãŒã ã¯ãŒã¯ãéããããã³ã°I / Oãå¿ èŠãšããããã§ã¯ãªããã¡ã¢ãªãæ£ãã䜿çšããããã§ãã I / Oæäœã®ã»ãšãã©ã¯ã¯ãšãªåŠçäžã«è¡ãå¿ èŠãããããŸãããèªã¿åãã¯ããã¢ã¯ãã£ããã€äžŠåã«è¡ãããšãã§ããæžã蟌ã¿ã¯é 延ããŸãã STATEfulããã»ã¹ã§é·ã幞ããªäººçãéãã¢ããªã±ãŒã·ã§ã³ã®ã¡ã¢ãªã«ããŒã¿ãå±éããããã·ã¥ããªããžã§ã¯ããã¢ã¬ã€ãæ§ç¯ããŸãã
3.ç°ãªãããã»ã¹éã§ãZeroMQïŒããã³ä»ã®MQïŒãTCPãHTTPãIPCãããã³ãã®ä»ã®ãããããã®ãä»ããŠå¯Ÿè©±ããŸãã ãããã£ãŠãããŸããŸãªããã»ã¹ã®ããŒã¿ã¯ããããã©ã®ãããªçš®é¡ã®ããŒã¿ã§ãããã«å¿ããŠãã¡ã¢ãªå ã§è€è£œïŒå ±æããŒã¿ã®å Žåã¯ãã£ãã·ã¥ïŒãããããã¹ãã£ãããŒãã»ãã·ã§ã³ã«åå²ããããããã»ã¹ééä¿¡ãéããŠçžäºã«åæã§ããŸãã
ãããã«
æ¯åãã¶ã€ã³ãã¿ãŒã³ãåèããŸãã ä»ã®äººã®æ±ºå®ã圌ãã®æš©åšã®ããã ãã«åããªãã§ãã ããã圌ãã¯æªãããšã§ã¯ãªããããããŸããããããªãã®å Žåã¯ããã§ã¯ãããŸããã ææ決å®ã¯å¯èœã§ããå¿ èŠã§ãããå®å šãªæ¶åãšç解ãå¿ èŠã§ãã
ãããžã§ã¯ããéå§ããåã«å¿ ãååãšçšèªã確èªãããããªãã¯ãã¡ã€ã³ã§èŸæžãäœæããããŸã§ããã調æŽããŠãã ããã ç§ãã¡ã®æ代ã«èç©ãããæŠå¿µã®æ··ä¹±ã«ãããããšãåãèšèã䜿ã£ãŠããŠããããªããåãããšã«ã€ããŠè©±ããŠãããšç¢ºä¿¡ããããšã¯äžå¯èœã§ãã
UPDïŒä»¥äžã®ã³ã¡ã³ãã¯ã çšèªçœå®³ã®æè¯ã®èšŒæ ã§ãã RESTãšMVCãäœã§ãããã ãã§ãªãã ã¹ããŒãã¬ã¹ãšã¹ããŒããã«ãäœã§ãããã«ã€ããŠããäžè¬çãªç解ã¯ãããŸããã ããã«èå³æ·±ãããšã«ãäžè¬ã«ãæ¡ä»¶ããäœã§ãããã«ã€ããŠã®äžè¬çãªç解ã¯ãããŸããã 誰ãããããããã»ã¹ã®ç¶æ ïŒãã®ã¡ã¢ãªïŒã誰ãããã¹ãŠã®ãµãŒããŒã®ç¶æ ïŒãã®ç¶æ ãå¥ã®ããã»ã¹ã«ååšããå¯èœæ§ãããïŒã誰ããã·ã¹ãã å šäœã®ç¶æ ïŒãããŠããŒã¿ããŒã¹ã«ä¿åããããã¡ã€ã«ãšããŒã¿ãç¶æ ãšèŠãªãããšãã§ããïŒãšããŠç解ããŠããŸãã ãã®ãããªç¶æ³ã§ã¯ãçãããããæ±ãå€ãçšèªãæšãŠãæ¹ãè¯ãã§ã ããã®æå³ã¯ãæ°ããçšèªã«åæããŠå°å ¥ãããšããåžæã¯ãŸã£ãããããŸãããããã§ã«å³å¯ãªèŸæžå®çŸ©ãäœæããŠããŸã ã ããã§ãªããã°ã話ãããšã¯ãŸã£ãããããŸããã1èªã ãã話ã察話ãæ§ç¯ããããšã¯äžå¯èœã§ããããŸã£ããç°ãªãæŠå¿µã«ã€ããŠã§ãã