GRPC의 4가지 서비스 유형

19761 단어 Protobuf
지난번에grpc의 사용방법을 간단히 소개하였으며, 하나의 방법으로 호출하였는데, grpc에는 네 가지 서비스 유형이 있는데, 아래에 각각 소개하였다.

단순 rpc


이것은 일반적인 rpc 호출입니다. 요청 대상이 되돌아오는 대상에 대응합니다.
proto  :
rpc simpleHello(Person) returns (Result) {}

service  
@Override
public void simpleHello(ProtoObj.Person request,
                  io.grpc.stub.StreamObserver responseObserver) {
    //    
    responseObserver.onNext(ProtoObj.Result.newBuilder().setString("hello, "+request.getMyName()).build());
    responseObserver.onCompleted();
}

client  
@Test
public void  simple() throws InterruptedException {

    final ManagedChannel channel = ManagedChannelBuilder.forAddress("127.0.0.1", 8080).usePlaintext(true).build();
    //       stub
    HelloServiceGrpc.HelloServiceBlockingStub blockingStub = HelloServiceGrpc.newBlockingStub(channel);

    ProtoObj.Person person = ProtoObj.Person.newBuilder().setMyName("World").build();
    //simple
    System.out.println("---simple rpc---");
    System.out.println(blockingStub.simpleHello(person).getString());
    channel.shutdown();
}

  
---simple rpc---
hello, World

서비스 사이드 흐름식 rpc


하나의 요청 대상, 서버는 여러 개의 결과 대상을 전송할 수 있다
proto  
rpc serverStreamHello(Person) returns (stream Result) {}

service  
@Override
public void serverStreamHello(ProtoObj.Person request,
                        io.grpc.stub.StreamObserver responseObserver) {
    //      
    responseObserver.onNext(ProtoObj.Result.newBuilder().setString("hello, "+request.getMyName()).build());
    responseObserver.onNext(ProtoObj.Result.newBuilder().setString("hello2, "+request.getMyName()).build());
    responseObserver.onNext(ProtoObj.Result.newBuilder().setString("hello3, "+request.getMyName()).build());
    responseObserver.onCompleted();
}

client  
@Test
public void serverStream(){

    final ManagedChannel channel = ManagedChannelBuilder.forAddress("127.0.0.1", 8080).usePlaintext(true).build();
    //       stub
    HelloServiceGrpc.HelloServiceBlockingStub blockingStub = HelloServiceGrpc.newBlockingStub(channel);

    ProtoObj.Person person = ProtoObj.Person.newBuilder().setMyName("World").build();

    //server side
    System.out.println("---server stream rpc---");
    //     Iterator
    Iterator it = blockingStub.serverStreamHello(person);
    while (it.hasNext()) {
        System.out.print(it.next());
    }
    channel.shutdown();
}

  
---server stream rpc---
string: "hello, World"
string: "hello2, World"
string: "hello3, World"

클라이언트 흐름 rpc


클라이언트가 여러 개의 요청 대상을 전송하고 서버에서 응답 결과를 되돌려줍니다
proto  
rpc clientStreamHello(stream Person) returns (Result) {}

service  
@Override
public io.grpc.stub.StreamObserver clientStreamHello(
       final io.grpc.stub.StreamObserver responseObserver) {
   //  observer        
   return new StreamObserver(){
       private ProtoObj.Result.Builder builder=ProtoObj.Result.newBuilder();
       @Override
       public void onNext(ProtoObj.Person value) {
            builder.setString(builder.getString() +"," + value.getMyName());
       }

       @Override
       public void onError(Throwable t) {

       }

       @Override
       public void onCompleted() {
           builder.setString("hello"+builder.getString());
           responseObserver.onNext(builder.build());
           responseObserver.onCompleted();
       }
   };
}

client  
@Test
public void clientStream() throws InterruptedException {
    final ManagedChannel channel = ManagedChannelBuilder.forAddress("127.0.0.1", 8080).usePlaintext(true).build();
    //     stub
    HelloServiceGrpc.HelloServiceStub asyncStub = HelloServiceGrpc.newStub(channel);
    ProtoObj.Person person = ProtoObj.Person.newBuilder().setMyName("World").build();

    //client side
    System.out.println("---client stream rpc---");
    StreamObserver responseObserver = new StreamObserver() {
        @Override
        public void onNext(ProtoObj.Result result) {
            System.out.println("client stream--" + result.getString());
        }

        @Override
        public void onError(Throwable t) {
        }

        @Override
        public void onCompleted() {
            //  channel
            channel.shutdown();
        }
    };
    StreamObserver clientStreamObserver = asyncStub.clientStreamHello(responseObserver);
    clientStreamObserver.onNext(ProtoObj.Person.newBuilder().setMyName("World").build());
    clientStreamObserver.onNext(ProtoObj.Person.newBuilder().setMyName("World2").build());
    clientStreamObserver.onCompleted();
    //         ,  sleep  
    Thread.sleep(1000);
}

  
---client stream rpc---
client stream--hello,World,World2

쌍방향 유동식 rpc


클라이언트 흐름식 rpc와 서버 흐름식 rpc를 결합하면 여러 개의 대상을 전송하고 여러 개의 응답 대상을 되돌릴 수 있다
proto  
rpc biStreamHello(stream Person) returns (stream Result) {}

service  
@Override
public io.grpc.stub.StreamObserver biStreamHello(
        final io.grpc.stub.StreamObserver responseObserver) {
    //  observer        
    return new StreamObserver(){
        private ProtoObj.Result.Builder builder=ProtoObj.Result.newBuilder();
        @Override
        public void onNext(ProtoObj.Person value) {
            responseObserver.onNext(ProtoObj.Result.newBuilder().setString("hello2, "+value.getMyName()).build());
            responseObserver.onNext(ProtoObj.Result.newBuilder().setString("hello3, "+value.getMyName()).build());
        }

        @Override
        public void onError(Throwable t) {
        }

        @Override
        public void onCompleted() {
            responseObserver.onCompleted();
        }
    };
}

client  
@Test
public void bidirectStream() throws InterruptedException {

    final ManagedChannel channel = ManagedChannelBuilder.forAddress("127.0.0.1", 8080).usePlaintext(true).build();
    //     stub
    HelloServiceGrpc.HelloServiceStub asyncStub = HelloServiceGrpc.newStub(channel);

    ProtoObj.Person person = ProtoObj.Person.newBuilder().setMyName("World").build();

    //bi stream
    System.out.println("---bidirectional stream rpc---");
    StreamObserver  responseObserver = new StreamObserver() {
        @Override
        public void onNext(ProtoObj.Result result) {
            System.out.println("bidirectional stream--"+result.getString());
        }

        @Override
        public void onError(Throwable t) {
        }

        @Override
        public void onCompleted() {
            channel.shutdown();
        }
    };
    StreamObserver biStreamObserver=asyncStub.biStreamHello(responseObserver);
    biStreamObserver.onNext(ProtoObj.Person.newBuilder().setMyName("World").build());
    biStreamObserver.onNext(ProtoObj.Person.newBuilder().setMyName("World2").build());
    biStreamObserver.onCompleted();
    //         ,  sleep  
    Thread.sleep(1000);

}

  
---bidirectional stream rpc---
bidirectional stream--hello2, World
bidirectional stream--hello3, World
bidirectional stream--hello2, World2
bidirectional stream--hello3, World2    

총결산


grpc는 유동식 방식을 사용하여 여러 개의 실례를 되돌려받거나 받아들여 부정확한 수조의 인삼과 출삼에 사용할 수 있다

좋은 웹페이지 즐겨찾기