gRPC的Golang簡單應用

ice_moss發表於2022-03-07

gRPC的Golang簡單應用

首先我們需要知道gRPC是一種用來通訊的框架,傳輸資料走的是二進位制流,資料結構使用protoBuf定義的

我們直接來看一個例子吧!

假如我們需要一個行程的資料:


statar:  //起點
statar_pos:  //起點位置
path_locations:  //中途位置
end:   //終點
end_pos //終點位置
duration_sec: //時間
fee_cent:  //費用
status:  //行程狀態

首先我們需要一個x.proto檔案如下:


syntax = "proto3";  //語法使用proto3
package coolcar;  //包名
option go_package = "coolcar/proto/gen/go/;trippb";  //路徑;包名

//使用message來定義結構體
message Location{
    double latitude = 1;
    double longitude = 2;
}

enum TripStatus{      //列舉型別
    TS_NOT_SPECIFID = 0;
    NOT_STARTED = 1;
    IN_PROGRESS = 2;
    FINISHED = 3;
    PAID = 4;
}

message Trip{
    string statar = 1;  //數字表示標記碼,不是賦值
    Location statar_pos = 5;
    repeated Location path_locations = 7;
    string end = 2;
    Location end_pos = 6;
    int64 duration_sec = 3;
    int64 fee_cent = 4;
    TripStatus status = 8;

}

輸入命令:

protoc -I=. --go_out=paths=source_relative:gen/go trip.proto

編譯出下面程式碼:

編譯後主要部分是:

  1. 行程結構體序列化及反序列化方法
  2. 位置結構體序列化及反序列化方法
  3. 列舉型別的等資料和方法
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
//     protoc-gen-go v1.26.0
//     protoc        v3.13.0
// source: trip.proto

package trippb

import (
    protoreflect "google.golang.org/protobuf/reflect/protoreflect"
    protoimpl "google.golang.org/protobuf/runtime/protoimpl"
    reflect "reflect"
    sync "sync"
)

const (
    // Verify that this generated code is sufficiently up-to-date.
    _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
    // Verify that runtime/protoimpl is sufficiently up-to-date.
    _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)

type TripStatus int32

const (
    TripStatus_TS_NOT_SPECIFID TripStatus = 0
    TripStatus_NOT_STARTED     TripStatus = 1
    TripStatus_IN_PROGRESS     TripStatus = 2
    TripStatus_FINISHED        TripStatus = 3
    TripStatus_PAID            TripStatus = 4
)

// Enum value maps for TripStatus.
var (
    TripStatus_name = map[int32]string{
        0: "TS_NOT_SPECIFID",
        1: "NOT_STARTED",
        2: "IN_PROGRESS",
        3: "FINISHED",
        4: "PAID",
    }
    TripStatus_value = map[string]int32{
        "TS_NOT_SPECIFID": 0,
        "NOT_STARTED":     1,
        "IN_PROGRESS":     2,
        "FINISHED":        3,
        "PAID":            4,
    }
)

func (x TripStatus) Enum() *TripStatus {
    p := new(TripStatus)
    *p = x
    return p
}

func (x TripStatus) String() string {
    return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
}

func (TripStatus) Descriptor() protoreflect.EnumDescriptor {
    return file_trip_proto_enumTypes[0].Descriptor()
}

func (TripStatus) Type() protoreflect.EnumType {
    return &file_trip_proto_enumTypes[0]
}

func (x TripStatus) Number() protoreflect.EnumNumber {
    return protoreflect.EnumNumber(x)
}

// Deprecated: Use TripStatus.Descriptor instead.
func (TripStatus) EnumDescriptor() ([]byte, []int) {
    return file_trip_proto_rawDescGZIP(), []int{0}
}

type Location struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    Latitude  float64 `protobuf:"fixed64,1,opt,name=latitude,proto3" json:"latitude,omitempty"`
    Longitude float64 `protobuf:"fixed64,2,opt,name=longitude,proto3" json:"longitude,omitempty"`
}

func (x *Location) Reset() {
    *x = Location{}
    if protoimpl.UnsafeEnabled {
        mi := &file_trip_proto_msgTypes[0]
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        ms.StoreMessageInfo(mi)
    }
}

func (x *Location) String() string {
    return protoimpl.X.MessageStringOf(x)
}

func (*Location) ProtoMessage() {}

func (x *Location) ProtoReflect() protoreflect.Message {
    mi := &file_trip_proto_msgTypes[0]
    if protoimpl.UnsafeEnabled && x != nil {
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        if ms.LoadMessageInfo() == nil {
            ms.StoreMessageInfo(mi)
        }
        return ms
    }
    return mi.MessageOf(x)
}

