go-wind-plugins 基于 Ent ORM 框架提供数据库适配器,同时集成了服务注册发现等插件。
基于 Ent ORM,统一 ent.Client 接口。
| 数据库 | 导入路径 | Dialect |
|---|
| MySQL | plugins/database/mysql | mysql |
| PostgreSQL | plugins/database/postgres | postgres |
| SQLite | plugins/database/sqlite | sqlite3 |
| ClickHouse | plugins/database/clickhouse | clickhouse |
| SQL Server | plugins/database/mssql | mssql |
| MariaDB | plugins/database/mariadb | mysql |
| TiDB | plugins/database/tidb | mysql |
| MongoDB | plugins/database/mongo | MongoDB Driver |
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),
)
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
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"
client := mysqlPlugin.GetClient()
client.Schema.Create(ctx,
schema.WithAtlas(true),
schema.WithDropIndex(true),
schema.WithDropColumn(true),
)
| 注册中心 | 导入路径 | 特点 |
|---|
| Consul | plugins/registry/consul | HashiCorp、健康检查 |
| Etcd | plugins/registry/etcd | Kubernetes 基础、Raft 一致性 |
| Nacos | plugins/registry/nacos | 阿里系、配置+注册一体化 |
| ZooKeeper | plugins/registry/zookeeper | 经典、强一致性 |
| Eureka | plugins/registry/eureka | Spring Cloud 集成 |
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"),
)
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()
registry:
consul:
addr: "localhost:8500"
scheme: http
health_check:
interval: 10s
timeout: 5s
deregister_after: 60s
tags: ["v1", "primary"]
| 配置中心 | 导入路径 | 特点 |
|---|
| File | plugins/config/file | 本地 YAML/JSON |
| Etcd | plugins/config/etcd | 分布式 KV |
| Consul | plugins/config/consul | Consul KV |
| Nacos | plugins/config/nacos | 配置+注册 |
| Apollo | plugins/config/apollo | 携程开源 |
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),
)
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