【问题标题】:500 Internal Server Error in Akka Scala serverAkka Scala 服务器中的 500 内部服务器错误
【发布时间】:2017-05-31 05:44:31
【问题描述】:

这是我使用 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] [default-akka.actor.default-dispatcher-6] [akka.actor.ActorSystemImpl(default)] 处理过程中的错误 请求:“空”。完成 500 内部服务器错误响应。

【问题讨论】:

  • 查看服务器日志文件以获取更多信息
  • 您是否获得了更完整的堆栈跟踪以在框架代码中进行调试?

标签: scala server akka akka-http


【解决方案1】:

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 配置部分。

【讨论】:

  • 我必须对application.conf 文件进行一些更改吗?
  • 你能检查更新的问题吗?我添加了日志输出
  • 是的,您可以在application.conf 中进行配置。根据您的更新,似乎是发送请求的代码有问题......至少服务器声称它正在接收null 请求或包含null 的请求。
  • 但是在请求代码中我确保它不会发送空请求。
【解决方案2】:

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

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

【讨论】:

  • @Nilesh 更改超时有什么帮助?
猜你喜欢
  • 2020-03-12
  • 2017-04-12
  • 2019-01-17
  • 2011-10-17
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多