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 流程引擎
  • 教程

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

Bootstrap 配置系统

go-wind-bootstrap 的配置系统以 Protobuf 为契约:顶层 BootstrapConfig 由 conf/proto/bootstrap/v1/ 下的 .proto 文件定义,一份 YAML 即可描述完整的应用拓扑——传输层、配置中心、注册中心、日志、追踪、指标、消息代理、存储、AI、工作流、缓存、脚本引擎、数据库。

一、配置结构(Protobuf 契约)

BootstrapConfig 的顶层字段与 proto 文件一一对应:

字段Proto 文件职责
appapp.proto应用元数据:id / name / version / env / stop_timeout
serverserver.proto传输层 + 中间件(HTTP、gRPC、MCP、Cron 等 optional 字段)
configconfig.proto配置中心源(可混合多种)
registryregistry.proto服务注册发现
loggerlog.proto日志系统
tracertracer.proto分布式追踪
metricsmetrics.proto指标监控
brokerbroker.proto消息代理
storagestorage.proto对象存储(MinIO / S3)
aiai.protoAI 模型客户端
workflowworkflow.proto工作流引擎
cachecache.proto缓存(本地内存 / Redis)
scriptscript.proto脚本引擎
databasedatabase.proto数据库

配置即应用:bootstrap 引擎读取 BootstrapConfig,通过注册的 Builder 自动组装 wind.App:

BootstrapConfig → Builder Registry → wind.Option 链 → wind.App

二、一份 YAML 描述完整应用拓扑

app:
  id: my-service-01
  name: my-service
  version: "1.0.0"
  env: production

server:
  http:
    addr: ":8080"
    middleware:
      recovery: {}
      cors:
        allowed_origins: ["*"]
      logging:
        skip_paths: ["/health"]
  grpc:
    addr: ":9000"
    reflection: true
    middleware:
      recovery: {}
      validate: {}

logger:
  type: zap
  zap:
    level: info
    format: json
    writer: file            # stdout / stderr / file
    output_path: logs/app.log
    max_size_mb: 100        # 配置滚动参数后走 lumberjack
    max_age_days: 7
    max_backups: 10
    compress: true

registry:
  type: etcd
  etcd:
    endpoints: ["localhost:2379"]
    ttl: 30
    dial_timeout: 5

tracer:
  type: otlp
  otlp:
    endpoint: "localhost:4317"
    insecure: true
    sampler: trace_id_ratio
    sample_ratio: 0.1

cache:
  redis:
    mode: standalone        # standalone / cluster / sentinel 按字段自动推断
    addr: "localhost:6379"
    key_prefix: "my-service"

database:
  sql:
    driver: mysql
    source: "user:pass@tcp(localhost:3306)/db?parseTime=true"
    max_open_connections: 100

三、配置面扩展

3.1 日志:zap stderr / 滚动日志,zerolog 滚动日志

  • zap 新增 writer 字段(stdout / stderr / file,默认 stdout)与滚动日志参数:max_size_mb(单文件最大体积 MB)、max_age_days(最大保留天数)、max_backups(最大备份数)、compress(压缩历史日志)。配置任一滚动参数后,文件输出走 lumberjack。
  • zerolog 同步补齐同一组滚动日志参数(max_size_mb / max_age_days / max_backups / compress)。
logger:
  type: zerolog
  zerolog:
    level: debug
    format: json
    output_path: logs/app.log
    max_size_mb: 50
    max_backups: 5

3.2 缓存:Redis 重写为 UniversalClient

Redis 缓存底层重写为 go-redis UniversalClient,单配置面覆盖三种部署形态,客户端模式按字段自动推断——配置 master_name 即 sentinel,配置 addrs(多于一个或配合 mode)即 cluster,否则 standalone:

cache:
  redis:
    mode: sentinel
    master_name: mymaster
    addrs: ["sentinel-1:26379", "sentinel-2:26379"]
    sentinel_username: sentinel-user
    sentinel_password: sentinel-pass
    key_prefix: "my-service"

同时补齐连接池与超时参数(dial_timeout、read_timeout、pool_size、min_idle_conns、max_retries 等)与 TLS 配置。

3.3 追踪:OTLP 适配器

追踪统一为 OTLP 一个类型,参数全部配置化:endpoint、insecure、headers、采样策略(always_on / always_off / trace_id_ratio / parent_based + sample_ratio)、use_http(HTTP 上报而非 gRPC)、批处理与导出超时:

tracer:
  type: otlp
  otlp:
    endpoint: "otel-collector:4317"
    sampler: parent_based
    use_http: false
    batch_timeout_ms: 5000

3.4 gRPC 服务器选项接线

gRPC 服务器的 TLS、反射、keepalive 等选项全部接入配置面:

