gRPC入门

gRPC入门

添加依赖

      
        1.23.0
        3.9.0
        3.9.0
    

 
        
            
                io.grpc
                grpc-bom
                ${grpc.version}
                pom
                import
            
        
    
 

        
            io.grpc
            grpc-netty-shaded
            runtime
        
        
            io.grpc
            grpc-protobuf
        
        
            io.grpc
            grpc-stub
        
        
            javax.annotation
            javax.annotation-api
            1.2
            provided 
        
        
            io.grpc
            grpc-testing
            test
        
        
            com.google.protobuf
            protobuf-java-util
            ${protobuf.version}
            

        
        
            
                kr.motd.maven
                os-maven-plugin
                1.5.0.Final
            
        
        

            
                org.xolstice.maven.plugins
                protobuf-maven-plugin
                0.5.1
                
                    com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}
                    grpc-java
                    io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
                

                
                    
                        
                            compile
                            compile-custom
                        
                    
                
                 
       
    
    

定义 proto文件

syntax = "proto3";  
//协议类型

package grpc.demo;

option java_package = "com.grpc";//变成java名的包名
option java_outer_classname = "service"; //变成java名的类名

// The greeting service definition.
service Greeter {
    // Sends a greeting
    rpc SayHello (HelloRequest) returns (HelloReply) {}   //定义接口 HelloRequest 请求参数,HelloReply 响应bean
    // Sends another greeting
    rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
    string name = 1;
}

// The response message containing the greetings
message HelloReply {
    string message = 1;
}

对proto文件进行编译,参考编译proto

编译成功会生成GreeterGrpc类,通过继承GreeterGrpc.GreeterImplBase ,重写其中自己在proto文件service中定义的两个接口 rpc SayHello (HelloRequest) returns (HelloReply) {} , rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}

public class GrpcServiceTest extends GreeterGrpc.GreeterImplBase {

    @Override
    public void sayHello(service.HelloRequest request, StreamObserver responseObserver) {
        String name = request.getName();
        log.info("收到请求{}",name);
        responseObserver.onNext(service.HelloReply.newBuilder().setMessage(name+"service.sayHello").build());
        responseObserver.onCompleted();

    }

    @Override
    public void sayHelloAgain(service.HelloRequest request, StreamObserver responseObserver) {
        super.sayHelloAgain(request, responseObserver);
    }
}

server类

 public static void main(String[] args) throws Exception{

//在8888端口上启动该服务  ,addSerice (new GrpcServiceTest())) ,可以增加多个
        final Server server = ServerBuilder.forPort(8888).addService(new GrpcServiceTest()).build().start();
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            server.shutdown();
        }));
        server.awaitTermination();
    }

client

 public static void main(String[] args) throws Exception{

//访问哪个地址
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8888).usePlaintext().build();

        GreeterGrpc.GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(channel);
        service.HelloReply reply = blockingStub.sayHello(service.HelloRequest.newBuilder().setName("zjb").build());
        System.out.println(reply.getMessage());

        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
    }

最后

  1. 启动Server类
  2. 运行client类,即可得到结果

你可能感兴趣的:(grpc)