"Netty learning to punch -- from beginners to giving up" -- 18 - netty's grpc Bidirectional Streaming RPC example

Keywords: Java Netty Gradle

Punch date (July 18, 2019)

Learning points

-Using grpc to complete Bidirectional Streaming RPC example

step

2. Write proto file
syntax = "proto3";


package study;

option java_package = "com.dragon.study";
option java_outer_classname = "Student";
option optimize_for = SPEED;
option java_multiple_files = true;

message StreamRequest{
    string message = 1;
}

message StreamResponse{
    string username = 1;
    string address = 2;
    int32 phone = 3;
}


service StudentService{
    rpc getAllUser(stream StreamRequest) returns (stream StreamResponse){}
}

3. Generate java classes with gradle generateProto
Run command:
gradle generateProto
4. Write Server/Client service
Server

The Server side is the same as the previous chapter
"Netty learning to clock in - from little white to giving up" -- grpc Simple RPC example of 15 - netty

package com.dragon.study.server;

import com.dragon.study.*;
import io.grpc.stub.StreamObserver;

import java.util.ArrayList;
import java.util.List;

public class StudentServiceImpl extends StudentServiceGrpc.StudentServiceImplBase {

     @Override
    public StreamObserver<StreamRequest> getAllUser(StreamObserver<StreamResponse> responseObserver) {
        StreamObserver<StreamRequest> streamObserver = new StreamObserver<StreamRequest>() {
            @Override
            public void onNext(StreamRequest value) {
                System.out.println("Parameters requested by client received SreamRequest.message = " + value.getMessage());
                responseObserver.onNext(StreamResponse.newBuilder().setAddress("Beijing").setPhone(123).setUsername("Wang Badu<"+value.getMessage()+">").build());
            }

            @Override
            public void onError(Throwable t) {

            }

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

        return streamObserver;
    }

}
Client
package com.dragon.study.client;

import com.dragon.study.*;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public class StudentClient {
    private static final Logger logger = Logger.getLogger(StudentClient.class.getName());

    //channel is equivalent to a connection, client core class
    private final ManagedChannel channel;
    //When the request is not a stream stream, blocking stub s can be used
    private final StudentServiceGrpc.StudentServiceBlockingStub blockingStub;

    //It should be pointed out that when the request is a stream stream, the asynchronous request mode is needed to request the service.
    private final StudentServiceGrpc.StudentServiceStub stub;

    public StudentClient(String host , int port){
        //ManagedChannelBuilder manages links for clients to create links
        this(ManagedChannelBuilder.forAddress(host,port).usePlaintext().build());
    }

    public StudentClient(ManagedChannel channel) {
        this.channel = channel;
        blockingStub = StudentServiceGrpc.newBlockingStub(channel);
        stub = StudentServiceGrpc.newStub(channel);
    }

    public void shutdown() throws InterruptedException {
        channel.shutdown().awaitTermination(100,TimeUnit.SECONDS);
    }

    public void getStreamUser(){
        StreamObserver<StreamRequest> requestStreamObserver = stub.getAllUser(new StreamObserver<StreamResponse>() {
            //Receive the returned results from the server
            @Override
            public void onNext(StreamResponse value) {
                System.out.println(value.getUsername() + "," + value.getAddress() + "," + value.getPhone());
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onCompleted() {

                System.out.println("conCompleted");

            }
        });
        requestStreamObserver.onNext(StreamRequest.newBuilder().setMessage("Mei Mei Da").build());
        requestStreamObserver.onCompleted();

    }

    public static void main(String[] args) throws InterruptedException {
        StudentClient client = new StudentClient("localhost",8080);
        try{
            client.getStreamUser();
        }finally {
            client.shutdown();
        }

    }

}

Meimeida by Wang baduzi, Beijing, 123
conCompleted

Posted by art15 on Tue, 22 Oct 2019 13:53:13 -0700