Hertz 学习笔记(2)

275 阅读3分钟

文档和各种资料内容好多啊,感觉要看花眼了。。。果然之前好像看哪个地方分享的说有个大佬在学校里只关心自己代码的性能,然后加入团队一段时间以后更关注自己代码的易用性,好多人没耐心看文档,看文档的不一定看得懂,能把代码跑起来就算会用了。完了,我现在就是这种状态。。。也许从具体代码入手会好点?这里我参考了 hertz-examples 里面服务器主题的各种例子,按照里面的罗列顺序来了解这个框架的方方面面。这个顺序如下(看不懂的我就跳过了,我会更新这部分内容,让之后的博客和这里有链接):

  • hello world 示例(本篇)
  • hertz server 配置示例(本篇)
  • 使用 http 1,TLS 等协议的示例
  • 使用 basicauth 中间件的示例
  • 使用 CORS 中间的示例
  • 使用 csrf 中间件的示例
  • 自定义中间件的示例
  • 使用 pprof 中间件的示例
  • 使用 RequestID 中间件的示例
  • 使用 Gzip 中间件的示例
  • 使用 Loadbalance 中间件的示例
  • 绑定参数和验证参数的示例
  • 获取 query,form,cookie 等类型参数的示例
  • 文件上传下载以及搭建静态文件服务的示例
  • 渲染 json,html,protobuf 的示例
  • 重定向到内部或外部 URI 的示例
  • hertz server 的流读写示例
  • 优雅退出 hertz server 的示例
  • 使用 hertz 提供的没有网络传输的接口编写单元测试的示例
  • 使用 Jaeger 进行链接追踪的示例
  • 使用 Prometheus 进行服务监控的示例
  • 使用 hertz 启动多端口服务的示例
  • 使用 adaptor 集成基于 http.Handller 接口开发的工具,包含使用 jade 作为模板引擎的示例
  • sentinel-golang 结合 hertz 使用的示例
  • 在 hertz server 中使用反向代理的示例
  • 使用 hlog 以及其日志扩展的示例

今天先把头两个简单的例子搞定。首先肯定要把项目拉下来才能用,这一点不在这里写了。

hello world 示例

例子具体代码如下,只有一个 main()

/*
 * Copyright 2022 CloudWeGo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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() {
	// server.Default() creates a Hertz with recovery middleware.
	// If you need a pure hertz, you can use server.New()
	h := server.Default()

	h.GET("/hello", func(ctx context.Context, c *app.RequestContext) {
		c.String(consts.StatusOK, "Hello hertz!")
	})

	h.Spin()
}

直接进入项目目录开第一个终端:

$ go run hello/main.go

第二个终端:

$ curl --location --request GET '127.0.0.1:8888/hello'

这个例子确实没配置,走的默认配置。

hertz server 配置示例

这个例子代码也是一个 main(),说明直接甩了一篇文档:

/*
 * Copyright 2022 CloudWeGo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"context"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/network/standard"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
)

func main() {
	// The default listening port is 8888.
	// You can modify it with server.WithHostPorts().
	h := server.Default(
		server.WithHostPorts("127.0.0.1:8080"),
		server.WithMaxRequestBodySize(20<<20),
		server.WithTransport(standard.NewTransporter),
	)

	h.GET("/hello", func(ctx context.Context, c *app.RequestContext) {
		c.String(consts.StatusOK, "Hello hertz!")
	})

	h.Spin()
}

配置文档

文档里既有 server 的配置也有 client 的配置,目前我只关心这段代码里用到的配置:

  • server.WithHostPorts("127.0.0.1:8080"):指定监听的地址和端口,输入要求字符串格式
  • server.WithMaxRequestBodySize(20<<20):配置最大请求体的大小,int 类型
  • server.WithTransport(standard.NewTransporter):更换底层 transport,这里这么写显然是没用默认值,默认值是 netpoll.NewTransporter

这样看这个配置也不是很复杂,希望之后也这么顺利。