Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Microservices in Scala: Play Framework

7,736 views

Published on

Presentation from Warsjawa 2014 workshop "Microservices in Scala". Topic covered: building websocket microservice using Play & Akka.

Published in: Software

Microservices in Scala: Play Framework

  1. 1. Microservices in Play & Akka Łukasz Sowa @luksow Jacek Głodek @jacekglodek
  2. 2. Play Framework ● General purpose ● (Aims to be) fully-blown web framework ● Part of Typesafe Reactive Platform ● For Java & Scala ● Popular (LinkedIn, Guardian, Klout, …) ● Not very well-suited for microservices... ● but still popular :)
  3. 3. Play Framework ● Rails-like (M)VC ● Routes => Controllers => Views
  4. 4. Play Framework
  5. 5. Routes (conf/routes) ● Type safe! ● VERB [path with params] [controller action] – GET / controllers.OpenGraphFetcher.index – GET /clients/:id controllers.ClientsController.show(id: Long) – PUT /orders/:clientid controllers.OrdersController.create(clientId: Int) ● Body params (POST, PUT etc.) have to be handled by Play forms or manually (ugly!)
  6. 6. Controllers (app/controllers/*) ● Composable Actions – Action: Request[A] => Result – DBAction: Session => Result – AuthAction: RequestWithUser[A] => Result – … ● Can be async
  7. 7. Views (app/views/*) ● Type safe templating language (twirl) ● Meh (aka leave it to frontenders)
  8. 8. Coding! 1) Clone & run sample project 2) Add action that takes two params (login: String, authToken: String), wire it to routes, test 3) Try authenticate your request with Redis
  9. 9. OpenGraph Info Fetcher ● Given list of URLs return OG info ● Synchronous? (request - response) ● Waiting for all OGs to fetch ● How to handle failure? ● Or maybe polling? ● No, websockets! ● Answers ASAP ● Failure? Just screw it
  10. 10. WebSockets ● Async of the web! ● Almost like good ol' TCP (BSD) sockets Play-flavoured websockets: ● Iteratees (FP black magic, good for streams) ● Actors (❤, good for messages)
  11. 11. Akka ● Toolkit for distributed & concurrent systems ● Based on concept of actor and messaging Actor: ● Encapsulates mutable state ● Reacts to messages ● Sends messages to other actors it knows: ● By address/name ● Because they send him message ● Because they were created by it ● ...
  12. 12. Actors in Akka ● Live in systems ● Can be distributed in space & time ● Process their messages sequentially ● No guarantees of message delivery/processing ● Can form tree-like hierarchies => supervision
  13. 13. Coding! 1) Write echo websocket using Akka Actors 2) Test it in web browser 3) Write not-so-stringly-typed websocket 4) Test it in web browser 5) Write proper types for task 6) Implement OG fetching 7) Test it in web browser 8) (For brave & ambitious) Implement caching

×