KiteX 是字节跳动框架组研发的下一代高性能、强可扩展性的 Go RPC 框架。目前已经在 Github 开源。作为字节跳动内部原有的 RPC 框架 Kite 的升级版,虽然开源时间不是很长,但其实从性能和扩展性上来说都已经在字节上万微服务的场景下做了充分的验证。
近期花了一些时间简单看了看源码,被整体的框架设计和扩展性深深吸引。希望能够单独开一个系列,带大家一起熟悉Kitex的使用,通过源码了解其提供的能力,学习整体的设计。
Github 仓库:github.com/cloudwego/k…
官方 example:github.com/cloudwego/k…
作为入门系列第一篇,今天我们先来看看如何上手。基于 kitex 0.2.1 版本。
准备工作
- 先准备好本地的 Golang 环境,设置好 GOPATH,建议使用较新的 Go 版本。
- 安装 kitex 和 thriftgo 两个命令行工具
go install github.com/cloudwego/kitex/tool/cmd/kitex@latest
go install github.com/cloudwego/thriftgo@latest
复制代码
这一步完成后,可以打开命令行运行 kitex --version
和 thriftgo --version
进行验证。
Server
创建本地仓库
我在Github上创建了公开的仓库,和这个系列文章同步更新:github.com/ag9920/kstu… ,克隆到本地后开始我们今天的开发。
创建IDL文件
Kitex 典型的使用场景就是基于 thrift 定义的 IDL 来定义服务接口,实现客户端和服务端的通信。
这里我们新建一个demo的服务定义: kstudy.thrift
namespace go api
struct Request {
1: string message1
2: string message2
}
struct Response {
1: string message
}
service KStudy {
Response Concat(1: Request req)
}
复制代码
可以看到,我们定义了一个KStudy
服务,包含一个Concat
接口,语义是将 Request 中的 message1 和 message2 两个字符串拼接起来,通过 Response 返回。
使用命令行工具生成代码
kitex 工具提供了很多选项扩展,可以通过 --help 来了解。
kitex --help
Version v0.2.0
Usage: kitex [flags] IDL
flags:
-I value
Add an IDL search path for includes.
-combine-service
Combine services in root thrift file.
-copy-idl
Copy each IDL file to the output path.
-invoker
Generate invoker side codes when service name is specified.
-module string
Specify the Go module name to generate go.mod.
-no-fast-api
Generate codes without injecting fast method.
-protobuf value
Specify arguments for the protobuf compiler.
-service string
Specify the service name to generate server side codes.
-thrift value
Specify arguments for the thrift compiler.
-thrift-plugin value
Specify thrift plugin arguments for the thrift compiler.
-type string
Specify the type of IDL: 'thrift' or 'protobuf'. (default "thrift")
-use string
Specify the kitex_gen package to import when generate server side codes.
-v
-verbose
Turn on verbose mode.
-version
Show the v