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

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

问题描述

快速背景

我正在通过一些示例学习 Akka HTTP 堆栈以创建新的 REST 项目(完全非 UI).我一直在使用和扩充 Akka HTTP 微服务示例 来处理一堆用例和配置,并且对 Scala 和Akka HTTP 工作.

当前设置

目前我有这样的配置:

object AkkaHttpMicroservice 使用 Service { 扩展应用程序覆盖隐式 val 系统 = ActorSystem()覆盖隐式 val executor = system.dispatcher覆盖隐式 val materializer = ActorMaterializer()覆盖 val config = ConfigFactory.load()覆盖 val logger = Logging(system, getClass)Http().bindAndHandle(routes, config.getString("http.interface"), config.getInt("http.port"))}

routes 参数只是一个简单的值,其中包含使用 pathpathPrefix 等的典型数据.

问题

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

我真的希望能够定义一组类来分离关注点并处理 Actor 设置和处理以处理应用程序的不同区域,并将封送处理留给根 App 扩展名.

这可能是我在 Java 中如何在我的类上使用诸如 @javax.ws.rs.Path("/whatever") 之类的注释做事的想法太多了.如果是这种情况,请随时指出心态的变化.

我尝试搜索几组不同的关键字,但我认为我问错了问题(例如,1, 2).

解决方案

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

您可以很容易地组合来自多个文件的路由.

FooRouter.scala

object FooRouter {val 路线 = 路径(foo"){完全的 {好的 ->富"}}}

BarRouter.scala

object BarRouter {val 路线 = 路径(酒吧"){完全的 {好的 ->酒吧"}}}

MainRouter.scala

导入FooRouter导入 BarRouter导入 akka.http.scaladsl.server.Directives._进口 ...对象主路由器{val 路线 = FooRouter.route ~ BarRouter.route}对象 AkkaHttpMicroservice 使用 Service { 扩展应用程序...Http().bindAndHandle(MainRouter.routes, config.getString("http.interface"), config.getInt("http.port"))}

这里有一些文档:

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

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

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

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

Quick Background

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.

Current Setup

Currently I have a configuration like this:

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"))
}

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

The Problem

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

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.

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.

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

解决方案

Problem 1 - combine routes in multiple files

You can combine routes from multiple files quite easy.

FooRouter.scala

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

BarRouter.scala

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

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"))
}

Here you have have some docs :

Problem 2 - seprate routing, marshalling, etc

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

Problem 3 - handle routes using annotations

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天全站免登陆