高性能 Go HTTP 框架Hertz

开发者 2024-10-8 04:36:08 97 0 来自 中国
什么是Hertz

Hertz[həːts]是一个用于 Go的高性能高可用性可扩展的HTTP 框架。它旨在为开发职员简化构建微服务。
为什么选择Hertz

Hertz的一大亮点是其极高的性能。您可以通过查看以下有关回显哀求的统计信息来对此有所相识。
另一点是它的易用性,我们将在下面讨论。
怎样利用Hertz

在这里,我们将编写一个简单的演示,资助您认识Hertz 框架的根本功能。
安装

在利用 Hertz 之前,您必要设置您的 Golang 开发情况并确保它 >= v1.15。
预备好 Golang 情况后,让我们创建小演示的项目文件夹,该文件夹通常位于$GOPATH/src.
mkdir userdemocd userdemo我剧烈保举利用 Hertz 自带的 Hertz 下令行工具hz。
hz是 Hertz 框架提供的用于天生代码的工具。现在,hz可以基于 thrift 和 protobuf 的 IDL 为 Hertz 项目天生脚手架。
您可以参考hz 工具包利用以获取更多信息。
go install github.com/cloudwego/hertz/cmd/hz@latesthz -v假如hz版本信息准确体现如下,那么我们已经完成安装并预备好根本的赫兹开发情况。
hz version v0.2.0界说 IDL

在本节中,我们将为我们的项目编写 IDL 文件userdemo。
hz可以利用 thrift IDL 或 protobuf IDL 天生代码,必要安装符合的编译器thriftgo或protoc。我们将以thrift为例。
让我们创建一个 idl 文件夹并界说user.thrift.
// idl/user.thriftnamespace go userstruct BaseResp {    1: i64 StatusCode;    2: string StatusMsg;    3: string data;}struct RegisterRequest {    1: string Username (api.body="username");    2: string Password (api.body="password");}struct RegisterResponse {    1: BaseResp BaseResp;}struct LoginRequest {    1: string Username (api.body="username");    2: string Password (api.body="password");}struct LoginResponse {    1: BaseResp BaseResp;}struct InfoRequest {    1: string Username (api.path="username");}struct InfoResponse {    1: BaseResp BaseResp;}service UserService {    RegisterResponse Register(1: RegisterRequest req) (api.post="/user/register");    LoginResponse Login(1: LoginRequest req) (api.post="/user/login");    InfoResponse Info(1: InfoRequest req) (api.get="/user/:username");}天生代码

在项目目录下实行以下下令。hz将为我们天生脚手架代码。
hz new -idl idl/user.thriftgo mod tidy假如修改user.thrift已天生的代码,还可以利用以下下令更新天生的代码。
hz update -idl idl/user.thrift这是天生的代码的结构及其user.thrift寄义。对它的简单明白将资助您入门。
.├── biz                               // business layer, which stores business logic related processes│   ├── handler                       // store handler file│   │   ├── user                      // user corresponds to the namespace defined in thrift IDL; for protobuf IDL, it corresponds to the last level of go_package│   │   │   |│   │   │   |__  user_service.go      // the handler file, the user will implement the method defined by the IDL service in this file, it will search for the existing handler in the current file when "update" command, and append a new handler to the end│   │   └── ping.go                   // ping handler carried by default, used to generate code for quick debugging, no other special meaning│   ├── model                         // IDL content-related generation code│   │   └── user                      // hello/example corresponds to the namespace defined in thrift IDL; for protobuf IDL, it corresponds to the last level of go_package │   │         └── user.go             // the product of thriftgo, It contains go code generated from the contents of hello.thrift definition. And it will be regenerated when use "update" command.│   └── router                        // generated code related to the definition of routes in IDL│       ├── user                      // hello/example corresponds to the namespace defined in thrift IDL; for protobuf IDL, it corresponds to the last level of go_package│       │     ├── hello.go            // the route registration code generated for the routes defined in hello.thrift by hz; this file will be regenerated each time the relevant IDL is updated│       │     └── middleware.go       // default middleware function, hz adds a middleware for each generated route group by default; when updating, it will look for the existing middleware in the current file and append new middleware at the end│       └── register.go               // call and register the routing definition in each IDL file; when a new IDL is added, the call of its routing registration will be automatically inserted during the update; do not edit├── go.mod                            // go.mod file, if not specified on the command line, defaults to a relative path to GOPATH as the module name├── idl                               // user defined IDL, location can be arbitrary│   └── user.thrift├── main.go                           // program entry├── router.go                         // user defined routing methods other than IDL└── router_gen.go                     // the route registration code generated by hz, for calling user-defined routes and routes generated by hz利用中心件

