这是我参与「第五届青训营 」伴学笔记创作活动的第 3 天
微服务
微服务架构与单体架构
单体架构中所有业务逻辑聚合在一起,业务开展在一个基础性的数据库之下,如果应用程序的一个进程遇到需求峰值,则必须扩展整个架构。随着代码库的增长,添加或改进整体式应用程序的功能变得更加复杂;
微服务架构将应用程序构建为独立的组件,并将每个应用程序进程作为一项服务运行。这些服务使用轻量级 API 通过明确定义的接口进行通信。这些服务是围绕业务功能构建的,每项服务执行一项功能。由于它们是独立运行的,因此可以针对各项服务进行更新、部署和扩展,以满足对应用程序特定功能的需求。根据服务对代码进行解耦,当需求改变时,可以只改变对应的服务,减少修改代码带来的风险
hertz使用
路由注册
package main
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)
func main(){
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))
h.StaticFS("/", &app.FS{Root: "./", GenerateIndexPages: true})
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")
})
h.Any("/ping_any", func(ctx context.Context, c *app.RequestContext) {
c.String(consts.StatusOK, "any")
})
h.Handle("LOAD","/load", func(ctx context.Context, c *app.RequestContext) {
c.String(consts.StatusOK, "load")
})
h.Spin()
}
路由组
package main
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)
func main(){
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))
v1 := h.Group("/v1")
// v1 := h.Group("/v1", basic_auth.BasicAuth(map[string]string{"test": "test"})) //在路由组使用中间件并注册
v1.Use(basic_auth.BasicAuth(map[string]string{"test": "test"})) // 注册完路由组后注册中间件
v1.GET("/get", func(ctx context.Context, c *app.RequestContext) {
c.String(consts.StatusOK, "get")
})
v1.POST("/post", func(ctx context.Context, c *app.RequestContext) {
c.String(consts.StatusOK, "post")
})
v2 := h.Group("/v2")
v2.PUT("/put", func(ctx context.Context, c *app.RequestContext) {
c.String(consts.StatusOK, "put")
})
v2.DELETE("/delete", func(ctx context.Context, c *app.RequestContext) {
c.String(consts.StatusOK, "delete")
})
h.Spin()
}
路由类型
Hertz 支持丰富的路由类型用于实现复杂的功能,包括静态路由、参数路由、通配路由。
路由的优先级:静态路由 > 命名路由 > 通配路由
静态路由:/api/getinfo
命名路由:/user/:name
获取路由命名的参数
h.GET("/user/:name", func(ctx context.Context, c *app.RequestContext) {
version := c.Param("name")
c.String(consts.StatusOK, "Hello %s", version)
})
通配路由:/src/*path
通配参数会匹配所有内容,
| /src/ | 匹配 |
|---|---|
| /src/somefile.go | 匹配 |
| /src/subdir/somefile.go | 匹配 |
NoRoute 与 NoMethod 使用
Hertz 提供了 NoRoute 与 NoMethod 方法用于全局处理 HTTP 404 与 405 请求。 当使用 NoMethod 时需要与 WithHandleMethodNotAllowed 配合使用。
h.NoRoute(func(c context.Context, ctx *app.RequestContext) {
ctx.String(consts.StatusOK, "no route")
})
// set NoMethod handler
h.NoMethod(func(c context.Context, ctx *app.RequestContext) {
ctx.String(consts.StatusOK, "no method")
})