MacOS Java+golang gRPC 代码生成
编译protobuf/protoc 工具
git clone https://github.com/protocolbuffers/protobuf.git
git checkout 3.3.x
mac 环境安装autoconf automake
brew install autoconf
brew install automake
build protobuf
cd protobuf
./autogen.sh
./configure
make
sudo make install
编译grpc-java
下载grpc-java
git clone https://github.com/grpc/grpc-java.git
git checkout git checkout v1.4.x
编译grpc-java
cd grpc-java
./gradlew build
生成目标文件compiler/build/exe/java_plugin/protoc-gen-grpc-java
准备grpc-go
下载grpc-java
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
go get -u google.golang.org/grpc
go install github.com/golang/protobuf/protoc-gen-go
protoc-gen-go需要放到环境变量里,让protoc程序能找到。
java 端生成代码
option java_package="com.dtstack.rdslite.task.engine.rpc";
protoc --plugin=protoc-gen-grpc-java=/Users/hx/workspace/java/github/grpc-java/compiler/build/exe/java_plugin/protoc-gen-grpc-java --grpc-java_out="./" "./service.proto"
golang 端生成代码
protoc -I=. -I=/Users/hx/workspace/go/src/ -I=/Users/hx/workspace/go/src/github.com/gogo/protobuf/protobuf/ \
--go_out=Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/duration.proto=github.com/gogo/protobuf/types,plugins=grpc:test service.proto
#-I=. 指向proto文件位置
#-I=/Users/hx/workspace/go/src/ #GOPATH
#-I=/Users/hx/workspace/go/src/github.com/gogo/protobuf/protobuf #依赖头文件
# --go_out= 说明参考https://github.com/gogo/protobuf
#test是生成目录
service.proto
syntax = "proto3";
package proto;
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.goproto_stringer_all) = false;
option (gogoproto.goproto_enum_prefix_all) = false;
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
option (gogoproto.stringer_all) = true;
option (gogoproto.gostring_all) = true;
option (gogoproto.description_all) = true;
option (gogoproto.populate_all) = true;
option (gogoproto.unmarshaler_all) = true;
option (gogoproto.marshaler_all) = true;
option (gogoproto.sizer_all) = true;
option (gogoproto.goproto_enum_stringer_all) = false;
option (gogoproto.enum_stringer_all) = true;
option (gogoproto.unsafe_marshaler_all) = false;
option (gogoproto.unsafe_unmarshaler_all) = false;
service EasyAgentService {
rpc registerSidecar(RegisterRequest) returns (RegisterResponse);
rpc readyForControl(ControlRequest) returns (stream ControlResponse);
rpc reportEvent(Event) returns (EmptyResponse);
}
service TransferService {
rpc send(Msg) returns (EmptyResponse);
}
message Msg {
string id = 1;
string index = 2;
string typ = 3;
bytes jsonBytes = 4;
bytes key = 5;
}
message EmptyResponse {}
message SidecarRequestHeader {
bytes id = 1 [(gogoproto.customtype) = "Uuid", (gogoproto.nullable) = false];
google.protobuf.Timestamp systime = 2 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
}
// Register
message RegisterRequest {
SidecarRequestHeader H = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
}
message RegisterResponse {
}
// Controlling
message ControlRequest {
SidecarRequestHeader H = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
}
message ControlResponse {
enum ControlCmd {
}
ControlCmd cmd = 1;
uint32 seqno = 2;
oneof options {
}