Netty 和 Ktor 投掷 jetty.io.EofException

Netty and Ktor throwing jetty.io.EofException

我正在使用 Ktor 和 Jetty 创建一个应用程序。有一条规则,即在执行某项操作之前,我需要检查端点是否启动/关闭。 为此,我创建了一个根据服务进行检查的功能。

suspend fun checkStatus(
  target: Target,
  login: String,
  passwordLogin: String,
  url: String
) {
  when (target) {
    Target.Elasticsearch -> {
      val client = HttpClient(Jetty) {
        install(Auth) {
          basic {
            username = login
            password = passwordLogin
          }
        }
      }
      runCatching {
        client.get<String>(url)
      }.onFailure {
        it.printStackTrace()
        throw it
      }
    }
  }
}

为了减小函数的大小,我刚刚使用了带有 elasticsearch 的示例。所以我有一个功能可以检查 elasticsearch 是否启动/关闭

suspend fun checkElasticStatus(
  username: String,
  password: String,
  https: Boolean,
  host: String,
  port: String
) = checkStatus(
  target = Target.Elasticsearch,
  login = username,
  passwordLogin = password,
  url = if (https) "https://$host:$port" else "http://$host:$port"
)

所以我在控制器中使用这个函数,然后再继续某些逻辑。

fun Route.orchestration() {
  route("/test") {
    post {
      runCatching {
        checkElasticStatus(
          environmentVariable(ev, "elk.username"),
          environmentVariable(ev, "elk.password"),
          environmentVariable(ev, "elk.https").toBoolean(),
          environmentVariable(ev, "elk.host"),
          environmentVariable(ev, "elk.port")
        )

        /** other codes **/
       }
     }
   }
 }

但我总是收到错误消息:

org.eclipse.jetty.io.EofException at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:283) at org.eclipse.jetty.io.WriteFlusher.flush(WriteFlusher.java:422) at org.eclipse.jetty.io.WriteFlusher.write(WriteFlusher.java:277) at org.eclipse.jetty.io.AbstractEndPoint.write(AbstractEndPoint.java:381) at org.eclipse.jetty.http2.HTTP2Flusher.process(HTTP2Flusher.java:259) at org.eclipse.jetty.util.IteratingCallback.processing(IteratingCallback.java:241) at org.eclipse.jetty.util.IteratingCallback.iterate(IteratingCallback.java:223) at org.eclipse.jetty.http2.HTTP2Session.newStream(HTTP2Session.java:543) at io.ktor.client.engine.jetty.JettyHttpRequestKt$executeRequest$jettyRequest.invoke(JettyHttpRequest.kt:40) at io.ktor.client.engine.jetty.JettyHttpRequestKt$executeRequest$jettyRequest.invoke(JettyHttpRequest.kt) at io.ktor.client.engine.jetty.UtilsKt.withPromise(utils.kt:14) at io.ktor.client.engine.jetty.JettyHttpRequestKt.executeRequest(JettyHttpRequest.kt:39) at io.ktor.client.engine.jetty.JettyHttpRequestKt$executeRequest.invokeSuspend(JettyHttpRequest.kt) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)

Caused by: java.nio.channels.AsynchronousCloseException at java.base/sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:501) at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:263) ... 18 more

org.eclipse.jetty.io.EofException at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:283) at org.eclipse.jetty.io.WriteFlusher.flush(WriteFlusher.java:422) at org.eclipse.jetty.io.WriteFlusher.write(WriteFlusher.java:277) at org.eclipse.jetty.io.AbstractEndPoint.write(AbstractEndPoint.java:381) at org.eclipse.jetty.http2.HTTP2Flusher.process(HTTP2Flusher.java:259) at org.eclipse.jetty.util.IteratingCallback.processing(IteratingCallback.java:241) at org.eclipse.jetty.util.IteratingCallback.iterate(IteratingCallback.java:223) at org.eclipse.jetty.http2.HTTP2Session.newStream(HTTP2Session.java:543) at io.ktor.client.engine.jetty.JettyHttpRequestKt$executeRequest$jettyRequest.invoke(JettyHttpRequest.kt:40) at io.ktor.client.engine.jetty.JettyHttpRequestKt$executeRequest$jettyRequest.invoke(JettyHttpRequest.kt) at io.ktor.client.engine.jetty.UtilsKt.withPromise(utils.kt:14) at io.ktor.client.engine.jetty.JettyHttpRequestKt.executeRequest(JettyHttpRequest.kt:39) at io.ktor.client.engine.jetty.JettyHttpRequestKt$executeRequest.invokeSuspend(JettyHttpRequest.kt) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)

Caused by: java.nio.channels.AsynchronousCloseException at java.base/sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:501) at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:263) ... 18 more

有人能帮帮我吗?

Jetty 引擎仅支持 HTTP/2 协议。

org.eclipse.jetty.io.EofException 当我向只能用 HTTP/1.1 响应的资源发出请求时抛出,例如http://www.google.com.

但是,该错误具有误导性,错误追踪器中有一个关于它的 issue