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-plugins 的每个插件都支持多种配置方式:函数式选项、配置文件、环境变量。在 bootstrap 模式下,通过 YAML 声明式配置自动装配。配置本身可来自 config/ 家族的 14 个配置源插件(见下文)。

一、配置源插件(Config)

config/ 家族实现统一的 config.Source 接口,共 14 个插件:

插件导入路径引擎
Apolloplugins/config/apollo携程 Apollo
Consulplugins/config/consulHashiCorp Consul KV
Etcdplugins/config/etcdCoreOS etcd
Envplugins/config/env环境变量
Fileplugins/config/file本地文件
FSplugins/config/fsfs.FS
HTTPplugins/config/httpHTTP 远程拉取
Kubernetesplugins/config/kubernetesK8s ConfigMap / Secret
Nacosplugins/config/nacos阿里云 Nacos
OSSplugins/config/oss对象存储
Polarisplugins/config/polaris腾讯云 Polaris
Redisplugins/config/redisRedis KV
Vaultplugins/config/vaultHashiCorp Vault
Zookeeperplugins/config/zookeeperApache ZooKeeper

多个配置源可组合使用:FallbackReader 按优先级依次尝试多个源,实现多源回退。

二、配置层级

# 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/crud/gorm"
)

func main() {
    // bootstrap 自动读取 config.yaml
    // 根据 server.http 配置创建 HTTP Server
    // 根据 cache.redis 配置创建 Redis Client
    // 根据 database 配置创建数据访问客户端
    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
最近更新:: 2026/11/3 12:42
Contributors: Bobo
Prev
go-wind-plugins 插件总览
Next
插件注册机制(SPI)