java grpc连接池_grpc(三)之grpc客户端使用连接池

本文展示了如何在Java中创建一个gRPC客户端,并利用连接池进行管理。代码示例中包含了`GrpcClient`类的详细实现,包括构造函数、关闭方法以及`getUserById`和`addBook`两个RPC方法,这些方法都通过连接池来调用gRPC服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值