简体   繁体   English

将Dispatcher与Spray HttpService一起使用

[英]Use a Dispatcher with Spray HttpService

My application has an API using SprayCan. 我的应用程序具有使用SprayCan的API。 In the application, any blocking code has a separate dispatcher for each specific resource. 在应用程序中,任何阻塞代码都为每个特定资源都有一个单独的调度程序。

Is it necessary to protect the API service from being blocked by the application by configuring it with it's own Dispatcher too? 是否也需要通过使用自己的Dispatcher配置API服务来防止API服务被应用程序阻止?

Also is it common practice to use a Router for an API service to handle a larger capacity of requests? 将路由器用于API服务也是一种常见的做法,可以处理更大容量的请求?

 class MyService extends Actor with HttpService {...}

 val service = system.actorOf(MyService.props(...).withDispatcher(???))

Is it necessary to protect the API service from being blocked by the application by configuring it with it's own Dispatcher too? 是否也需要通过使用自己的Dispatcher配置API服务来防止API服务被应用程序阻止?

Usually not necessary. 通常没有必要。 Check reference.conf that comes as default config with Spray to see if that dispatcher will satisfy your needs. 使用Spray检查默认配置中提供的reference.conf ,以查看该调度程序是否可以满足您的需求。 If not, provide a custom one. 如果没有,请提供自定义的。

Also is it common practice to use a Router for an API service to handle a larger capacity of requests? 将路由器用于API服务也是一种常见的做法,可以处理更大容量的请求?

Usually requests are handed off to another Actor to unblock the route or ran as Future (possibly on a separate thread pool). 通常,将请求移交给另一个Actor来解除阻塞路由或以Future身份运行(可能在单独的线程池上)。 See all available options here: How does spray.routing.HttpService dispatch requests? 在这里查看所有可用选项: spray.routing.HttpService如何调度请求? .

Finally, you should not block in the route handler because it will block your service. 最后,您不应阻塞路由处理程序,因为它会阻塞您的服务。 From your description it sounds like your blocking code runs in a Future or similar. 根据您的描述,听起来您的阻止代码在Future或类似版本中运行。 As long as it does not make route handler wait for result/block it's fine. 只要它不使路由处理程序等待结果/阻塞就可以了。

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

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