net.devh grpc-spring-boot-starter 2.11.0.RELEASE
详细内容请参考示例代码
定义proto文件
使用grpc-java编译生成java代码
1.7.0 1.31.1
3.13.0
0.6.1 io.grpc grpc-stub ${grpc.version} io.grpc grpc-protobuf ${grpc.version} com.google.protobuf protobuf-java ${protoc.version} kr.motd.maven os-maven-plugin ${os.plugin.version}
org.xolstice.maven.plugins protobuf-maven-plugin ${protobuf.plugin.version} true
com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}
grpc-java
io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
${project.basedir}/src/main/proto ${project.basedir}/src/main/java false
compile compile compile-custom
compile 至此,java代码生成完成
使用protoc生成swagger与go代码
protoc --proto_path=../interactive-common/src/main/proto --go_out=plugins=grpc,Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor:. google/api/annotations.proto google/api/http.protoprotoc --proto_path=../interactive-common/src/main/proto --go_out=plugins=grpc,Mgoogle/api/annotations.proto=interactive-interview-grpc-gateway/google/api:. common.protoprotoc --proto_path=../interactive-common/src/main/proto --go_out=plugins=grpc,Mgoogle/api/annotations.proto=interactive-interview-grpc-gateway/google/api,Mcommon.proto=interactive-interview-grpc-gateway/interactive/msg/protobuf:. interView/interView.protoprotoc --proto_path=../interactive-common/src/main/proto --grpc-gateway_out=logtostderr=true,Mcommon.proto=interactive-interview-grpc-gateway/interactive/msg/protobuf:. interView/interView.protoprotoc --proto_path=../interactive-common/src/main/proto --swagger_out=logtostderr=true:. interView/interView.protoprotoc --proto_path=../interactive-common/src/main/proto --grpc-gateway_out=logtostderr=true:. common.protoprotoc --proto_path=../interactive-common/src/main/proto --swagger_out=logtostderr=true:. common.proto
import ("context""flag""fmt""github.com/golang/glog""github.com/grpc-ecosystem/grpc-gateway/runtime""github.com/rs/cors""google.golang.org/grpc" // 此处导入的是protoc生成的interview "interactive-interview-grpc-gateway/interactive/msg/interview/protobuf""net/http")// grpc端口var (grpcServerEndpoint = flag.String("grpc-server-endpoint", "127.0.0.1:9003", "gRPC server endpoint")httpServerEndpoint = flag.String("http", ":19003", "http server address")version = flag.Bool("version", false, "show version"))var VersiOnName= "1.0.0"func run() error {ctx := context.Background()ctx, cancel := context.WithCancel(ctx)defer cancel()mux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, err != nil {glog.Fatal(err)}}
至此,go客户端编写完成
编译生成exe文件
go build -o [filename].exe main.go
启动服务端,再启动客户端,即可访问proto中定义的接口。
【文章原创作者:美国服务器 https://www.68idc.cn处的文章,转载请说明出处】