简体   繁体   English

Akka HTTP:如何与参与者分割路线

[英]Akka HTTP: How to split routes with actors

I have an old Scala/Akka Http project that I'm trying to simplify and refactor. 我有一个旧的Scala / Akka Http项目,我正在尝试简化和重构。 I was wondering if there's a better way to organize routes and perhaps split them across actors. 我想知道是否有更好的方法来组织路线,并可能将其分配给演员。 Here's what I have at the moment (far from ideal): 这是我目前所拥有的(远非理想):

``` ```

object MyAPI {
  def props(): Props = Props(new MyAPI())
  val routes = pathPrefix("api") {
    pathPrefix("1") {
      SomeActor.route  //More routes can be appended here using ~

    }
  }
}

final class MyAPI extends Actor with ActorLogging {

  implicit lazy val materializer = ActorMaterializer()
  implicit lazy val executionContext = context.dispatcher

  Http(context.system)
    .bindAndHandleAsync(Route.asyncHandler(MyAPI.routes), MyHttpServer.httpServerHostName, MyHttpServer.httpServerPort)
    .pipeTo(self)

  override def receive: Receive = {
    case serverBinding: ServerBinding =>
      log.info(s"Server started on  ${serverBinding.localAddress}")
      context.become(Actor.emptyBehavior)
    case Status.Failure(t) =>
      log.error(t, "Error binding to network interface")
      context.stop(self)
  }
}

``` ```

``` ```

object SomeActor {

  def props(): Props = Props[SomeActor]
  val route = get {
    pathPrefix("actor") {
      pathEnd {
        complete("Completed") //Is there a clean way 'ask' the actor below?
      }
    }
  }

}

class SomeActor extends Actor with ActorLogging {
  implicit lazy val executionContext = context.dispatcher;



  override def receive: Receive = {
    //receive and process messages here

  }

``` ```

So, my question is - is there a clean way to structure and refactor routes instead of lumping them together in one large route definition? 因此,我的问题是-是否有一种干净的方法来构造和重构路由,而不是将它们集中在一个大型路由定义中? I could perhaps create a hierarchy of actors (routers) and the main route definition just delegates it to the routers and we incrementally add more details as we go deeper in the actor hierarchy. 我也许可以创建角色(路由器)的层次结构,而主路由定义只是将其委派给路由器,并且随着角色层次结构的深入,我们会逐步添加更多详细信息。 But is there a generally accepted patter or two to organize routes? 但是,是否有公认的一两个模式来组织路线?

I would like to suggest you on the basis of the functionality you can have as many actors you you want, but create one supervisor actors which will keep watch on each child actor. 我想根据您可以拥有的演员数量来建议您,但是要创建一个监督演员,以监视每个子演员。 And all the supervision strategies should be written into the supervisor itself, and all the msg you gonna sent to the actors should be forwarded by the supervisor. 并且所有监督策略都应写入监督者本身,而您要发送给演员的所有消息都应由监督者转发。

As soon as you get the data from the end point, may be get or post method take the data into someRequest case class. 一旦从端点获取数据,则可能是get或post方法将数据带入someRequest案例类。 Then sent it to some handleReq() method. 然后将其发送到一些handleReq()方法。 And then do your processing make traits on functionality basis. 然后,您的处理将使功能成为基础。

You can structure project something like this. 您可以构建类似这样的项目。 src/ actor //all the actors will be in this package src / actor //all the actors will be in this package

model // all the case classes and constants 模型// all the case classes and constants

repo // all the db related function you can do here 回购// all the db related function you can do here

service // all your routes and endPoint functions 服务// all your routes and endPoint functions

Now you can have package util where all the utilities trait you can put which will be used by any of the actor, or service may be you have lots of validations you can have a package named validator. 现在,您可以拥有util包,其中可以放置所有实用程序特征,任何actor都可以使用它,或者服务可能是您具有大量验证,您可以拥有一个名为validator的程序包。

The structure is depends on your business. 结构取决于您的业务。 I think it would help. 我认为这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM