目录

kitex protobuf 踩坑

最近想试一下kitex,但是官方文档里例子都是thrift的,没有protobuf的,因此记录一下配置踩坑的过程。

1. 安装protoc

官方Github下载安装最新的protoc

1
2
3
4
5
6
# 我这里是linux amd64版本
mkdir protobuf && cd protobuf
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protoc-3.19.4-linux-x86_64.zip
unzip ./protoc-3.19.4-linux-x86_64.zip
sudo cp ./bin/protoc /usr/local/bin
protoc --version

2. 安装golang插件

参见:谷歌官方文档

1
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest

3. 安装kitex

1
2
go install github.com/cloudwego/kitex/tool/cmd/kitex@latest
go install github.com/cloudwego/thriftgo@latest # optional

4. kitex we go!

编写一个proto文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
syntax = "proto3";
package tutorial;

import "google/protobuf/timestamp.proto";
option go_package = "github.com/protocolbuffers/protobuf/examples/go/tutorialpb";

message Person {
  string name = 1;
  int32 id = 2;  // Unique ID number for this person.
  string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    string number = 1;
    PhoneType type = 2;
  }

  repeated PhoneNumber phones = 4;

  int32 last_updated = 5;
}

// Our address book file is just one of these.
message AddressBook {
  repeated Person people = 1;
}

然后编译它

1
kitex -module example -type protobuf  ./test.proto

编译好的go在kitex_gen文件夹下

问题小计

  1. 编译protobuf出现报错:kitex_opt unknown flag

这个问题是没有安装protobuf的golang插件,安装一下就好了,同时注意protoc要是最新版本的