GoWindGoWind
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
GitHub
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
GitHub
  • 介绍

    • GoWind 框架
    • 框架整体架构
  • go-wind 核心

    • go-wind 核心框架
    • App 生命周期管理
    • Context 传播
    • Transport 抽象
    • Log 门面接口
  • go-wind-plugins 插件

    • go-wind-plugins 插件总览
    • 插件配置系统
    • 插件注册机制(SPI)
    • 日志适配插件
    • 传输协议插件
    • 消息中间件插件
    • 编码解码插件
    • 安全与认证插件
    • 链路追踪插件
    • 缓存插件
    • 对象存储插件(OSS)
    • 限流插件
    • 指标监控插件
    • AI 插件
    • 工作流插件
    • 数据库与缓存插件
    • Bootstrap 集成与网关插件
    • 压缩与序列化插件
    • 模板渲染与验证插件
  • go-wind-bootstrap 启动器

    • go-wind-bootstrap 声明式启动器
    • Bootstrap 配置系统
    • Bootstrap SPI 机制
    • 声明式中间件编排
    • Bootstrap 命令行集成
    • Bootstrap 实战示例
  • go-wind-flow 流程引擎

    • GoWind Flow 流程引擎
  • 教程

    • 快速入门教程
    • 自定义插件开发教程
    • 多协议同时监听教程
    • 框架迁移指南

声明式中间件编排

go-wind-bootstrap 支持通过 YAML 声明中间件链,无需在代码中手动编写中间件初始化:中间件通过配置文件的 optional 字段启用,参数也通过配置传入。

一、中间件配置模型

HTTP / gRPC 中间件定义为 server.proto 中 Middleware 消息的 optional 字段——字段出现即启用(recovery: {} 表示以默认参数启用)。注册顺序遵循 proto 字段序,与 YAML 书写顺序无关。

server:
  http:
    addr: ":8080"
    middleware:
      recovery:
        stack_trace: true
      cors:
        allowed_origins:
          - "*"
        allowed_methods:
          - GET
          - POST
      logging:
        skip_paths:
          - /health
      request_id:
        header_name: X-Request-ID

二、内置中间件

2.1 HTTP 中间件

中间件配置字段说明
Recoveryrecovery异常恢复,可选 stack_trace(记录堆栈,默认 true)
CORScors跨域资源共享:allowed_origins / allowed_methods / allowed_headers / exposed_headers / allow_credentials / max_age
Logginglogging请求日志,可选 skip_paths
Request IDrequest_id请求 ID 注入,可选 header_name(默认 X-Request-Id)
TracingtracingOpenTelemetry 链路追踪,自动使用全局 TracerProvider,无需额外参数
Rate Limitrate_limit限流:rate(每秒请求数)/ burst(突发容量)/ wait(等待模式)
Timeouttimeout请求超时:default_timeout_ms / status_code(默认 503)

2.2 gRPC 中间件

gRPC 中间件均为开关型(消息体为空,出现即启用):

中间件配置字段说明
Recoveryrecovery异常恢复,返回 Internal 错误
Logginglogging请求日志
TracingtracingOpenTelemetry 链路追踪
Validatevalidate请求校验
server:
  grpc:
    addr: ":9000"
    middleware:
      recovery: {}
      logging: {}
      tracing: {}
      validate: {}

三、执行顺序

HTTP 中间件按 proto 字段序注册,形成洋葱模型:

recovery → cors → logging → request_id → tracing → rate_limit → timeout
    ↓ 请求按序穿过每一层
  → → → Handler → → →
  ← ← ← 响应按逆序返回 ← ← ←

即 Recovery 最先捕获 panic,Timeout 最贴近业务 Handler。

四、自定义中间件

配置面之外的中间件(业务审计、自定义认证等)通过适配器的追加式注册 API 挂载:RegisterServerSetup 的回调在配置中间件挂载完成后执行,回调内通过 srv.Use 添加自定义中间件、srv.GET 等方法注册路由。

package main

import (
    "net/http"

    httpAdapter "github.com/tx7do/go-wind-bootstrap/transport/http"
)

// 自定义中间件:类型为 func(http.Handler) http.Handler
func auditLog(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // 记录审计日志...
        next.ServeHTTP(w, r)
    })
}

func init() {
    httpAdapter.RegisterServerSetup(func(srv *httpAdapter.Server) {
        srv.Use(auditLog)
        srv.GET("/health", func(w http.ResponseWriter, r *http.Request) {
            w.WriteHeader(http.StatusOK)
        })
    })
}

RegisterServerSetup 是追加式注册,支持多处调用(代码生成器与手写代码共存),回调按注册顺序执行。相关路由注册模式见 Bootstrap 实战示例。

相关文档

  • Bootstrap 介绍
  • Bootstrap 配置系统
  • Bootstrap 实战示例
  • 安全与认证插件
  • 限流插件
Edit this page
最近更新:: 2026/11/3 12:42
Contributors: Bobo
Prev
Bootstrap SPI 机制
Next
Bootstrap 命令行集成