Go语言与Protobuf

1. Protobuf 介绍

Protobuf 是 Protocol Buffers 的简称 ,是Google开源的的一种数据描述语言,其初始定位类似于 XMLjson 等数据描述语言,Protobuf 最常见的使用场景是用于RPC系统中,这是因为protobuf 是一种轻便高效的结构化数据存储格式,可以序列化,比较适合做数据存储或者RPC数据交互格式

2. Protobuf 特点

优点 :

  1. protobuf 以高效二进制存储,占用空间比 XML 小 3~10倍,解析速度快 20 ~ 100倍
  2. 代码生成机制
  3. 多编程语言支持
  4. 兼容性比较好

缺点 :

  1. 可读性差,调试相对困难

想了解 protobuf 具体详情 参考如下 :

https://developers.google.com/protocol-buffers

3. Protobuf 环境配置

3.1 安装protobuf 基础工具

  1. 根据自己开发系统下载protobuf 编译器

    下载地址 https://github.com/protocolbuffers/protobuf/releases

  2. 配置环境变量

  3. 验证是否安装成功

    # 查看protobuf编译器版本
    $ protoc --version
    libprotoc 3.11.0
    # 查看把帮助文档
    $ protoc --help
    .
    .
    .
     --cpp_out=OUT_DIR           Generate C++ header and source.
      --csharp_out=OUT_DIR        Generate C# source file.
      --java_out=OUT_DIR          Generate Java source file.
      --js_out=OUT_DIR            Generate JavaScript source.
      --objc_out=OUT_DIR          Generate Objective C header and source.
      --php_out=OUT_DIR           Generate PHP source file.
      --python_out=OUT_DIR        Generate Python source file.
      --ruby_out=OUT_DIR          Generate Ruby source file.
      @<filename>                 Read options and filenames from file. If a
                                  relative file path is specified, the file
                                  will be searched in the working directory.
                                  The --proto_path option will not affect how
                                  this argument file is searched. Content of
                                  the file will be expanded in the position of
                                  @<filename> as in the argument list. Note
                                  that shell expansion is not applied to the
                                  content of the file (i.e., you cannot use
                                  quotes, wildcards, escapes, commands, etc.).
                                  Each line corresponds to a single argument,
                                  even if it contains spaces.
    
    

3.2 安装编程语言插件

我们选择Go语言的代码生成插件

在命令行下执行

go get github.com/golang/protobuf/protoc-gen-go 

该命令执行完成之后,我们会看到这么一个目录

$GOPATH/src/github.com/golang/protobuf

安装成功之后会在 $GOPATH/bin 目录下生成 protoc-gen-go的可执行文件

