learning_notes

学习笔记

View project on GitHub

protobuf

参考 自动生成 gogoproto

概念

Protocol buffers是一个灵活的、高效的、自动化的用于对结构化数据进行序列化的协议

拓展

  1. 默认值
syntax = "proto3";

package main;

import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
	string default_string = 50000;
	int32 default_int = 50001;
}

message Message {
	string name = 1 [(default_string) = "gopher"];
	int32 age = 2[(default_int) = 10];
}
  1. 验证数据
    go get github.com/mwitkow/go-proto-validators/protoc-gen-govalidators
syntax = "proto3";

package main;

import "github.com/mwitkow/go-proto-validators/validator.proto";

message Message {
	string important_string = 1 [
		(validator.field) = {regex: "^[a-z]{2,5}$"}
	];
	int32 age = 2 [
		(validator.field) = {int_gt: 0, int_lt: 100}
	];
}

  1. rest接口
syntax = "proto3";

package main;

import "google/api/annotations.proto";

message StringMessage {
  string value = 1;
}

service RestService {
	rpc Get(StringMessage) returns (StringMessage) {
		option (google.api.http) = {
			get: "/get/{value}"
		};
	}
	rpc Post(StringMessage) returns (StringMessage) {
		option (google.api.http) = {
			post: "/post"
			body: "*"
		};
	}
}

命令安装protoc-gen-grpc-gateway插件:

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway

再通过插件生成grpc-gateway必须的路由处理代码:

protoc -I/usr/local/include -I. \
	-I$GOPATH/src \
	-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
	--grpc-gateway_out=. \
	hello.proto
	

在对外公布REST接口时,我们一般还会提供一个Swagger格式的文件用于描述这个接口规范

 go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger

 protoc -I. \
	-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
	--swagger_out=. \
	hello.proto