Akka Scala服务器中的500内部服务器错误

时间:2017-05-31 05:44:32

标签: scala server akka akka-http

这是我使用Akka框架编写的服务器代码:

case class Sentence(data: String)
case class RawTriples(triples: List[String])

trait Protocols extends DefaultJsonProtocol {
    implicit val sentenceRequestFormat = jsonFormat1(Sentence)
    implicit val rawTriplesFormat = jsonFormat1(RawTriples)
}

trait Service extends Protocols {
    implicit val system: ActorSystem
    implicit def executor: ExecutionContextExecutor
    implicit val materializer: Materializer

    val openie = new OpenIE
    def config: Config
    val logger: LoggingAdapter

    lazy val ipApiConnectionFlow: Flow[HttpRequest, HttpResponse, Any] =
        Http().outgoingConnection(config.getString("services.ip-api.host"), config.getInt("services.ip-api.port"))

    def ipApiRequest(request: HttpRequest): Future[HttpResponse] = Source.single(request).via(ipApiConnectionFlow).runWith(Sink.head)

    val routes = {
        logRequestResult("akka-http-microservice") {
            pathPrefix("openie") {
                post { 
                    decodeRequest{
                        entity(as[Sentence]){ sentence =>
                            complete {
                                var rawTriples = openie.extract(sentence.data)
                                        val resp: MutableList[String] = MutableList()

                                for(rtrip <- rawTriples){
                                    resp += (rtrip.toString())
                                }
                                val response: List[String] = resp.toList

                                println(response)
                                response
                            }
                        }
                    }
                }
            }
        }
    }
}

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

服务器接受包含句子的POST请求,并返回一个json数组。它工作正常,但如果我使用并行化代码过于频繁地请求它,那么它会产生500内部服务器错误。我想知道是否有任何参数可以在服务器中设置以避免(用于接受请求的就绪线程数等)。

在日志文件中,错误记录为:

  

[错误] [05/31/2017 11:48:38.110]   [默认-akka.actor.default-调度-6]   [akka.actor.ActorSystemImpl(默认值)]处理期间出错   请求:&#39; null&#39;。完成500内部服务器错误响应。

2 个答案:

答案 0 :(得分:2)

bindAndHandle方法上的文档显示了您想要的内容:

/**
 * Convenience method which starts a new HTTP server at the given endpoint and uses the given `handler`
 * [[akka.stream.scaladsl.Flow]] for processing all incoming connections.
 *
 * The number of concurrently accepted connections can be configured by overriding
 * the `akka.http.server.max-connections` setting. Please see the documentation in the reference.conf for more
 * information about what kind of guarantees to expect.
 *
 * To configure additional settings for a server started using this method,
 * use the `akka.http.server` config section or pass in a [[akka.http.scaladsl.settings.ServerSettings]] explicitly.
 */

akka.http.server.max-connections可能就是你想要的。正如文档建议的那样,您还可以深入了解akka.http.server配置部分。

答案 1 :(得分:0)

在application.conf文件中添加以下内容

akka.http {
  server {
    server-header = akka-http/${akka.http.version}
    idle-timeout = infinite
    request-timeout = infinite
  }
}