GitHubãªãã¸ããªã¯ãã¡ãã
ãªãä½ã£ãã®ãï¼
Scalaæ¥çã®ãã¬ã¼ã ã¯ã¼ã¯ã¯é¢æ°åçãªã¢ããã¼ãã®ãã®ã主æµã«ãªã£ã¦ãã¾ãããããã®ãã¬ã¼ã ã¯ã¼ã¯ã¯ãã³ããããã³ã°I/Oã並åå¦çããã¾ãæ±ããã¨ãã§ãã¾ãããæ¬è³ªçãªè¤éããããããã¾ããèªåã®çµé¨ã§ã¯ãç¹ã«ä¼æ¥ã·ã¹ãã ãªã©ã®å ´åã¯å¾æ¥ã®åæãµã¼ãã¬ããã§ãååã¨ããã±ã¼ã¹ãå¤ãã®ã§ã¯ãªããã¨æãã¦ãã¾ãã
ã¾ããæ¢åã®Scalaç¨ã®ãã¬ã¼ã ã¯ã¼ã¯ã¯ã¿ã¤ãã»ã¼ããªDSLã§ã«ã¼ãã£ã³ã°ãå ¥åºåãã©ã¡ã¼ã¿ãªã©ãå®ç¾©ãããã®ãå¤ãã®ã§ããããã®æã®ãã®ã¯ã·ã³ãã«ãªéç解æã§ã¡ã¿æ å ±ãæ½åºãããã¨ãé£ããããSwaggerãªã©ã®å¤é¨ãã¼ã«ã¨ã®ç¸æ§ãæªãã¨ããæ¬ ç¹ãããã¾ãã
Restyã¯ãã®ãããªã±ã¼ã¹ã«ããã¦ãããã°ã©ãã³ã°è¨èªã¨ãã¦ã®Scalaã®ã¢ããã³ãã¼ã¸ãæ´»ãããã¨ã®ã§ããã·ã³ãã«ãªãã¬ã¼ã ã¯ã¼ã¯ãããã¨ããã®ã§ã¯ã¨ãããã¨ã§ä½ã£ã¦ã¿ããã®ã§ãã
使ã£ã¦ã¿ãã
以ä¸ã®ã³ãã³ããå®è¡ãããã¨ã§ãµã³ãã«ããã¸ã§ã¯ããå®è¡ãããã¨ãã§ãã¾ãã
$ git clone https://github.com/takezoe/resty-sample.git $ cd resty-sample/ $ sbt ~jetty:start
Swagger UIãããã©ã«ãã§çµã¿è¾¼ã¾ãã¦ããã®ã§http://localhost:8080/swagger-ui/
ã«ã¢ã¯ã»ã¹ãããã¨ã§ãµã³ãã«ããã¸ã§ã¯ãã§å®è£
ããã¦ããAPIã®åä½ã確èªãããã¨ãã§ãã¾ãã
ããã§ã¯å®éã®ã½ã¼ã¹ã³ã¼ããç°¡åã«è¦ã¦ããã¾ãããã以ä¸ã¯æãã·ã³ãã«ãªã³ã³ããã¼ã©ã®å®è£ ä¾ã§ãã
class HelloController { @Action(method = "GET", path = "/hello/{name}") def hello(name: String): Message = { Message(s"Hello ${name}!") } } case class Message(message: String)
APIã®ã¨ã³ããã¤ã³ãã«ãªãã¡ã½ããã«ã¯@Action
ã¨ããã¢ããã¼ã·ã§ã³ãä»ä¸ããå¿
è¦ãããã¾ããã¡ã½ããã®å¼æ°ã¯ãã¹ãã¯ã¨ãªæååããªãã¸ã§ã¯ãã®å ´åã¯ãªã¯ã¨ã¹ãããã£ã¨ãã¦éä¿¡ãããJSONããRestyã«ãã£ã¦èªåçã«æ¸¡ããã¾ããã¾ããæ»ãå¤ãåã«å¿ãã¦èªåçã«ããã¹ããJSONã¨ãã¦è¿å´ããã¾ãã
ã³ã³ããã¼ã©ãRestyã«ç»é²ããããã«ä»¥ä¸ã®ãããªãªã¹ããä½æãã¦ããå¿ è¦ãããã¾ãã
@WebListener class InitializeListener extends ServletContextListener { override def contextDestroyed(e: ServletContextEvent): Unit = { } override def contextInitialized(e: ServletContextEvent): Unit = { Resty.register(new HelloController()) } }
åºæ¬çã«ã¯ããã ãã§Web APIãå®è£ ãããã¨ãã§ãã¾ãã
Swaggerã¨Hystrix
Restyã§ã¯ä½ãããªãã¦ãSwaggerãHystrixã¨ã®é£æºãå¯è½ã§ãã追å ã§è¨å®ãè¡ãªã£ãããã¡ã¿ãã¼ã¿ãå®ç¾©ãããããå¿ è¦ã¯ããã¾ããã
ã¾ãSwaggerã§ãããSwaggerã®JSONã¯ã³ã³ããã¼ã©ããèªåçã«çæãããhttp://localhost:8080/swagger.json
ã§å
¬éããã¾ããã¾ããåè¿°ã®ããã«Swagger UIã¯http://localhost:8080/swagger-ui/
ã§å©ç¨å¯è½ã§ãã
Hystrixã®ã¡ããªã¯ã¹ã¯ã¢ã¯ã·ã§ã³ãã¨ã«éä¿¡ããã¦ãã¾ããã¹ããªã¼ã ã®ã¨ã³ããã¤ã³ãã¯http://localhost:8080/hystrix.stream
ã§å©ç¨å¯è½ãªã®ã§Hystrixã®ããã·ã¥ãã¼ãã«ãã®URLã追å ããã°ããã«ã¢ãã¿ãªã³ã°ãéå§ãããã¨ãã§ãã¾ãã
ã¾ã¨ã
Restyã¯Scalaã§JSONãã¼ã¹ã®Web APIãä½æããããã®æçã®æ¹æ³ãæä¾ãã¾ãããµã¼ãã¬ããä¸ã§åä½ããé¢æ°åã¢ããã¼ããæ¡ã£ã¦ãã¾ãããæ¨ä»ã®Scalaæ¥çã®ãã¬ã³ãã«åãã¦ã¯ãã¾ãããã¨ã¦ãç°¡åã§ã·ã³ãã«ã§å®å®ãã¦ããå®å ¨ã§ããã¾ããå ¨èªåã®Swaggerã¨Hystrixé£æºãæ¨æºã§çµã¿è¾¼ã¾ãã¦ãã¾ãã
æ¯éGitHubãªãã¸ããªã«ã¢ã¯ã»ã¹ãã¦è©¦ãã¦ã¿ã¦ããã ããã°ã¨æãã¾ãã