GoWind 开源生态GoWind 开源生态
首页
框架
GoWind Admin
GoWind CMS
GoWind IM
GoWind UBA
GoWind IoT
GoWind Toolkit
GoWind Quant
GitHub
首页
框架
GoWind Admin
GoWind CMS
GoWind IM
GoWind UBA
GoWind IoT
GoWind Toolkit
GoWind Quant
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 CLI 工具
    • Bootstrap 实战示例
  • 教程

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

插件配置系统

go-wind-plugins 的每个插件都支持多种配置方式:函数式选项、配置文件、环境变量。在 bootstrap 模式下,通过 YAML 声明式配置自动装配。

一、配置层级

# config.yaml
server:
  http:
    addr: ":8080"
    timeout: 30s
    cors:
      enabled: true
      origins: ["*"]

database:
  default:                    # 数据库连接名
    driver: mysql
    dsn: "user:pass@tcp(localhost:3306)/db?parseTime=true"
    max_open_conns: 100
    max_idle_conns: 20
    conn_max_lifetime: 300s

cache:
  redis:
    addr: "localhost:6379"
    db: 0
    pool_size: 50

broker:
  kafka:
    addrs: ["localhost:9092"]
    group_id: "my-service"

二、函数式选项

所有插件都提供 With* 系列函数式选项:

import (
    httpPlugin "github.com/tx7do/go-wind-plugins/transport/http"
    redisPlugin "github.com/tx7do/go-wind-plugins/cache/redis"
    zapPlugin "github.com/tx7do/go-wind-plugins/log/zap"
)

// 手动构建
httpServer := httpPlugin.NewServer(
    httpPlugin.WithAddr(":8080"),
    httpPlugin.WithHandler(router),
    httpPlugin.WithReadTimeout(10*time.Second),
)

cache := redisPlugin.NewClient(
    redisPlugin.WithAddr("localhost:6379"),
    redisPlugin.WithDB(0),
    redisPlugin.WithPoolSize(50),
)

logger := zapPlugin.NewLogger(
    zapPlugin.WithLevel("info"),
    zapPlugin.WithOutput("stdout"),
    zapPlugin.WithFormat("json"),
)

三、Bootstrap 自动装配

使用 bootstrap 时,配置文件自动映射到插件选项:

import (
    _ "github.com/tx7do/go-wind-plugins/transport/http"     // blank import
    _ "github.com/tx7do/go-wind-plugins/cache/redis"
    _ "github.com/tx7do/go-wind-plugins/log/zap"
    _ "github.com/tx7do/go-wind-plugins/database/mysql"
)

func main() {
    // bootstrap 自动读取 config.yaml
    // 根据 server.http 配置创建 HTTP Server
    // 根据 cache.redis 配置创建 Redis Client
    // 根据 database.default 配置创建 Ent Client
    app := bootstrap.New("config.yaml")
    app.Run()
}

四、配置结构映射

每个插件定义自己的配置结构体:

// transport/http/config.go
package http

type Config struct {
    Addr         string        `yaml:"addr"`
    Timeout      time.Duration `yaml:"timeout"`
    ReadTimeout  time.Duration `yaml:"read_timeout"`
    WriteTimeout time.Duration `yaml:"write_timeout"`
    CORS         CORSConfig    `yaml:"cors"`
    TLS          TLSConfig     `yaml:"tls"`
}

type CORSConfig struct {
    Enabled    bool     `yaml:"enabled"`
    Origins    []string `yaml:"origins"`
    Methods    []string `yaml:"methods"`
    Headers    []string `yaml:"headers"`
    Credentials bool    `yaml:"credentials"`
}

五、环境变量覆盖

配置值可通过环境变量覆盖,格式为 大写_下划线 连接:

# 覆盖 server.http.addr
export SERVER_HTTP_ADDR=":9090"

# 覆盖 database.default.dsn
export DATABASE_DEFAULT_DSN="user:pass@tcp(prod-db:3306)/db"

# 覆盖 cache.redis.addr
export CACHE_REDIS_ADDR="prod-redis:6379"

六、多实例配置

某些插件支持多实例,通过连接名区分:

database:
  default:        # 主库
    driver: mysql
    dsn: "user:pass@tcp(master:3306)/db"
  readonly:       # 只读副本
    driver: mysql
    dsn: "user:pass@tcp(replica:3306)/db"
  analytics:      # 分析库
    driver: clickhouse
    dsn: "clickhouse://localhost:9000/analytics"

cache:
  default:        # 主缓存
    driver: redis
    addr: "localhost:6379"
  session:        # Session 缓存
    driver: redis
    addr: "localhost:6380"

相关文档

  • 插件注册机制
  • 插件总览
  • Bootstrap 配置系统
  • 核心框架介绍
Edit this page
Last Updated:: 6/21/26, 9:28 PM
Contributors: Bobo
Prev
go-wind-plugins 插件总览
Next
插件注册机制(SPI)