package com.oy.grpc.client;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import com.oy.grpc.BookServiceGrpc;
import com.oy.grpc.GrpcClientPool;
import com.oy.grpc.GrpcLib.GrpcReply;
import com.oy.grpc.GrpcLib.addBookRequest;
import com.oy.grpc.GrpcLib.getUserByIdRequest;
import com.oy.grpc.UserServiceGrpc;
import com.oy.utils.UtilFunctions;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;public classGrpcClient {public static String host = "localhost";privatefinal ManagedChannel channel;privatefinal UserServiceGrpc.UserServiceBlockingStub userBlockingStub;privatefinal BookServiceGrpc.BookServiceBlockingStub bookBlockingStub;public GrpcClient(String host, intport) {
channel=ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
userBlockingStub=UserServiceGrpc.newBlockingStub(channel);
bookBlockingStub=BookServiceGrpc.newBlockingStub(channel);
}public voidshutdown() throws InterruptedException {
channel.shutdown().awaitTermination(10, TimeUnit.SECONDS);
}
@SuppressWarnings({"rawtypes"})public staticObject call(String rpcMethoddName, Object... args) throws Exception {
UtilFunctions.log.info("=========== GrpcClient#call begin ===========");
GrpcClient client= null;try{
client=GrpcClientPool.borrowObject();//client = new GrpcClient(host, 23333);
Class[] argsTypes= newClass[args.length];for (int i = 0; i < args.length; i++) {
UtilFunctions.log.info("args types: {}", args[i].getClass());
argsTypes[i]=args[i].getClass();
}
Method method=client.getClass().getMethod(rpcMethoddName, argsTypes);
Object result=method.invoke(client, args);
UtilFunctions.log.info("=========== GrpcClient#call end ===========");returnresult;
}catch(Exception e) {
UtilFunctions.log.error("GrpcClient#call error, msg:{}, exception:{}", e.toString(), e);return null;
}finally{if (client != null) {
GrpcClientPool.returnObject(client);//client.shutdown();
}
}
}//============= User module =============
publicObject getUserById(Integer id) {
UtilFunctions.log.info("=========== GrpcClient#getUserById begin ===========");
getUserByIdRequest request=getUserByIdRequest.newBuilder().setId(id).build();
GrpcReply response;try{
response=userBlockingStub.getUserById(request);
UtilFunctions.log.info("GrpcClient#getUserById response, code:{}, data:{}", response.getCode(),
response.getData());
}catch(StatusRuntimeException e) {
UtilFunctions.log.error("GrpcClient#addBook error, msg:{}, exception:{}", e.toString(), e);return null;
}returnresponse;
}//============= Book module =============
publicObject addBook(Integer id, String name, Double price) {
UtilFunctions.log.info("=========== GrpcClient#addBook begin ===========");
addBookRequest request=addBookRequest.newBuilder().setId(id).setName(name).setPrice(price).build();
GrpcReply response;try{
response=bookBlockingStub.addBook(request);
UtilFunctions.log.info("GrpcClient#addBook response, code:{}, data:{}", response.getCode(),
response.getData());
UtilFunctions.log.info("=========== GrpcClient#addBook end ===========");
}catch(StatusRuntimeException e) {
UtilFunctions.log.error("GrpcClient#addBook error, msg:{}, exception:{}", e.toString(), e);return null;
}returnresponse;
}
}