项目作者: mtumilowicz

项目描述 :
Introduction to akka in scala: http & actors.
高级语言: Scala
项目地址: git://github.com/mtumilowicz/scala-akka-http-actor-workshop.git
创建时间: 2020-10-17T20:25:42Z
项目社区:https://github.com/mtumilowicz/scala-akka-http-actor-workshop

开源协议:GNU General Public License v3.0

下载


Build Status
License: GPL v3

scala-akka-http-workshop

preface

  • goals of this workshop
    • introduction to akka http
      • routing and marshalling
  • workshop plan:
    • playground with roles: developer, product owner
    • develop microservice with features
      • user could buy a venue if he can afford it
      • userA could buy a venue from userB if he can afford it

akka http

  • implements a full server/client-side HTTP stack on top of akka-actor and akka-stream
  • provides a DSL to describe HTTP “routes” and how they should be handled
    • Route is the central concept of Akka HTTP’s Routing DSL
      1. type Route = RequestContext => Future[RouteResult]
      • when a route receives a request (RequestContext) it can do one of these things
        1. requestContext.complete(…) - given response is sent to the client as reaction to the request
        2. requestContext.reject(…) - route does not want to handle the request
        3. requestContext.fail(…) -
      • RequestContext
        • wraps an HttpRequest instance to enrich it with additional information that are typically required
          by the routing logic (ex. ExecutionContext)
      • RouteResult
        • simple abstract data type (ADT) that models the possible non-error results of a Route
        • Complete, Rejected
    • Directive
      • a small building block used for creating route structures
        1. val route: Route = { ctx => ctx.complete("yeah") } // standard way to build route
        2. val route: Route = _.complete("yeah") // scala syntax
        3. val route = complete("yeah") // complete directive
      • example
        1. val route: Route =
        2. pathPrefix("venues") {
        3. concat(
        4. pathEnd {
        5. get {
        6. complete(handler.action)
        7. }
        8. },
        9. ...
        10. )
        11. }
    • PathMatcher
      • mini-DSL is used to match incoming URL’s and extract values from them
      • used in the path directive
      • example
        1. path("foo" / "bar") // matches /foo/bar
  • marshalling and unmarshalling
    • marshalling - converting a higher-level (object) into lower-level representation, ex. a “wire format”
      • also called “serialization” or “pickling”
    • “unmarshalling” - reverse process to marshalling
      • also called “deserialization” or “unpickling”
    • is done separately from the route declarations
  • timeouts
    • idle-timeout - if a connection is open but no request/response is being written to it for over idle-timeout time,
      the connection will be automatically closed.
    • request timeouts - limits the maximum time it may take to produce an HttpResponse from a route
      • if that deadline is not met the server will automatically inject a Service Unavailable HTTP response and
        close the connection to prevent it from leaking and staying around indefinitely