• notice
  • Congratulations on the launch of the Sought Tech site

Generate gRPC Java code method using Protocol Buffer 3

Since my tests are all Java, I only introduce java here, other languages are similar!

I won't talk about the extra polite words about gRPC here. For details, you can go to the official website to check the information and introduction;

1. Download protocol buffer 2/3 

    Documentation introduction: https://developers.google.com/protocol-buffers/docs/proto3

    Download address: https://repo1.maven.org/maven2/com/google/protobuf/protoc/3.0.2/

    After the download is complete, add the window environment variable (because I have two versions locally, so I named it protoc2/protoc3) and verify it after the addition is complete. As shown below:

    If it appears in the same picture of the author, the installation is successful!

2. When compiling gRPC, the protocol buffer needs to use protoc-gen-grpc-java as a plug-in to generate code,

    Documentation introduction: http://www.grpc.io/docs/quickstart/java.html

    Download address: https://repo1.maven.org/maven2/io/grpc/protoc-gen-grpc-java/1.0.1/

    After the download is complete, the same is added to the winddos environment variable.


3. Write the .proto file. Named: grpc-helloworld.proto . The content of the file is as follows:


syntax = "proto3";option java_generic_services = true;option java_multiple_files = true;option java_package = "com.hservice.grpc.schema";option java_outer_classname = "HelloWorldProto";// The greeting service definition.service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}}// The request message containing the user's name.message HelloRequest {
  string name = 1;}// The response message containing the greetingsmessage HelloReply {
  string message = 1;}

First generate the Proto file

Then execute the build command: ( >>> protoc3 --plugin=protoc-gen-grpc-java=D:/sysEnv/protoc-gen-grpc-java.exe --grpc-java_out=java --proto_path=proto proto/ g
rpc-helloworld.proto) to generate gRPC files


    If the execution fails at this step, please pay attention to the configuration of the path parameters.

4. After generation, the GreeterGrpc.java file will exist under the com.hservice.grpc.schema package. My post-generated java code is as follows:



5. Inherit and override the rpc method in gRPC


 * @author Rayn on 2016/9/25.
 * @email [email protected].
 */public class GrpcGreeterImpl extends GreeterGrpc.GreeterImplBase {
     * <pre>
     * Sends a greeting
     * </pre>
     * @param request
     * @param responseObserver
    public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
        HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + request.getName()).build();

So far, the corresponding work has been completed.


Note: My environment is win7 64-bit, protobuf 3, grpc plugin is 1.0.1, if you have any questions, you can communicate!


Technical otaku

Sought technology together

Related Topic


Leave a Reply