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 基于 Ent ORM 框架提供数据库适配器,同时集成了服务注册发现等插件。

一、数据库适配器

基于 Ent ORM,统一 ent.Client 接口。

1.1 支持的数据库

数据库导入路径Dialect
MySQLplugins/database/mysqlmysql
PostgreSQLplugins/database/postgrespostgres
SQLiteplugins/database/sqlitesqlite3
ClickHouseplugins/database/clickhouseclickhouse
SQL Serverplugins/database/mssqlmssql
MariaDBplugins/database/mariadbmysql
TiDBplugins/database/tidbmysql
MongoDBplugins/database/mongoMongoDB Driver

1.2 使用示例

import mysqlPlugin "github.com/tx7do/go-wind-plugins/database/mysql"

client, _ := mysqlPlugin.New(
    mysqlPlugin.WithDSN("user:pass@tcp(localhost:3306)/mydb?parseTime=true"),
    mysqlPlugin.WithMaxOpenConns(100),
    mysqlPlugin.WithMaxIdleConns(20),
    mysqlPlugin.WithConnMaxLifetime(300 * time.Second),
    mysqlPlugin.WithLogLevel("info"),
    mysqlPlugin.WithSlowThreshold(200 * time.Millisecond),
)

1.3 YAML 配置

database:
  default:               # 默认连接
    driver: mysql
    dsn: "user:pass@tcp(localhost:3306)/mydb?parseTime=true"
    max_open_conns: 100
    max_idle_conns: 20
    conn_max_lifetime: 300s
    log_level: warn      # silent | error | warn | info
    slow_threshold: 200ms

  readonly:              # 只读副本
    driver: postgres
    dsn: "postgres://user:pass@replica:5432/mydb?sslmode=disable"
    max_open_conns: 50

  analytics:             # 分析库
    driver: clickhouse
    dsn: "clickhouse://localhost:9000/analytics"

1.4 数据库迁移

// 自动迁移
client := mysqlPlugin.GetClient()
client.Schema.Create(ctx,
    schema.WithAtlas(true),                    // 使用 Atlas 迁移引擎
    schema.WithDropIndex(true),
    schema.WithDropColumn(true),
)

二、服务注册发现

2.1 适配器列表

注册中心导入路径特点
Consulplugins/registry/consulHashiCorp、健康检查
Etcdplugins/registry/etcdKubernetes 基础、Raft 一致性
Nacosplugins/registry/nacos阿里系、配置+注册一体化
ZooKeeperplugins/registry/zookeeper经典、强一致性
Eurekaplugins/registry/eurekaSpring Cloud 集成

2.2 服务注册

import consulPlugin "github.com/tx7do/go-wind-plugins/registry/consul"

reg := consulPlugin.New(
    consulPlugin.WithAddr("localhost:8500"),
    consulPlugin.WithServiceName("my-service"),
    consulPlugin.WithServiceAddr("10.0.0.1"),
    consulPlugin.WithServicePort(8080),
    consulPlugin.WithHealthCheck("http://10.0.0.1:8080/health", 10*time.Second),
    consulPlugin.WithTags("v1", "primary"),
)

2.3 服务发现

// 获取服务实例列表
instances, _ := reg.GetService(ctx, "my-service")
for _, ins := range instances {
    fmt.Printf("%s:%d (weight=%d, healthy=%v)\n",
        ins.Address, ins.Port, ins.Weight, ins.Healthy)
}

// 负载均衡
lb := registry.NewRoundRobin(instances)
instance := lb.Select()

2.4 YAML 配置

registry:
  consul:
    addr: "localhost:8500"
    scheme: http
    health_check:
      interval: 10s
      timeout: 5s
      deregister_after: 60s
    tags: ["v1", "primary"]

三、配置中心

3.1 适配器列表

配置中心导入路径特点
Fileplugins/config/file本地 YAML/JSON
Etcdplugins/config/etcd分布式 KV
Consulplugins/config/consulConsul KV
Nacosplugins/config/nacos配置+注册
Apolloplugins/config/apollo携程开源

3.2 动态配置

import nacosConfigPlugin "github.com/tx7do/go-wind-plugins/config/nacos"

source := nacosConfigPlugin.New(
    nacosConfigPlugin.WithAddr("localhost:8848"),
    nacosConfigPlugin.WithNamespace("production"),
    nacosConfigPlugin.WithDataID("my-service.yaml"),
    nacosConfigPlugin.WithGroup("DEFAULT_GROUP"),
    nacosConfigPlugin.WithWatch(true),     // 监听配置变更
)

3.3 配置热更新

source.Watch(func(event config.Event) {
    fmt.Println("config changed:", event.Key)
    // 重新加载配置
    reloadConfig(event.Value)
})

四、多源配置合并

config:
  sources:
    - type: file
      path: ./config.yaml
    - type: nacos
      addr: localhost:8848
      data_id: my-service.yaml
  # 后加载的源覆盖先加载的

相关文档

  • 插件配置系统
  • 缓存插件
  • 插件总览
Edit this page
Last Updated:: 6/21/26, 9:28 PM
Contributors: Bobo
Prev
工作流插件
Next
Bootstrap 集成与网关插件