// Deprecated: Use Location.ProtoReflect.Descriptor instead.
func (*Location) Descriptor() ([]byte, []int) {
    return file_trip_proto_rawDescGZIP(), []int{0}
}

func (x *Location) GetLatitude() float64 {
    if x != nil {
        return x.Latitude
    }
    return 0
}

func (x *Location) GetLongitude() float64 {
    if x != nil {
        return x.Longitude
    }
    return 0
}

type Trip struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    Statar        string      `protobuf:"bytes,1,opt,name=statar,proto3" json:"statar,omitempty"` //數字表示標記碼,不是賦值
    StatarPos     *Location   `protobuf:"bytes,5,opt,name=statar_pos,json=statarPos,proto3" json:"statar_pos,omitempty"`
    PathLocations []*Location `protobuf:"bytes,7,rep,name=path_locations,json=pathLocations,proto3" json:"path_locations,omitempty"`
    End           string      `protobuf:"bytes,2,opt,name=end,proto3" json:"end,omitempty"`
    EndPos        *Location   `protobuf:"bytes,6,opt,name=end_pos,json=endPos,proto3" json:"end_pos,omitempty"`
    DurationSec   int64       `protobuf:"varint,3,opt,name=duration_sec,json=durationSec,proto3" json:"duration_sec,omitempty"`
    FeeCent       int64       `protobuf:"varint,4,opt,name=fee_cent,json=feeCent,proto3" json:"fee_cent,omitempty"`
    Status        TripStatus  `protobuf:"varint,8,opt,name=status,proto3,enum=coolcar.TripStatus" json:"status,omitempty"`
}

func (x *Trip) Reset() {
    *x = Trip{}
    if protoimpl.UnsafeEnabled {
        mi := &file_trip_proto_msgTypes[1]
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        ms.StoreMessageInfo(mi)
    }
}

func (x *Trip) String() string {
    return protoimpl.X.MessageStringOf(x)
}

func (*Trip) ProtoMessage() {}

func (x *Trip) ProtoReflect() protoreflect.Message {
    mi := &file_trip_proto_msgTypes[1]
    if protoimpl.UnsafeEnabled && x != nil {
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        if ms.LoadMessageInfo() == nil {
            ms.StoreMessageInfo(mi)
        }
        return ms
    }
    return mi.MessageOf(x)
}

// Deprecated: Use Trip.ProtoReflect.Descriptor instead.
func (*Trip) Descriptor() ([]byte, []int) {
    return file_trip_proto_rawDescGZIP(), []int{1}
}

func (x *Trip) GetStatar() string {
    if x != nil {
        return x.Statar
    }
    return ""
}

func (x *Trip) GetStatarPos() *Location {
    if x != nil {
        return x.StatarPos
    }
    return nil
}

func (x *Trip) GetPathLocations() []*Location {
    if x != nil {
        return x.PathLocations
    }
    return nil
}

func (x *Trip) GetEnd() string {
    if x != nil {
        return x.End
    }
    return ""
}

func (x *Trip) GetEndPos() *Location {
    if x != nil {
        return x.EndPos
    }
    return nil
}

func (x *Trip) GetDurationSec() int64 {
    if x != nil {
        return x.DurationSec
    }
    return 0
}

func (x *Trip) GetFeeCent() int64 {
    if x != nil {
        return x.FeeCent
    }
    return 0
}

func (x *Trip) GetStatus() TripStatus {
    if x != nil {
        return x.Status
    }
    return TripStatus_TS_NOT_SPECIFID
}

var File_trip_proto protoreflect.FileDescriptor

