具有多个路由配置的akka​​-http [英] akka-http with multiple route configurations

查看:92
本文介绍了具有多个路由配置的akka​​-http的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一些示例,以学习Akka HTTP堆栈来创建新的REST项目(完全非UI)。我一直在使用和扩充 Akka HTTP微服务示例来处理大量用例,并配置,并且对Scala& amp;的效果感到惊讶。 Akka HTTP工作。

I am running through some examples learning the Akka HTTP stack for creating a new REST project (completely non-UI). I have been using and augmenting the Akka HTTP Microservice Example to work through a bunch of use cases and configurations and have been pleasantly surprised by how well Scala & Akka HTTP work.

当前我有这样的配置:

object AkkaHttpMicroservice extends App with Service {
  override implicit val system = ActorSystem()
  override implicit val executor = system.dispatcher
  override implicit val materializer = ActorMaterializer()

  override val config = ConfigFactory.load()
  override val logger = Logging(system, getClass)

  Http().bindAndHandle(routes, config.getString("http.interface"), config.getInt("http.port"))
}

routes 参数只是一个简单值,其中使用 path包含典型数据 pathPrefix 等。

The routes parameter is just a simple value that has the typical data within it using path, pathPrefix, etc.

有没有办法在多个Scala文件中设置路由,或者在其中的某个示例中设置路由?

Is there any way to set up routing in multiple Scala files or an example somewhere out there?

我真的很想能够定义一组类分开顾虑并处理Actor的设置和处理,以处理应用程序的不同区域,而将封送处理留给根 App 扩展名。

I would really like to be able to define a set of classes that separate the concerns and deal with Actor setup and processing to deal with different areas of the application and just leave the marshaling to the root App extension.

这可能是我考虑如何使用 @ javax.ws.rs.Path( / whatever)在我的课程上。在这种情况下,请随时指出思维方式的变化。

This might be me thinking too much in terms of how I did things in Java using annotations like @javax.ws.rs.Path("/whatever") on my classes. If that is the case, please feel free to point out the change in mindset.

我尝试搜索一些不同的关键字集,但相信我在问错问题(例如, 1 2 )。

I tried searching for a few different set of keywords but believe I am asking the wrong question (eg, 1, 2).

推荐答案

问题1-将路由合并到多个文件中

您可以很容易地将多个文件中的路由合并。

You can combine routes from multiple files quite easy.

FooRouter.scala

FooRouter.scala

object FooRouter {
   val route = path("foo") {
       complete {
          Ok -> "foo"
       } 
   }       
}

BarRouter.scala

BarRouter.scala

object BarRouter {
   val route = path("bar") {
       complete {
          Ok -> "bar"
       } 
   }       
}

MainRouter.scala

MainRouter.scala

import FooRouter
import BarRouter
import akka.http.scaladsl.server.Directives._
import ...

object MainRouter {
   val routes = FooRouter.route ~ BarRouter.route
}

object AkkaHttpMicroservice extends App with Service {
  ...    
  Http().bindAndHandle(MainRouter.routes, config.getString("http.interface"), config.getInt("http.port"))
}

这里有一些文档:

  • http://doc.akka.io/docs/akka-http/current/scala/http/routing-dsl/overview.html
  • http://doc.akka.io/docs/akka-http/current/scala/http/routing-dsl/routes.html

问题2-单独的路由,编组等

是的,您可以分隔路由,编组和应用程序逻辑。这里有激活器示例: https://github.com/theiterators/reactive-microservices

Yes, you can separate routing, marshalling and application logic. Here you have activator example: https://github.com/theiterators/reactive-microservices

问题3-使用注释处理路由

我不知道允许您使用注释在akka-http中定义路由。尝试了解有关DSL路由的更多信息。这代表了HTTP路由的另一种方法,但它也是一种便捷的工具。

I don't know any lib that allow you to use annotion to define routing in akka-http. Try to learn more about DSL routing. This represents a different approach to http routing but it is convenient tool too.

这篇关于具有多个路由配置的akka​​-http的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