GRPC如何重用客户端连接?
GRPC how to reuse client connection?
// ServiceA - proto
const proto = grpc.load({
root: protoDir,
file: 'serviceA.proto'
});
// ServiceB - proto
const proto = grpc.load({
root: protoDir,
file: 'serviceB.proto'
});
let clientA = new ddl_proto.com.mypackage.serviceA(common_url, grpc.credentials.createInsecure());
clientA.functionOne({
options: JSON.stringify(user_options)
}, (error, response) => {
console.log(response)
})
let clientB = new ddl_proto.com.mypackage.serviceB(common_url, grpc.credentials.createInsecure());
clientB.functionTwo({
options: JSON.stringify(user_options)
}, (error, response) => {
console.log(response)
})
如果 ServiceA
和 ServiceB
由同一台服务器提供服务,有什么方法可以重用客户端对象吗?
我可以做这样的事情吗:
let commonClient = new grpc.Client(common_url, grpc.credentials.createInsecure());
commonClient.ServiceA.functionOne();
commonClient.ServiceB.functionTwo();
目前,您不能为两种不同的服务明确地将单个连接附加到客户端。但是,如果您创建两个具有相同 URL、凭据和选项(如果有)的客户端,它们最终应该使用相同的底层连接。
// ServiceA - proto
const proto = grpc.load({
root: protoDir,
file: 'serviceA.proto'
});
// ServiceB - proto
const proto = grpc.load({
root: protoDir,
file: 'serviceB.proto'
});
let clientA = new ddl_proto.com.mypackage.serviceA(common_url, grpc.credentials.createInsecure());
clientA.functionOne({
options: JSON.stringify(user_options)
}, (error, response) => {
console.log(response)
})
let clientB = new ddl_proto.com.mypackage.serviceB(common_url, grpc.credentials.createInsecure());
clientB.functionTwo({
options: JSON.stringify(user_options)
}, (error, response) => {
console.log(response)
})
如果 ServiceA
和 ServiceB
由同一台服务器提供服务,有什么方法可以重用客户端对象吗?
我可以做这样的事情吗:
let commonClient = new grpc.Client(common_url, grpc.credentials.createInsecure());
commonClient.ServiceA.functionOne();
commonClient.ServiceB.functionTwo();
目前,您不能为两种不同的服务明确地将单个连接附加到客户端。但是,如果您创建两个具有相同 URL、凭据和选项(如果有)的客户端,它们最终应该使用相同的底层连接。