server:
  grpc:
    addr: ":9000"
    reflection: true              # 启用 gRPC 反射
    tls:
      file:
        cert_path: /etc/certs/tls.crt
        key_path: /etc/certs/tls.key
    keepalive_time: 30            # 探测间隔(秒)
    keepalive_timeout: 10         # 探测等待响应超时(秒)
    keepalive_min_time: 300       # 客户端最小探测间隔(秒)
    connection_timeout: 20        # 请求级连接超时(秒)
    max_recv_msg_size: 4194304    # 最大接收消息(字节)
    max_send_msg_size: 4194304    # 最大发送消息(字节)
    max_concurrent_streams: 100   # 最大并发流

TLS 使用通用 TLS proto(文件路径或 PEM 内联 + insecure_skip_verify),同一结构也被 registry、config、cache 等配置面复用。

3.5 MCP 传输层参数配置化

MCP(Model Context Protocol)传输层支持 server_type(SSE / HTTP / STDIO / IN_PROCESS)、instructions(返回给客户端的能力说明)与细粒度能力开关:

server:
  mcp:
    name: my-mcp-server
    version: "1.0.0"
    address: ":8090"
    server_type: SSE
    instructions: "订单查询与管理工具集"
    recovery: true
    logging: true
    tool_capabilities: true
    resource_capabilities: true
    enable_input_schema_validation: true

3.6 对象存储:MinIO / S3 对齐新配置面

storage 支持 MinIO 与 S3 两种类型,字段对齐新配置面(endpoint、凭证、region、use_ssl、force_path_style、bucket 等):

storage:
  minio:
    endpoint: "localhost:9000"
    access_key: minioadmin
    secret_key: minioadmin
    use_ssl: false

3.7 数据库:同步 go-crud 新增向量 / 图库适配器

关系型/文档型/搜索型引擎升级 go-crud 后同步获得向量检索能力,并新增向量与图库适配器:Qdrant、Milvus、Weaviate(向量)、Neo4j(图),连同 GORM / MongoDB / ClickHouse / Doris / Elasticsearch / OpenSearch / InfluxDB / Cassandra 共 13 种,全部走 SPI 自注册。

四、字段映射由 protoc-gen-fieldmap 生成

各适配器里"配置字段 → 上游选项"的机械映射不再手工维护:每个适配器旁一份 mapping.spec.yaml 声明映射规则,由 protoc-gen-fieldmap 生成强类型的 mapping.gen.go(如 MapSQLOptions、NewMinioConfig);复合选项、TLS 装载等特殊字段以 skip: true 登记并保留在适配器手工段。

CI 中的 fieldmap-drift 任务按 spec 重新生成并逐字节比对 mapping.gen.go——改了 proto 忘了同步 spec 的 PR 会在 CI 直接变红,配置面与上游库选项之间不会发生漂移。完整维护约定见 protoc-gen-fieldmap 字段映射生成器。

五、配置加载与多格式

LoadConfigFromFile 按扩展名自动识别格式:.yaml / .yml → YAML,.json → JSON,.bin / .pb → Protobuf 二进制。命令行通过 Cobra 集成,-c/--conf 指定配置文件:

go run main.go -c config.yaml

六、程序内访问配置

BootstrapConfig 是强类型的 Protobuf 消息,程序内通过生成代码访问(非字符串路径取值)。半密封模式下 Context 提供类型化访问器:

bctx, err := bootstrap.BootstrapWithContext(nil, cfg)
if err != nil {
    log.Fatal(err)
}
defer bctx.Cleanup()

// 访问原始配置(Protobuf 消息)
appName := bctx.Config().GetApp().GetName()
httpAddr := bctx.Config().GetServer().GetHttp().GetAddr()

// 访问装配好的实例(按组件类型键取用,如 "gorm"/"redis"/"etcd")
db := bctx.Database("gorm")
cache := bctx.Cache("redis")
reg := bctx.Registry("etcd")   // GetService/Watch/Register/Deregister

// 运行
bctx.App().Run(bctx)

七、敏感信息管理

方式安全级别说明
环境变量注入高K8s Secret / Docker Secrets 启动前注入配置
Vault 配置源最高config.vault 配置源动态拉取
配置中心高Nacos/Apollo 配置源托管
TLS PEM 内联高证书可经 Secret 挂载,勿提交仓库
# 不要在配置文件中硬编码密码!
# 错误:source: "user:password123@tcp(localhost:3306)/db"
# 正确:部署时经环境变量/配置中心注入,仓库中的 YAML 只保留模板

相关文档

  • Bootstrap 介绍
  • Bootstrap SPI 机制
  • protoc-gen-fieldmap 字段映射生成器
  • 插件配置系统
  • 核心框架介绍
Edit this page
最近更新:: 2026/11/3 12:42
Contributors: Bobo
Prev
go-wind-bootstrap 声明式启动器
Next
Bootstrap SPI 机制