Go 框架三件套详解 | 青训营笔记

119 阅读3分钟

这是我参与「第五营」笔记创作活动的的第7天

Go 框架三件套详解

一、GORM

Gorm 是一个已经迭代了10年+的功能强大的 ORM 框架,在字节内部被广泛使用并且拥有非常丰富的开源扩展。

由于 Gorm 官方文档非常详细,而且有中文版本,这里附上官方文档链接: GORM 指南

⭐ 可以着重看这些部分:

⭐ GORM 拥有非常丰富的扩展生态,以下列举一部分常用扩展。

扩展网址
GORM 代码生成工具github.com/go-gorm/gen
GORM 分片库方案github.com/go-gorm/sha…
GORM 手动索引github.com/go-gorm/hin…
GORM 乐观锁github.com/go-gorm/opt…
GORM 读写分离github.com/go-gorm/dbr…
GORM OpenTelemetry 扩展github.com/go-gorm/ope…

二、Kitex

Kitex 是字节内部的 Golang 微服务 RPC 框架,具有高性能、强可扩展的主要特点,支持多协议并目拥有丰富的开源扩展。

同样,Kitex 官方文档也配有详细的中文版本,这里附上官方文档链接:Kitex | CloudWeGo

⭐ kitex 暂时没有针对 Windows 做支持,如果本地开发环境是 Windows 建议使用 WSL2

⭐ 可以着重看这些部分:

⭐ Kitex 拥有非常丰富的扩展生态,以下列举一部分常用扩展。

扩展网址
XDS 扩展github.com/kitex-contr…
opentelemetry 扩展github.com/kitex-contr…
ETCD 服务注册与发现扩展github.com/kitex-contr…
Nacos 服务注册与发现扩展github.com/kitex-contr…
Zookeeper 服务注册与发现扩展github.com/kitex-contr…
polaris 扩展github.com/kitex-contr…
丰富的示例代码与业务Demogithub.com/cloudwego/k…

三、Hertz

Hertz 是字节内部的 HTTP 框架参考了其他开源框架的优势,结合字节跳动内部的需求,具有高易用性、高性能、高扩展性特点。

⭐ Hertz 快速入门教程:快速开始 | CloudWeGo

⭐ Hertz 提供了 GETPOSTPUTDELETEANY 等方法用于注册路由。

func RegisterRoute(h *server.Hertz) {
    h.GET("/get", func(ctx context.Context, c *app.RequestContext) {
        c.String(consts.StatusOK, "get")
    })
    h.POST("/post", func(ctx context.Context, c *app.RequestContext) {
        c.String(consts.StatusOK, "post")
    })
    h.PUT("/put", func(ctx context.Context, c *app.RequestContext) {
        c.String(consts.StatusOK, "put")
    })
    h.DELETE("/delete", func(ctx context.Context, c *app.RequestContext) {
        c.String(consts.StatusOK, "delete")
    })
    h.PATCH("/patch", func(ctx context.Context, c *app.RequestContext) {
        c.String(consts.StatusOK, "patch")
    })
    h.HEAD("/head", func(ctx context.Context, c *app.RequestContext) {
        c.String(consts.StatusOK, "head")
    })
    h.OPTIONS("/options", func(ctx context.Context, c *app.RequestContext) {
        c.String(consts.StatusOK, "options")
    })
}

⭐ Hertz 提供了路由组( Group )的能力,用于支持路由分组的功能

// Simple group: v1
v1 := h.Group( relativePath:"/v1")
{
    // LoginEndpoint is a handler func
    v1.POST( relativePath:"/ogin",oginEndpoint)
    v1.POST( relativePath:"/submit", submitEndpoint)
    v1.POST( relativePath:"/streaming_read",readEndpoint)
}

// Simple group:v2
v2 := h.Group( relativePath: " /v2")
{
    v2.POST( relativePath:"/login",oginEndpoint)
    v2.POST( relativePath:"/submit", submitEndpoint)
    v2.POST( relativePath:"/streaming_read" readEndpoint)
}

⭐ Hertz 提供了参数路由、通配路由。路由的优先级:静态路由 > 命名路由 > 通配路由

// This handler will match: "/hertz/version", but will not match : "/hertz/" or "/hertz"
h.GET("/hertz/:version", func(ctx context.Context, c *app.RequestContext) {
    version := c.Param("version")
    c.String(consts.StatusOK, "Hello %s", version)
})

// However, this one will match "/hertz/v1/" and "/hertz/v2/send"
h.GET("/hertz/:version/*action", func(ctx context.Context, c *app.RequestContext) {
        version := c.Param("version")
        action := c.Param("action")
        message := version + " is " + action
        c.String(consts.StatusOK, message)
})

// For each matched request Context will hold the route definition
h.POST("/hertz/:version/*action", func(ctx context.Context, c *app.RequestContext) {
    // c.FullPath() == "/hertz/:version/*action" // true
    c.String(consts.StatusOK, c.FullPath())
})

⭐ hertz 提供了 Bind、Validate、BindAndValidate 函数进行参数的绑定和校验

type Args struct {
    Query 	string	 `query:"query"`
    QuerySlice	[]string `query:"g"`
    Path	string	 `path:"path"`
    Header 	string	 `header:"header"`
    Form	string	 `form:"form"`
    Json	string	 `json:"json"`
    Vd		int	 `query:"vd" vd:"$==|$==1"`
}

Func main() {
    h := server.Default(server.WithHostPorts( hp:"127.0.0.1:8080"))

    h.POST(relativePath: "v:path/bind", func(ctx context.Context,c *app.RequestContext) {
        var arg Argserr := c.BindAndValidate(&arg)
        if err != nil {
            panic(err)
        }
        fmt.PrintIn(arg)
    })

    h.Spin()
}

⭐ 更多功能请看官网文档,这里附上官方文档链接:Hertz | CloudWeGo

⭐ Hertz 拥有非常丰富的扩展生态,以下列举一部分常用扩展。

扩展网址
HTTP2 扩展github.com/hertz-contr…
opentelemetry 扩展github.com/hertz-contr…
国际化扩展github.com/hertz-contr…
反向代理扩展github.com/hertz-contr…
JWT 鉴权扩展github.com/hertz-contr…
Websocket 扩展github.com/hertz-contr…
丰富的示例代码与业务Demoqithub.com/cloudwego/h…

四、实战案例

笔记项目是一个使用 Hertz、Kitex、Gorm 搭建出来的具备一定业务逻辑的后端 API 项目。

项目地址kitex-examples/bizdemo/easy_note at main · cloudwego/kitex-examples · GitHub