安装好Go语言的代码生成插件之后,我们测试一下,步骤如下:

  1. 编写一个以.proto 为后缀的protobuf文件

    |

    |__example

    |____ demo2.proto

    文件是 example/demo2.proto

    syntax = "proto3"; // 使用 protobuf3的语法
    package example;
    
    message Person {
        string name = 1;
        int32 age = 2;
        enum Gender {
            MALE = 0;
            FEMALE = 1;
            UNKNOWN = 2;
        }
        message Other {
            string addr = 1;
            string hobby = 2;
            Gender g = 3;
        }
        Other info = 3;
    
    }
    
    
  1. 使用 protoc-gen-go 生成 go语言代码

    tips : 如果执行下面命令不生效,可以尝试将 protoc-gen-go的可执行文件复制一份到 $GOROOT/bin/ 目录下

     protoc --go_out=. example/demo2.proto
    

    对上述命令简单解释一下:

    --go_out 参数是告诉 protoc 编译器去加载对应的 protoc-gen-go工具,通过这个工具生成对应的Golang 代码

    =. 表示生成代码的存放在当前目录下,当然也可指定到其他文件目录下

    example/demo2.protoc 是protobuf文件的路径

    在实际使用过程中我们灵活使用参数即可

    我们将看到一个文件名为 demo2.pb.go文件生成

    |

    |__example

    |____ demo2.proto

    |____ demo2.pb.go

    这个新增的文件就是自动生成的 go 语言代码 ,我们看一下 demo2.pb.go的部分代码

    1. 开发过程中我们定义好 .proto 文件之后,这段代码是插件自动生成,不同的编程语言语法上会有区别
    2. 生成的代码通常不需要我们关注
    3. 业务层中只按需使用自动生成代码文件中提供好的 工具
    // Code generated by protoc-gen-go. DO NOT EDIT.
    // source: demo2.proto
    
    package example
    
    import (
     fmt "fmt"
     proto "github.com/golang/protobuf/proto"
     math "math"
    )
    
    // Reference imports to suppress errors if they are not otherwise used.
    var _ = proto.Marshal
    var _ = fmt.Errorf
    var _ = math.Inf
    
    // This is a compile-time assertion to ensure that this generated file
    // is compatible with the proto package it is being compiled against.
    // A compilation error at this line likely means your copy of the
    // proto package needs to be updated.
    const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
    
    type Person_Gender int32
    
    const (
     Person_MALE    Person_Gender = 0
     Person_FEMALE  Person_Gender = 1
     Person_UNKNOWN Person_Gender = 2
    )
    
    var Person_Gender_name = map[int32]string{
     0: "MALE",
     1: "FEMALE",
     2: "UNKNOWN",
    }
    
    var Person_Gender_value = map[string]int32{
     "MALE":    0,
     "FEMALE":  1,
     "UNKNOWN": 2,
    }
    
    func (x Person_Gender) String() string {
     return proto.EnumName(Person_Gender_name, int32(x))
    }
    
    func (Person_Gender) EnumDescriptor() ([]byte, []int) {
     return fileDescriptor_80e54830e2bc2dba, []int{0, 0}
    }
    
    type Person struct {
     Name                 string        `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
     Age                  int32         `protobuf:"varint,2,opt,name=age,proto3" json:"age,omitempty"`
     Info                 *Person_Other `protobuf:"bytes,3,opt,name=info,proto3" json:"info,omitempty"`
     XXX_NoUnkeyedLiteral struct{}      `json:"-"`
     XXX_unrecognized     []byte        `json:"-"`
     XXX_sizecache        int32         `json:"-"`
    }
    
    func (m *Person) Reset()         { *m = Person{} }
    func (m *Person) String() string { return proto.CompactTextString(m) }
    func (*Person) ProtoMessage()    {}
    func (*Person) Descriptor() ([]byte, []int) {
     return fileDescriptor_80e54830e2bc2dba, []int{0}
    }
    
    func (m *Person) XXX_Unmarshal(b []byte) error {
     return xxx_messageInfo_Person.Unmarshal(m, b)
    }
    func (m *Person) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
     return xxx_messageInfo_Person.Marshal(b, m, deterministic)
    }
    func (m *Person) XXX_Merge(src proto.Message) {
     xxx_messageInfo_Person.Merge(m, src)
    }
    func (m *Person) XXX_Size() int {
     return xxx_messageInfo_Person.Size(m)
    }
    func (m *Person) XXX_DiscardUnknown() {
     xxx_messageInfo_Person.DiscardUnknown(m)
    }
    
    var xxx_messageInfo_Person proto.InternalMessageInfo
    
    func (m *Person) GetName() string {
     if m != nil {
         return m.Name
     }
     return ""
    }
    
    func (m *Person) GetAge() int32 {
     if m != nil {
         return m.Age
     }
     return 0
    }
    
    func (m *Person) GetInfo() *Person_Other {
     if m != nil {
         return m.Info
     }
     return nil
    }
    
    type Person_Other struct {
     Addr                 string        `protobuf:"bytes,1,opt,name=addr,proto3" json:"addr,omitempty"`
     Hobby                string        `protobuf:"bytes,2,opt,name=hobby,proto3" json:"hobby,omitempty"`
     G                    Person_Gender `protobuf:"varint,3,opt,name=g,proto3,enum=example.Person_Gender" json:"g,omitempty"`
     XXX_NoUnkeyedLiteral struct{}      `json:"-"`
     XXX_unrecognized     []byte        `json:"-"`
     XXX_sizecache        int32         `json:"-"`
    }
    

3.3 Protobuf 基础使用

|

|__example

|____ demo2.proto

|____ demo2.pb.go

|__ main.go

main.go

package main

import (
    "GoNote/chapter10/demo9/example"
    "fmt"
    "github.com/golang/protobuf/proto"
    "log"
)

func main() {
    // 相当于示例化一个Person的对象
    // 这个Person的结构体就是我们通过代码自动生成的
    d1 := example.Person{
        Name: "tom",
        Age:  99,
        Info: &example.Person_Other{
            Addr:  "beijing",
            Hobby: "code",
            G:     example.Person_MALE,
        },
    }
    // 进行序列化操作
    d1Encode, err := proto.Marshal(&d1)
    if err != nil {
        log.Fatal(err.Error())
    }
    fmt.Println(d1Encode)
    // 进行反序列化操作
    d1Decode := example.Person{}
    err = proto.Unmarshal(d1Encode, &d1Decode)
    if err != nil {
        log.Fatal(err.Error())
    }
    fmt.Println(d1Decode.GetName())
    fmt.Println(d1Decode.GetAge())
    fmt.Println(d1Decode.GetInfo().GetG())
}

go run main.go

[10 3 116 111 109 16 99 26 15 10 7 98 101 105 106 105 110 103 18 4 99 111 100 101]
tom
99
MALE

Protobuf 基本使用强烈推荐看看官方文档 :

https://developers.google.com/protocol-buffers/docs/gotutorial

4. Protobuf 协议语法

我们注意到编写的 .proto 文件是按特定的语法格式编写的

  1. Protobuf的语法相对很简单
  2. Protobuf的语目前 proto2proto3 两个版本
    1. proto2 https://developers.google.com/protocol-buffers/docs/proto
    2. proto3 https://developers.google.com/protocol-buffers/docs/proto3
  3. 如果项目无历史负担,强烈推荐使用 proto3 的语法编写 .proto 文件

5. Protobuf与RPC

我们将Protobuf 和RPC 结合起来做一个简单的Demo

步骤如下 :

  1. 编写.proto文件
  2. 自动生成代码 .pb.go 的代码文件
  3. 编写 RPC的服务端
  4. 编写 PRC的客户端
  5. 运行测试

目录结构如下

|
|___example
|______demo3.proto
|______demo3.pb.go
|___main
|______demo3_server.go
|______demo3_client.go

编写 demo3.proto 文件

syntax = "proto3"; // 使用 protobuf3的语法
package example;

message Demo3Request {
    int64 id = 1;
}
message Demo3Response {
    string name = 1;
    int32 age = 2;
    enum Gender {
        MALE = 0;
        FEMALE = 1;
        UNKNOWN = 2;
    }
    message Other {
        string addr = 1;
        string hobby = 2;
        Gender g = 3;
    }
    Other info = 3;
}

生成 .pb.go文件

protoc --go_out=. demo3.proto

编写 rpc服务端文件 demo3_server.go

package main

import (
    "GoNote/chapter10/demo9/example"
    "github.com/pkg/errors"
    "log"
    "net/http"
    "net/rpc"
)

type Demo3Service struct {
}

func (d *Demo3Service) GetUser(request example.Demo3Request, response *example.Demo3Response) error {
    // 模拟数据
    // 数据获取逻辑自行设计
    datas := map[int64]example.Demo3Response{
        1: {Name: "AAA", Age: 999, Info: &example.Demo3Response_Other{Addr: "beijing", Hobby: "sport", G: example.Demo3Response_MALE}},
        2: {Name: "BBB", Age: 888, Info: &example.Demo3Response_Other{Addr: "上海", Hobby: "sport", G: example.Demo3Response_FEMALE}},
        3: {Name: "CCC", Age: 777, Info: &example.Demo3Response_Other{Addr: "wuhan", Hobby: "sport", G: example.Demo3Response_UNKNOWN}},
        4: {Name: "DDD", Age: 666, Info: &example.Demo3Response_Other{Addr: "重庆", Hobby: "sport", G: example.Demo3Response_MALE}},
        5: {Name: "EEE", Age: 555, Info: &example.Demo3Response_Other{Addr: "", Hobby: "sport", G: example.Demo3Response_FEMALE}},
    }
    // 模拟业务处理逻辑
    if value, ok := datas[request.GetId()]; ok {
        *response = value
    } else {
        return errors.New("not found")
    }
    return nil
}
func main() {
    rpc.Register(new(Demo3Service))
    rpc.HandleHTTP()
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Println(err.Error())
    }
}

编写 rpc 客户端文件 demo3_client.go

package main

import (
    "GoNote/chapter10/demo9/example"
    "fmt"
    "log"
    "net/rpc"
)

func main() {
    client, err := rpc.DialHTTP("tcp", ":8080")
    if err != nil {
        log.Fatal(err.Error())
    }
    request := example.Demo3Request{Id: 1}
    var response example.Demo3Response
    err = client.Call("Demo3Service.GetUser", request, &response)
    if err != nil {
        log.Fatal(err.Error())
    }
    fmt.Println(response.GetName())
    fmt.Println(response.GetInfo().GetAddr())
}

运行 demo3_server.godemo3_client.go

AAA
beijing
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容

  • 我是教师,对评职称有点抵触。 一是评职称要有英语职称考试成绩,英语?我个物理老师有个毛用啊? 二是评职要写论文或发...
    孔雀东南飞飞阅读 502评论 8 18
  • 第一章:建立侦探社 作者:美死了少女 清晨,程小浩(程胖子)来到了特桂中学的教导处办理建立侦探社的资料,程小浩要...
    美死了阅读 442评论 4 8
  • 工欲善其事必先利其器 怎么也得先把Miniconda 装在VMware吧! 打开Miniconda官网 按需下载M...
    倔强的胖子阅读 969评论 0 0
  • 好友们,你们好,这是我第三次生病在简书上写了,我非常的难受,希望大家以后多运动,就不会像我这样总生病了,接...
    望舒北辰阅读 401评论 1 0