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 核心框架

go-wind 是 GoWind 微服务生态的核心框架,提供应用生命周期管理、传输层抽象、日志门面和上下文传播。核心代码不到 500 行,仅依赖 golang.org/x/sync。

一、设计哲学

不是一揽子全栈框架,而是一盒积木。

原则说明
组合优于继承每个插件只实现标准接口
接口优于实现核心只定义最小接口,实现由插件提供
零隐藏魔法所有行为显式声明,无隐式初始化
零外部依赖仅依赖 golang.org/x/sync
函数式选项WithServer, WithName 等链式配置

二、核心模块

go-wind/
├── app.go          # App 引擎:生命周期管理
├── context.go      # Context:请求级元数据传播
├── instance.go     # 服务实例模型
├── errors.go       # 统一错误定义
├── transport/      # Transport 抽象
│   └── transport.go
└── log/            # Log 门面
    ├── log.go
    ├── level.go
    └── global.go

三、App 引擎

3.1 创建 App

package main

import (
    "context"
    "github.com/tx7do/go-wind"
)

func main() {
    app := wind.New(
        wind.WithName("my-service"),
        wind.WithServer(myHTTPServer),
        wind.WithServer(myGRPCServer),
    )

    if err := app.Run(context.Background()); err != nil {
        panic(err)
    }
}

3.2 函数式选项

选项说明
WithName(name)设置服务名称
WithServer(server)添加 Transport Server
WithLogger(logger)设置日志器
WithShutdownTimeout(d)设置优雅停止超时
WithSignalHandler()启用信号处理(SIGTERM/SIGINT)

3.3 Server 接口

type Server interface {
    Start(context.Context) error
    Stop(context.Context) error
    Endpoint() []string
    Type() string
}

任何实现此接口的类型都可以作为 Server 注册到 App。

四、快速示例

package main

import (
    "context"
    "fmt"
    "net/http"
    "github.com/tx7do/go-wind"
)

// 自定义 HTTP Server
type MyServer struct {
    server *http.Server
}

func (s *MyServer) Start(ctx context.Context) error {
    s.server = &http.Server{Addr: ":8080", Handler: s.routes()}
    go s.server.ListenAndServe()
    return nil
}

func (s *MyServer) Stop(ctx context.Context) error {
    return s.server.Shutdown(ctx)
}

func (s *MyServer) Endpoint() []string {
    return []string{"http://localhost:8080"}
}

func (s *MyServer) Type() string { return "http" }

func (s *MyServer) routes() http.Handler {
    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "Hello, GoWind!")
    })
    return mux
}

func main() {
    app := wind.New(
        wind.WithName("hello-service"),
        wind.WithServer(&MyServer{}),
        wind.WithSignalHandler(),
    )
    app.Run(context.Background())
}

相关文档

  • App 生命周期管理
  • Context 传播
  • Transport 抽象
  • Log 门面接口
  • 框架整体架构
Edit this page
Last Updated:: 6/21/26, 9:28 PM
Contributors: Bobo
Next
App 生命周期管理