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: Spray

1,704 views

Published on

Presentation from Warsjawa 2014 workshop "Microservices in Scala". Topic covered: building simple REST microservice using Spray's routing DSL, MongoDB & Redis.

Published in: Software
  • Be the first to comment

Microservices in Scala: Spray

  1. 1. Microservices in Spray Łukasz Sowa @luksow Jacek Głodek @jacekglodek
  2. 2. Spray ● Client- & server-side REST/HTTP libs ● Multiple layers – low-level to DSL ● Really fast (check out benchmarks!) ● „Acquired” by Typesafe ● Spray => akka-http – Reactive streams – New building block for Play ● Well-suited for REST microservices
  3. 3. Spray ● HttpRequest => HttpResponse ● Different levels of abstraction, focus: DSL ● Key concepts – Routing directives – Marshalling
  4. 4. Spray - routing ● Directives – Transform, extract, filter, side-effecting – Composable – Concatanable
  5. 5. Spray - routing ● Useful directives – path, pathPrefix – get, post, put, delete, patch – parameters – formFields – entity – logRequestResponse – complete (!)
  6. 6. Coding! ● Try out at least following combinations: 1) pathPrefix + path (with extracting) 2) get + parameters 3) post + formFields
  7. 7. Spray - marshalling ● Return anything that is marshalable ● String (OK(value)) ● HttpEntity ● Option[T] (OK(T) vs. NotFound(T)) ● Either[A, B] (OK(A) vs. OK(B)) ● Future[T] (OK(T) – success vs. InternalServerError) ● Loads of predefined stuff, ex. JSON ● You can write your own!
  8. 8. Coding! 1) Try Option & Future marshallers 2) Use JSON marshaller to return JSON 3) Combine 1) & 2) 4) Use entity directive 5) Add validation 6) Time to create real microservices, yay!
  9. 9. Registration microservice ● POST /auth/account ● JSON { login: l, password: p, passwordConfirmation: pC } ● OK + { login: l, authToken: aT } ● BadRequest ● POST /auth/session ● Form login: l, password: p ● OK + { login: l, authToken: aT } ● Forbidden ● GET /auth/account (H: AuthToken: aT) ● OK + { login: l, authToken: aT, … } ● Forbidden
  10. 10. Links microservice ● Authenticate all the things! (Forbidden) ● POST /links/ ● JSON { url: u, description: d } ● Save: ^ + date + author ● GET /links/:id ● JSON { url: u, description: d, date: dt, author: a } ● GET /links?limit=N&offset=M ● JSON { links: [url] }

×