Skip to content

tech config ops

ez4bk edited this page Oct 30, 2026 · 1 revision

配置与运维

这页讲三件事:配置文件是怎么被找到和解析的、有哪些键、上线时要注意什么。

配置文件从哪来

框架在 ProvideConfig 阶段加载配置,顺序固定:

.env(如果存在)            ← godotenv
  ↓
环境变量                     ← EZ_MODE / EZ_ENV / EZ_LOG_LEVEL
  ↓
config/<mode>.yml            ← 主配置,严格解码
  ↓
config/config_rewrite.yml    ← 覆写文件(可选)
  ↓
config/<mode>_custom.yml     ← 用户配置(可选),只进全局 viper

<mode> 取值来自 EZ_MODE(或别名 EZ_ENV):local、dev、test、stage、prod,默认 local。写错会直接 panic,而不是悄悄回落。

主配置由 config.Config 承载,解码时 SetStrict(true):框架不认识的键会报错。所以往 <mode>.yml 里写自定义字段会被拒绝——自定义配置请写进 <mode>_custom.yml。

主配置有哪些键

app:
  name: demo
  ver: 1.0.0
  host: 0.0.0.0
  port: 8899

  pprof_host: ""        # 与 host/port 相同或全空 → pprof 挂业务端口
  pprof_port: 0

  db_migration:
    auto_migrate: true  # 启动插件前执行迁移

  lifecycle:
    ready_timeout: 10s
    drain_timeout: 10s
    close_timeout: 5s

  middleware:
    logger: { ... }
    http: { logger: { excludes: [] } }
    grpc:
      server:
        default:
          _startup: true
          addr: ":11211"
    monitor:            # Prometheus,默认关闭
      _startup: false
      listen: ""
      metric: "/metrics"
    tracing: { ... }
    mysql: { ... }
    redis: { ... }
    kafka: { ... }
    rabbitmq: { ... }
    elasticsearch: { ... }
    etcd: { ... }

每个中间件段的细节见基础设施插件与传输层插件。

用户配置

业务自己的配置放 config/<mode>_custom.yml,用自己的结构体读:

type ConsumerConfig struct {
	Topic string `yaml:"topic"`
	Group string `yaml:"group"`
}

var cfg ConsumerConfig
if err := ezboot.LoadUserConfig(&cfg); err != nil {
	return nil, err
}

两个已知边界:

  • <mode>_custom.yml 只进全局 viper,不影响 config.Config。写进 custom 的框架字段不会生效,也不会报错。
  • 目前不会对"写了但没人用"的字段告警。拼错字段名是静默的,只能靠回读配置或单测发现。

超时预算怎么算

生命周期预算是按插件计的,而且串行:

总关闭时间 ≈ Σ(drain_timeout) + Σ(close_timeout)

模板项目有 7 个插件,默认值下最坏情况是 7×10s + 7×5s = 105s。K8s 默认的 30 秒会被这个值直接打爆,所以必须保证:

terminationGracePeriodSeconds: 120  # 大于 Σdrain + Σclose,再留余量

否则 Pod 会在排空完成前被 SIGKILL,Drain 里的等待就白做了。

K8s 探针

探针 路径 含义
liveness /_healthy 进程还活着;一直 200,不查依赖
readiness /readyz 该不该接流量;started && !draining 才 200

建议:

readinessProbe:
  httpGet: { path: /readyz, port: 8899 }
  periodSeconds: 5
  timeoutSeconds: 3
  failureThreshold: 2

timeoutSeconds 给 3 秒是因为 /readyz 是纯内存判断,正常在微秒级;留出余量避免网络抖动误判。

上线前检查

  • pprof_host / pprof_port 是否符合预期(默认会暴露 /debug/pprof)
  • monitor._startup 是否需要打开;打开后 /metrics 挂在哪
  • terminationGracePeriodSeconds 是否覆盖 drain + close 预算
  • db_migration.auto_migrate 在多副本部署时是否该关掉(否则多个实例同时迁移);它目前只认 middleware.mysql.default,SQLite / PostgreSQL 不会自动迁移
  • <mode>_custom.yml 里的字段拼写是否与结构体一致

相关页面

Clone this wiki locally

⌂ 换网址