快速开始

最小可运行示例

 1package main
 2
 3import (
 4    "net/http"
 5
 6    "github.com/go-zeus/zeus/app"
 7)
 8
 9func main() {
10    app.Run(&app.Config{Port: 8080}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
11        w.Write([]byte("hello from zeus"))
12    }))
13}
14
15// curl http://localhost:8080

默认装配(零配置自动启用)

未配置时自动启用,用户零感知:

默认项内置实现
Server 协议按 handler 类型推断(http.Handler → HTTP)
注册中心registry/memory(L1)
日志log/slog 输出到 stdout
中间件recovery + requestID + 请求日志
健康检查/health /health/ready /health/live
Metrics非默认装配:L1 app.Run 不注入 meter、不注册 /metrics;需经 L3 WithMeter + metrics 中间件显式启用
信号处理SIGTERM/SIGINT/SIGQUIT → 优雅关闭(10s 超时)
服务名zeus-service(可覆盖)

关键规则:默认装配不允许失败。任何"必须配置才能跑"的字段都是设计缺陷。

添加业务路由

L1 入口接受任意 http.Handler,可以传入 http.ServeMux

1func main() {
2    mux := http.NewServeMux()
3    mux.HandleFunc("/api/users", handleUsers)
4    mux.HandleFunc("/api/orders", handleOrders)
5
6    app.Run(&app.Config{Port: 8080, Name: "my-api"}, mux)
7}

启用 Registry(L2 升级)

只需改 Config 中的 Registry URL 即可切换到分布式注册中心:

1import _ "github.com/go-zeus/zeus/plugins/registry/etcd"  // 注册 etcd:// scheme
2
3app.Run(&app.Config{
4    Name:     "my-service",
5    Port:     8080,
6    Cluster:  "canary",                    // 灰度集群
7    Registry: "etcd://localhost:2379",
8}, handler)

关闭与信号

应用自动监听以下信号:

  • SIGTERM / SIGINT / SIGQUIT → 触发优雅关闭
  • 默认 10s 关闭超时(可通过 app.WithStopTimeout 调整)

下一步