Help us shape the future of Go! Take the 2026 Go Developer Survey

hertz

module
v0.3.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT

README

hertz

Hertz 运行时集成——日志桥接、生命周期、SSE/health/metrics 子包、注册发现与 NNS 适配器、OTel 桥接。

项目主页:https://oekit.github.io/ · 名称由来:Open Easy Kit(开源 · 易用 · 工具集合) 🤖 AI Agent:本仓规范与硬规则见 AGENTS.md;机器可读索引见 llms.txt(接口签名以源码为准)。

本仓库是 ekit(git.enjoye.top/enjoydream/ekit v0.34.0) 拆分出的独立 Go module,属于 oekit(Open Easy Kit)模块矩阵。

安装

go get github.com/oekit/hertz@v0.3.5

能做什么

  • hlog 桥接:框架日志 → logx,caller 指向真实调用方
  • RunServer:接 bootstrap 生命周期、优雅关闭、netpoll 监听失败自恢复
  • Shutdown Hook:关停前刷数据(接受 error,不 panic)
  • SSE:官方 protocol/sse 封装(Send/SendRaw/KeepAlive)
  • health/metrics/render/adaptor 子包齐全

使用示例

服务骨架
import (
    hertzserver "github.com/cloudwego/hertz/pkg/app/server"
    ekithertz "github.com/oekit/hertz/hertz"
)

h := hertzserver.New(hertzserver.WithHostPorts(":8080"),
    hertzserver.WithExitWaitTime(5*time.Second))
ekithertz.RunServer(app, "my-service", h)
SSE 流式推送
import ekitsse "github.com/oekit/hertz/hertz/sse"

h.GET("/stream", func(ctx context.Context, c *app.RequestContext) {
    s := ekitsse.NewStream(c)
    defer s.Close()
    _ = s.Send("progress", map[string]any{"pct": 50})
    _ = s.KeepAlive()
})

> ⚠️ **建议**:`hlog.SetSilentMode(true)` 屏蔽客户端异常断连噪音;`WithExitWaitTime` > 最大请求耗时,且 < K8s terminationGracePeriod。
## 包清单

