You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala 3环境下基于ZIO 2.x的zio-http应用启动后立即停止的原因排查

Why does my Scala 3 + ZIO + zhttp app stop immediately after starting?

Hey there, let's break down why your app is shutting down right away and how to fix it.

The Root Cause

The main issue here is a version mismatch between ZIO and zhttp:

  • You're using ZIO 2.0.0-RC5 but zhttp 2.0.0-RC6
  • zhttp's 2.x releases are tightly coupled to specific ZIO 2.x versions. Different release candidates (RCs) have subtle API changes that aren't backward compatible, so zhttp can't properly hook into the ZIO runtime, causing the server to start and immediately terminate.

Your Scala 2 example works because it's using a matching ZIO/zhttp version combo. The switch to ZIOAppDefault (Scala 3's recommended entry point) isn't the problem here—version alignment is.

The Fix

All you need to do is sync your ZIO version with zhttp's. Update your build.sbt to use ZIO 2.0.0-RC6 (matching your zhttp version):

val zioVersion = "2.0.0-RC6" // Changed from 2.0.0-RC5
val zioHttpVersion = "2.0.0-RC6"

libraryDependencies += "dev.zio" %% "zio" % zioVersion
libraryDependencies ++= Seq(
  "dev.zio" %% "zio-test" % zioVersion % "test",
  "dev.zio" %% "zio-test-sbt" % zioVersion % "test",
  "dev.zio" %% "zio-test-magnolia" % zioVersion % "test" // optional
)
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")

libraryDependencies += "io.d11" %% "zhttp" % zioHttpVersion
libraryDependencies += "io.d11" %% "zhttp-test" % zioHttpVersion % Test

Rebuild and run your app, and the server should stay up and running as expected.

Bonus: Verify with Logging

To confirm the server is starting properly, you can add a log message to your code. This helps you see exactly when the server is ready:

import zhttp.http.*
import zhttp.http.Method.GET
import zhttp.service.Server
import zio.*
import zio.logging.log

object HexAppApplication extends ZIOAppDefault {
  val app: HttpApp[Any, Nothing] = Http.collect[Request] {
    case GET -> !! / "text" => Response.text("Hello World!")
    case GET -> !! / "json" => Response.json("""{"greetings": "Hello World!"}""")
  }

  val program: URIO[Any, ExitCode] = 
    Server.start(8090, app)
      .tap(_ => log.info("Server successfully started on port 8090 🚀"))
      .exitCode

  override def run: URIO[Any, ExitCode] = program
}

内容的提问来源于stack exchange,提问作者codependent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 18:22:32