"Netty learning to clock in -- from beginners to giving up" -- grpc Server Streaming Rpc example of 16 - netty

Keywords: Java Netty Gradle

Punch date (July 18, 2019)

Learning points

-Using grpc to complete Server 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 UserRequest{
    int32 age = 1;
}

message UserResponse{
    string username = 1;
    int32 age = 2;
    string city = 3;
}


service StudentService{
    rpc GetStudentsByAge(UserRequest) returns(stream UserResponse){}
}

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 void getStudentsByAge(UserRequest request, StreamObserver<UserResponse> responseObserver) {
        System.out.println("getStudentsByAge() Parameters requested by client received , age = " + request.getAge());
        responseObserver.onNext(UserResponse.newBuilder().setAge(15).setUsername("Zhang San").setCity("Beijing").build());
        responseObserver.onNext(UserResponse.newBuilder().setAge(20).setUsername("Li Si").setCity("Shanghai").build());
        responseObserver.onNext(UserResponse.newBuilder().setAge(25).setUsername("Wang Wu").setCity("Tianjin").build());
        responseObserver.onNext(UserResponse.newBuilder().setAge(30).setUsername("Zhao Liu").setCity("Shenzhen").build());
        responseObserver.onNext(UserResponse.newBuilder().setAge(35).setUsername("Seven Masters").setCity("Guangzhou").build());
        responseObserver.onCompleted();
    }

}
Client
package com.dragon.study.client;

import com.dragon.study.StudentRequest;
import com.dragon.study.StudentResponse;
import com.dragon.study.StudentServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

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;
    private final StudentServiceGrpc.StudentServiceBlockingStub blockingStub;

    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);
    }

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

        Iterator<UserResponse> Iterator = blockingStub.getStudentsByAge(UserRequest.newBuilder().setAge(10).build());
        while (Iterator.hasNext()){
            UserResponse response = Iterator.next();
            System.out.println(response.getUsername() + "," + response.getAge() + "," + response.getCity());
        }
    }
    public static void main(String[] args) throws InterruptedException {
        StudentClient client = new StudentClient("localhost",8080);
        try{
           client.getUsers(1)
        }finally {
            client.shutdown();
        }


    }

}

Start Server and Client respectively
 Operation result:
Zhang San, 15, Beijing
 Li Si, 20, Shanghai
 Wang Wu, 25, Tianjin
 Zhao Liu, 30, Shenzhen
 Qiye, 35, Guangzhou

Posted by akjackson1 on Tue, 22 Oct 2019 08:55:05 -0700