GRPC:客户端ID或连接信息? [英] GRPC: Client ID or connection information?

查看:411
本文介绍了GRPC:客户端ID或连接信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从服务器端获取有关RPC调用的连接信息?还是像唯一的客户ID之类的东西?

Is there a way to get the connection information on RPC calls from server side? Or maybe something like unique client ID?

推荐答案

There is no connecton information which may help distinguish clients. One reason of this is proxies: different clients can have same IP and port (as I understand)

一种可能的解决方案是应用程序级别的握手协议.您可以添加rpc方法连接"并发送clientId作为来自服务器的响应.之后,您可以将自定义标头(元数据)附加到rpc调用.

One possible solution is handshake protocol in app level. You can add rpc method "Connect" and send clientId as response from server. Afterthat you can attach custom headers (metadata) to your rpc calls.

客户端Java代码:

String clientId = getIdfromServer();
Metadata.Key<String> CLIENT_ID = Metadata.Key.of("client_id", ASCII_STRING_MARSHALLER);
Metadata fixedHeaders = new Metadata();
fixedHeaders.put(CLIENT_ID, clientId);
blockingStub = MetadataUtils.attachHeaders(blockingStub, fixedHeaders);

此C ++服务器端代码显示如何在服务器上处理此类标头:

This C++ server side code shows how to handle such header on server:

::grpc::Status YourRPC(::grpc::ServerContext* context, const Your* request, YourResponse* response)
{
    const auto clientMetadata = context->client_metadata();
    auto it = clientMetadata.find("client_id");
    auto clientId = std::string(it->second.begin(), it->second.end());
}

我注意到元数据密钥不区分大小写. Grpc将键转换为小写字母.

I noticed that metadata key is case insensitive. Grpc converts keys to lowercase.

这篇关于GRPC:客户端ID或连接信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