Hertz 支持许多常用的中心件。在这种情况下,我们将利用Session 中心件来资助我们盘算用户登录的次数。
如前所述,hz资助我们搭建了大量的脚手架代码。我们只必要关注业务代码。要利用 Session 中心件,您只必要简单地修改如下_loginMw方法middleware.go。
func _loginMw() []app.HandlerFunc {   // your code...   return []app.HandlerFunc{      // use session middleware      sessions.Sessions("usersession", cookie.NewStore([]byte("secret"))),   }}嗯,这不是很轻易吗?
改进处置惩罚步调

接下来我们将编写处置惩罚步调,特别是user_service.go文件。
Hertz 负责数据绑定的简单验证和一些杂务。我们必要做的就是处置惩罚哀求。

  • 我们先来看看Register方法。
PostForm我们可以通过该方法从 Post 哀求表单中吸取数据。您还可以利用StringorJSON方法向客户端返回字符串或 JSON 数据,并指定相应状态码。
// Register .// @router /user/register/ [POST]func Register(ctx context.Context, c *app.RequestContext) {   var err error   var req user.RegisterRequest   err = c.BindAndValidate(&req)   if err != nil {      c.String(400, err.Error())      return   }   resp := new(user.RegisterResponse)   username := c.PostForm("username")   password := c.PostForm("password")   if dal.CheckUsername(username) {      dal.CreateUser(username, password)      resp.BaseResp = &user.BaseResp{         StatusCode: 0,         StatusMsg:  "register success",      }      c.JSON(200, resp.BaseResp)      return   }   resp.BaseResp = &user.BaseResp{      StatusCode: 1,      StatusMsg:  "register failed",   }   c.JSON(400, resp.BaseResp)} 2.png

  • 接下来,让我们来看看Login方法。
这些方法中的大多数都雷同于Register,除了我们利用 Session 中心件,它只是设置来盘算差异用户登录的次数。
我们可以利用sessions.Default方法来检索会话对象并利用Get和Set方法来编辑存储在会话中的值。
// Login .// @router /user/login/ [POST]func Login(ctx context.Context, c *app.RequestContext) {   var err error   var req user.LoginRequest   err = c.BindAndValidate(&req)   if err != nil {      c.String(400, err.Error())      return   }   resp := new(user.LoginResponse)   username := c.PostForm("username")   password := c.PostForm("password")   if dal.CheckPassword(username, password) {      session := sessions.Default(c)      var count int      cnt := session.Get(username)      if cnt == nil {         count = 0         dal.SetFrequency(username, count)      } else {         count = cnt.(int)         count++         dal.SetFrequency(username, count)      }      session.Set(username, count)      _ = session.Save()      resp.BaseResp = &user.BaseResp{         StatusCode: 0,         StatusMsg:  "login success",      }      c.JSON(200, resp.BaseResp)      return   }   resp.BaseResp = &user.BaseResp{      StatusCode: 1,      StatusMsg:  "login failed",   }   c.JSON(400, resp.BaseResp)}

  • 末了,我们来看看Info方法。
在此方法中,我们利用 Hertz 的参数化门路功能,它允许我们利用定名参数(比方 )指定门路:name,以便该参数与路径段匹配。
我们设置:username参数路由,利用Param方法获取哀求路径中的值。
// Info .// @router /user/:username [GET]func Info(ctx context.Context, c *app.RequestContext) {   var err error   var req user.InfoRequest   err = c.BindAndValidate(&req)   if err != nil {      c.String(400, err.Error())      return   }   resp := new(user.InfoResponse)   username := c.Param("username")   frequency := dal.GetFrequency(username)   resp.BaseResp = &user.BaseResp{      StatusCode: 0,      StatusMsg:  "info success",      Data:       strconv.Itoa(frequency),   }   c.JSON(200, resp)}其他功能

假如您查看router/user/user.go天生者hz,您会看到 Hertz 主动利用Route Group功能,该功能可以资助您对复杂的门路进行排序和构造。
当我们利用server.Defaultin 时main.go,Hertz 也默以为我们注册了规复中心件,它优雅地处置惩罚了 panic。
既然我们已经介绍了一些紧张的 Hertz 方法以及怎样利用它们,我渴望这将资助您快速开始利用 Hertz。
概括

此演示仅涵盖 Hertz 功能的一小部分。您可以查看cloudwego/hertz相识更多信息。我确信文档可以回复您的所有标题。
这个演示的代码在这里。
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 14:24, Processed in 0.181284 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表