- \`hertz/\` — 运行时集成:日志桥接(hlog→logx)、RunServer 生命周期、Shutdown Hook;子包 adaptor/health/metrics/render/sse
- \`handler/\` — crudh(CRUD HTTP handler)、validatorh(参数绑定)、versionh
- \`registry/hertz/\`、\`nns/hertz/\` — Hertz 注册发现适配器
- \`governance/hertz/\` — 动态限流/超时中间件
- \`observability/tracing/instrumentation/hertzotel|hertztracer/\` — OTel / OpenTracing 桥接

> 自研标准中间件栈与 contrib 桥接已迁 [ekit-middleware](https://github.com/oekit/middleware);handler 层(crudh/validatorh/versionh)已迁 [ekit-handler](https://github.com/oekit/handler)——本仓库因此**不再依赖 GORM 与 middleware 栈**。

## 矩阵总览(23 活跃模块,自动生成)

| 模块 | 内容 | 矩阵内依赖 |
|---|---|---|
| [pkg](https://github.com/oekit/pkg) | 微工具集 16 合 1(errors/validator/stringx/slicex/enumx/convert/encoding/jsonrepair/httpjson/mathx/pathx/pagination/ptrx/timex/uidgen/cache) | — |
| [concurrency](https://github.com/oekit/concurrency) | 并发与流控原语(async/group/singleflight/progress/syncx/ratelimit/retry) | — |
| [logx](https://github.com/oekit/logx) | 统一日志门面(多后端可替换 + Sink + caller 归属)+ 优雅关停 | — |
| [core](https://github.com/oekit/core) | 应用基建件(governance 治理 schema / lifecycle 契约 / network IP 工具 / version) | — |
| [istio](https://github.com/oekit/istio) | Istio Proxyless——Kitex xDS 动态熔断 | — |
| [metrics](https://github.com/oekit/metrics) | Prometheus 门面 + 弹性指标族 | [core](https://github.com/oekit/core) |
| [tracing](https://github.com/oekit/tracing) | OpenTelemetry 追踪 + trace_id 真相源 | [logx](https://github.com/oekit/logx) |
| [registry](https://github.com/oekit/registry) | 注册发现核心 + Nacos 实现 + 配置加载 + sdbridge | [core](https://github.com/oekit/core)、[logx](https://github.com/oekit/logx) |
| [data](https://github.com/oekit/data) | 缓存检查器(TTL/failTTL/inflight 合并)+ LocalCache | [concurrency](https://github.com/oekit/concurrency)、[logx](https://github.com/oekit/logx) |
| [gorm](https://github.com/oekit/gorm) | SQL/GORM 全家——连接池增强、框架无关 CRUD Service、仓储 | [logx](https://github.com/oekit/logx)、[pkg](https://github.com/oekit/pkg) |
| [redis](https://github.com/oekit/redis) | Redis 全家——客户端封装、缓存后端、分布式锁 | [concurrency](https://github.com/oekit/concurrency)、[data](https://github.com/oekit/data)、[logx](https://github.com/oekit/logx) |
| [httpx](https://github.com/oekit/httpx) | HTTP 客户端 + 负载均衡——服务发现/熔断/重试/指标/OTel 透传 | [concurrency](https://github.com/oekit/concurrency)、[core](https://github.com/oekit/core)、[logx](https://github.com/oekit/logx)、[metrics](https://github.com/oekit/metrics)、[registry](https://github.com/oekit/registry)、[tracing](https://github.com/oekit/tracing) |
| [network](https://github.com/oekit/network) | 轻量网络客户端——WebSocket/NATS/MQ 抽象、连接与流工具、Octopus 推送 | [concurrency](https://github.com/oekit/concurrency)、[logx](https://github.com/oekit/logx)、[tracing](https://github.com/oekit/tracing) |
| [timertask](https://github.com/oekit/timertask) | 分片定时任务(FNV-1a、心跳续约) | [concurrency](https://github.com/oekit/concurrency)、[logx](https://github.com/oekit/logx) |
| [middleware](https://github.com/oekit/middleware) | Hertz 自研标准中间件栈 + contrib 桥接 + hmw | [concurrency](https://github.com/oekit/concurrency)、[core](https://github.com/oekit/core)、[logx](https://github.com/oekit/logx)、[metrics](https://github.com/oekit/metrics)、[pkg](https://github.com/oekit/pkg)、[registry](https://github.com/oekit/registry)、[tracing](https://github.com/oekit/tracing) |
| [nns](https://github.com/oekit/nns) | NNS 服务治理客户端——provider 无关发现/配置/实例缓存 | [concurrency](https://github.com/oekit/concurrency)、[core](https://github.com/oekit/core)、[logx](https://github.com/oekit/logx)、[registry](https://github.com/oekit/registry) |
| [nns-endpoint](https://github.com/oekit/nns-endpoint) | NNS 端点工厂——per-service 熔断、换实例重试、gRPC 池 | [concurrency](https://github.com/oekit/concurrency)、[core](https://github.com/oekit/core)、[httpx](https://github.com/oekit/httpx)、[logx](https://github.com/oekit/logx)、[metrics](https://github.com/oekit/metrics)、[nns](https://github.com/oekit/nns)、[registry](https://github.com/oekit/registry) |
| [bootstrap](https://github.com/oekit/bootstrap) | 应用生命周期引导——bootstrap.Run 骨架 + healthz | [concurrency](https://github.com/oekit/concurrency)、[logx](https://github.com/oekit/logx) |
| [handler](https://github.com/oekit/handler) | HTTP handler 层——通用 CRUD Handler、参数绑定、版本端点 | [concurrency](https://github.com/oekit/concurrency)、[core](https://github.com/oekit/core)、[gorm](https://github.com/oekit/gorm)、[logx](https://github.com/oekit/logx)、[metrics](https://github.com/oekit/metrics)、[middleware](https://github.com/oekit/middleware)、[pkg](https://github.com/oekit/pkg)、[registry](https://github.com/oekit/registry)、[tracing](https://github.com/oekit/tracing) |
| [hertz](https://github.com/oekit/hertz) | Hertz 运行时集成——日志桥接、生命周期、SSE/health/metrics、适配器、OTel | [bootstrap](https://github.com/oekit/bootstrap)、[concurrency](https://github.com/oekit/concurrency)、[core](https://github.com/oekit/core)、[logx](https://github.com/oekit/logx)、[metrics](https://github.com/oekit/metrics)、[nns](https://github.com/oekit/nns)、[registry](https://github.com/oekit/registry)、[tracing](https://github.com/oekit/tracing) |
| [kitex](https://github.com/oekit/kitex) | Kitex 生态——日志桥接、治理热更、生命周期、BizError 桥接、kitexotel | [bootstrap](https://github.com/oekit/bootstrap)、[concurrency](https://github.com/oekit/concurrency)、[core](https://github.com/oekit/core)、[logx](https://github.com/oekit/logx)、[metrics](https://github.com/oekit/metrics)、[nns](https://github.com/oekit/nns)、[pkg](https://github.com/oekit/pkg)、[registry](https://github.com/oekit/registry) |
| [workflow](https://github.com/oekit/workflow) | 组合式流程编排——串行/并行/异步/条件分支 + Hook AOP | [concurrency](https://github.com/oekit/concurrency)、[logx](https://github.com/oekit/logx) |
| [kitex-examples](https://github.com/oekit/kitex-examples) | Kitex echo 示例——client/server + IDL + 治理配置 | [bootstrap](https://github.com/oekit/bootstrap)、[concurrency](https://github.com/oekit/concurrency)、[core](https://github.com/oekit/core)、[kitex](https://github.com/oekit/kitex)、[logx](https://github.com/oekit/logx)、[metrics](https://github.com/oekit/metrics)、[nns](https://github.com/oekit/nns)、[pkg](https://github.com/oekit/pkg)、[registry](https://github.com/oekit/registry)、[tracing](https://github.com/oekit/tracing) |
## 当前仓库依赖

详见上方矩阵表中本仓库行。

## 与 ekit 单体仓库的关系

- 代码与 ekit v0.34.0 等价,module 路径 `git.enjoye.top/enjoydream/ekit/...` → `github.com/oekit/hertz/...`
- 各子仓库独立发版(SemVer);开发规范沿用 ekit AGENTS.md 约定

## License

见 [LICENSE](LICENSE)

Directories

Path Synopsis
governance
hertz
Package govhertz 把 governance.Store 消费成 hertz server 治理中间件 (audit 5.2 阶段 1:hertz 侧治理)。
Package govhertz 把 governance.Store 消费成 hertz server 治理中间件 (audit 5.2 阶段 1:hertz 侧治理)。
Package hertz 为 Hertz HTTP 框架提供 ekit 生态集成。
Package hertz 为 Hertz HTTP 框架提供 ekit 生态集成。
adaptor
Package adaptor 提供 hertz-contrib/adaptor 常用 helper,让 ekit 业务 用 1 行代码把 net/http 生态(http.FileServer / embed.FS / gorilla/websocket)接入 hertz。
Package adaptor 提供 hertz-contrib/adaptor 常用 helper,让 ekit 业务 用 1 行代码把 net/http 生态(http.FileServer / embed.FS / gorilla/websocket)接入 hertz。
health
Package health 提供 Hertz 健康检查端点。
Package health 提供 Hertz 健康检查端点。
metrics
Package metrics 提供 Hertz Prometheus 指标端点。
Package metrics 提供 Hertz Prometheus 指标端点。
render
Package render 提供业务特定的 hertz Render interface 实现,与 hertz 官方 render 子包(https://www.cloudwego.io/zh/docs/hertz/tutorials/basic-feature/render/) 风格一致。
Package render 提供业务特定的 hertz Render interface 实现,与 hertz 官方 render 子包(https://www.cloudwego.io/zh/docs/hertz/tutorials/basic-feature/render/) 风格一致。
sse
Package sse 提供 Hertz SSE(Server-Sent Events)封装。
Package sse 提供 Hertz SSE(Server-Sent Events)封装。
nns
hertz
Package hertz 提供 nns 与 CloudWeGo Hertz 集成的适配器。
Package hertz 提供 nns 与 CloudWeGo Hertz 集成的适配器。
observability
tracing/instrumentation/hertzotel
Package hertzotel 提供 Hertz HTTP client/server 的 OpenTelemetry tracing 集成 helper,桥接 hertz-contrib/obs-opentelemetry/tracing。
Package hertzotel 提供 Hertz HTTP client/server 的 OpenTelemetry tracing 集成 helper,桥接 hertz-contrib/obs-opentelemetry/tracing。
tracing/instrumentation/hertztracer
Package hertztracer 提供 Hertz HTTP client/server 的 OpenTracing tracing 集成 helper,桥接 hertz-contrib/tracer。
Package hertztracer 提供 Hertz HTTP client/server 的 OpenTracing tracing 集成 helper,桥接 hertz-contrib/tracer。
registry
hertz
Package hertz 提供 ekit registry 抽象到 CloudWeGo Hertz 框架的适配器。
Package hertz 提供 ekit registry 抽象到 CloudWeGo Hertz 框架的适配器。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL