1、定义一个服务-useinfoservice.proto

syntax = "proto3";//标识 proto版本 建议使用proto3
package userinfoservice;//proto包名 避免命名冲突,也可以作为引入其他proto文件时使用
option java_package = "com.example.userinfoservice" ;//生成的类将带有此包名,不指定则使用package
option cc_generic_services = true;
option go_package = "./pb";
option java_outer_classname = "UserInfoEntity";//指定生成后的类名,里面会包含req/res,不指定则使用文件名

message GetUserInfoReq{
  string id = 1;
}
message GetUserInfoRes{
  string id = 1;
  string name = 2;
  int32 age = 3;
}

service UserInfoService {
  rpc getUserInfo(GetUserInfoReq) returns (GetUserInfoRes);
}

2、通过proto文件生成代码

1) Go

$ go install google.golang.org/protobuf/cmd/[email protected]
$ go install google.golang.org/grpc/cmd/[email protected]
$ export PATH="$PATH:$(go env GOPATH)/bin"
protoc --go_out=. --go_opt=paths=source_relative \\
    --go-grpc_out=. --go-grpc_opt=paths=source_relative \\
    proto/userinfo.proto

2) Java