var file_trip_proto_rawDesc = []byte{
    0x0a, 0x0a, 0x74, 0x72, 0x69, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x63, 0x6f,
    0x6f, 0x6c, 0x63, 0x61, 0x72, 0x22, 0x44, 0x0a, 0x08, 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f,
    0x6e, 0x12, 0x1a, 0x0a, 0x08, 0x6c, 0x61, 0x74, 0x69, 0x74, 0x75, 0x64, 0x65, 0x18, 0x01, 0x20,
    0x01, 0x28, 0x01, 0x52, 0x08, 0x6c, 0x61, 0x74, 0x69, 0x74, 0x75, 0x64, 0x65, 0x12, 0x1c, 0x0a,
    0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01,
    0x52, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65, 0x22, 0xb3, 0x02, 0x0a, 0x04,
    0x54, 0x72, 0x69, 0x70, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x61, 0x72, 0x18, 0x01,
    0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x61, 0x72, 0x12, 0x30, 0x0a, 0x0a,
    0x73, 0x74, 0x61, 0x74, 0x61, 0x72, 0x5f, 0x70, 0x6f, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b,
    0x32, 0x11, 0x2e, 0x63, 0x6f, 0x6f, 0x6c, 0x63, 0x61, 0x72, 0x2e, 0x4c, 0x6f, 0x63, 0x61, 0x74,
    0x69, 0x6f, 0x6e, 0x52, 0x09, 0x73, 0x74, 0x61, 0x74, 0x61, 0x72, 0x50, 0x6f, 0x73, 0x12, 0x38,
    0x0a, 0x0e, 0x70, 0x61, 0x74, 0x68, 0x5f, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73,
    0x18, 0x07, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x63, 0x6f, 0x6f, 0x6c, 0x63, 0x61, 0x72,
    0x2e, 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0d, 0x70, 0x61, 0x74, 0x68, 0x4c,
    0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x10, 0x0a, 0x03, 0x65, 0x6e, 0x64, 0x18,
    0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x65, 0x6e, 0x64, 0x12, 0x2a, 0x0a, 0x07, 0x65, 0x6e,
    0x64, 0x5f, 0x70, 0x6f, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x63, 0x6f,
    0x6f, 0x6c, 0x63, 0x61, 0x72, 0x2e, 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x06,
    0x65, 0x6e, 0x64, 0x50, 0x6f, 0x73, 0x12, 0x21, 0x0a, 0x0c, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69,
    0x6f, 0x6e, 0x5f, 0x73, 0x65, 0x63, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0b, 0x64, 0x75,
    0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x53, 0x65, 0x63, 0x12, 0x19, 0x0a, 0x08, 0x66, 0x65, 0x65,
    0x5f, 0x63, 0x65, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x66, 0x65, 0x65,
    0x43, 0x65, 0x6e, 0x74, 0x12, 0x2b, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x08,
    0x20, 0x01, 0x28, 0x0e, 0x32, 0x13, 0x2e, 0x63, 0x6f, 0x6f, 0x6c, 0x63, 0x61, 0x72, 0x2e, 0x54,
    0x72, 0x69, 0x70, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75,
    0x73, 0x2a, 0x5b, 0x0a, 0x0a, 0x54, 0x72, 0x69, 0x70, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12,
    0x13, 0x0a, 0x0f, 0x54, 0x53, 0x5f, 0x4e, 0x4f, 0x54, 0x5f, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46,
    0x49, 0x44, 0x10, 0x00, 0x12, 0x0f, 0x0a, 0x0b, 0x4e, 0x4f, 0x54, 0x5f, 0x53, 0x54, 0x41, 0x52,
    0x54, 0x45, 0x44, 0x10, 0x01, 0x12, 0x0f, 0x0a, 0x0b, 0x49, 0x4e, 0x5f, 0x50, 0x52, 0x4f, 0x47,
    0x52, 0x45, 0x53, 0x53, 0x10, 0x02, 0x12, 0x0c, 0x0a, 0x08, 0x46, 0x49, 0x4e, 0x49, 0x53, 0x48,
    0x45, 0x44, 0x10, 0x03, 0x12, 0x08, 0x0a, 0x04, 0x50, 0x41, 0x49, 0x44, 0x10, 0x04, 0x42, 0x1e,
    0x5a, 0x1c, 0x63, 0x6f, 0x6f, 0x6c, 0x63, 0x61, 0x72, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f,
    0x67, 0x65, 0x6e, 0x2f, 0x67, 0x6f, 0x2f, 0x3b, 0x74, 0x72, 0x69, 0x70, 0x70, 0x62, 0x62, 0x06,
    0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}

var (
    file_trip_proto_rawDescOnce sync.Once
    file_trip_proto_rawDescData = file_trip_proto_rawDesc
)

func file_trip_proto_rawDescGZIP() []byte {
    file_trip_proto_rawDescOnce.Do(func() {
        file_trip_proto_rawDescData = protoimpl.X.CompressGZIP(file_trip_proto_rawDescData)
    })
    return file_trip_proto_rawDescData
}

var file_trip_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
var file_trip_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_trip_proto_goTypes = []interface{}{
    (TripStatus)(0),  // 0: coolcar.TripStatus
    (*Location)(nil), // 1: coolcar.Location
    (*Trip)(nil),     // 2: coolcar.Trip
}
var file_trip_proto_depIdxs = []int32{
    1, // 0: coolcar.Trip.statar_pos:type_name -> coolcar.Location
    1, // 1: coolcar.Trip.path_locations:type_name -> coolcar.Location
    1, // 2: coolcar.Trip.end_pos:type_name -> coolcar.Location
    0, // 3: coolcar.Trip.status:type_name -> coolcar.TripStatus
    4, // [4:4] is the sub-list for method output_type
    4, // [4:4] is the sub-list for method input_type
    4, // [4:4] is the sub-list for extension type_name
    4, // [4:4] is the sub-list for extension extendee
    0, // [0:4] is the sub-list for field type_name
}

func init() { file_trip_proto_init() }
func file_trip_proto_init() {
    if File_trip_proto != nil {
        return
    }
    if !protoimpl.UnsafeEnabled {
        file_trip_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
            switch v := v.(*Location); i {
            case 0:
                return &v.state
            case 1:
                return &v.sizeCache
            case 2:
                return &v.unknownFields
            default:
                return nil
            }
        }
        file_trip_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
            switch v := v.(*Trip); i {
            case 0:
                return &v.state
            case 1:
                return &v.sizeCache
            case 2:
                return &v.unknownFields
            default:
                return nil
            }
        }
    }
    type x struct{}
    out := protoimpl.TypeBuilder{
        File: protoimpl.DescBuilder{
            GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
            RawDescriptor: file_trip_proto_rawDesc,
            NumEnums:      1,
            NumMessages:   2,
            NumExtensions: 0,
            NumServices:   0,
        },
        GoTypes:           file_trip_proto_goTypes,
        DependencyIndexes: file_trip_proto_depIdxs,
        EnumInfos:         file_trip_proto_enumTypes,
        MessageInfos:      file_trip_proto_msgTypes,
    }.Build()
    File_trip_proto = out.File
    file_trip_proto_rawDesc = nil
    file_trip_proto_goTypes = nil
    file_trip_proto_depIdxs = nil
}

現在我們就可以在main中使用了

package main

import (
    trippb "coolcar/proto/gen/go" //包名:路徑
    "encoding/json"
    "fmt"

    "google.golang.org/protobuf/proto"
)

func main() {
    trips := trippb.Trip{
        Statar:      "北京",
        End:         "南京",
        DurationSec: 3600,
        FeeCent:     1000,
        StatarPos: &trippb.Location{
            Latitude:  30,
            Longitude: 120,
        },
        EndPos: &trippb.Location{
            Latitude:  125,
            Longitude: 35,
        },
        PathLocations: []*trippb.Location{
            {
                Latitude:  31,
                Longitude: 121,
            },
            {
                Latitude:  33,
                Longitude: 122,
            },
            {
                Latitude:  34,
                Longitude: 124,
            },
        },
        Status: trippb.TripStatus_FINISHED,
    }
    fmt.Println(&trips)

    //模擬兩端互動
    p, err := proto.Marshal(&trips) //將資料轉為二進位制流
    if err != nil {
        panic(err)
    }
    fmt.Printf("%X\n", p)
    var trips2 trippb.Trip
    err = proto.Unmarshal(p, &trips2) //將二進位制流解碼
    if err != nil {
        panic(err)
    }
    fmt.Println(&trips2)

    b, err := json.Marshal(&trips2) //將二進位制流轉為json格式
    if err != nil {
        panic(err)
    }
    fmt.Printf("%s\n", b)
    //微服務透過二進位制流進行資訊交流
}

輸出:


statar:"北京"  statar_pos:{latitude:30  longitude:120}  path_locations:{latitude:31  longitude:121}  path_locations:{latitude:33  longitude:122}  path_locations:{latitude:34  longitude:124}  end:"南京"  end_pos:{latitude:125  longitude:35}  duration_sec:3600  fee_cent:1000  status:FINISHED
0A06E58C97E4BAAC1206E58D97E4BAAC18901C20E8072A12090000000000003E40110000000000005E403212090000000000405F401100000000008041403A12090000000000003F40110000000000405E403A12090000000000804040110000000000805E403A12090000000000004140110000000000005F404003
statar:"北京"  statar_pos:{latitude:30  longitude:120}  path_locations:{latitude:31  longitude:121}  path_locations:{latitude:33  longitude:122}  path_locations:{latitude:34  longitude:124}  end:"南京"  end_pos:{latitude:125  longitude:35}  duration_sec:3600  fee_cent:1000  status:FINISHED
{"statar":"北京","statar_pos":{"latitude":30,"longitude":120},"path_locations":[{"latitude":31,"longitude":121},{"latitude":33,"longitude":122},{"latitude":34,"longitude":124}],"end":"南京","end_pos":{"latitude":125,"longitude":35},"duration_sec":3600,"fee_cent":1000,"status":3}

* 注意: protobuf中每個欄位都是可選的。*

例如:可以避免系統更新時舊系統與新系統之間的通訊,這樣可以避免如果老系統裡面沒有相應欄位,那麼系統將會掛掉,或者新系統刪去原來欄位,客服端通訊也不會因為欄位不存在而掛掉。

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章