如何响应多个gRPC客户端?

我想知道,

我正在构建一个可以具有多个gRPC服务器并且肯定会有多个gRPC客户端的应用程序, 如何在服务器端确定这是我正在与之交谈的客户端,并且仅向该客户端发送数据。 我正在使用双向流式RPC,现在数据已广播到每个客户端,但我不希望这样。 go gRPC中的哪些功能使其成为可能或我如何实现它?</ p>
</ div>

展开原文

原文

I am building an application which can have multiple gRPC servers and definitely will have multiple gRPC clients, I wanted to know, how to identify on server side that this is the client I am talking to and only send data to that client. I am using bidirectional streaming RPC and right now the data gets broadcasted to every client and I don't want that. What functions in go gRPC make it possible or how can I implement it?

dpnvrt3119
dpnvrt3119 您需要一个流客户端拦截器,以及google.golang.org/grpc/metadata。这样,您可以拦截每个呼叫并根据任何流检查其身份(我同意答案,JWT是一个不错的选择)
大约一年之前 回复

2个回答



有两种阅读此问题的方法。 一种方法是将其读取为之前回答的身份验证问题。 第二种方式是作为连接/会话问题阅读的方式。</ p>

当客户端连接时,grpc服务器将调用一个函数以在其自己的goroutine中实现该调用, 功能将仅与发起该呼叫的客户端通话。 因此,您注册为grpc服务器的结构将在许多连接中共享,但是每个连接将在其自己的goroutine中运行,并且只会与启动它的客户端进行通信。 这也意味着您必须确保grpc服务器实现是线程安全的。</ p>

您提到数据正在广播到每个客户端吗? grpc中没有广播,您确定是这样吗?</ p>
</ div>

展开原文

原文

There are two ways to read this question. One way is to read it as the auth problem as answered before. The second way is how I read it, as a connection/session problem.

When the client connects, the grpc server will invoke a function to implement the call in its own goroutine, and that function will be only talking to the client that initiated that call. So, the struct you registered as your grpc server will be shared among many connections, but each connection will run in its own goroutine, and will only talk to the client that initiated it. That also means you have to make sure the grpc server implementation is thread-safe.

You mentioned data is being broadcasted to every client? There is no broadcast in grpc, are you sure that's what's happening?

douxiong0668
douxiong0668 是的,并不是每个流都播放广播,我误会了,谢谢您的帮助
大约一年之前 回复



这听起来像是一个常见的身份验证/授权问题,最终与gRPC或Go无关。</ p> \ n

您需要一种让客户表明自己身份的方法。 我个人是 JWT 的粉丝。 在标准HTTP请求中,有授权标头可以指示谁在发出请求。 同样,gRPC支持附加到每个远程调用的元数据。 在我当前的工作项目中,每个调用都必须在元数据中包含一个JWT,否则我将不处理请求。 除了登录端点之外的每个呼叫。</ p>

我还没有研究如何获取诸如gRPC客户端的IP地址或有关客户端连接的其他信息之类的详细信息,但是可能由 gRPC生成的代码可能会被客户端伪造。 如果设计正确,JWT可以提供加密信任,确保客户就是他们声称的身份。</ p>
</ div>

展开原文

原文

This sounds like a common authentication/authorization problem that ultimately won't have much to do with gRPC or Go.

You need a way for a client to indicate who they are. Personally I'm a fan of JWTs. In a standard HTTP request, there are authorization headers that can indicate who is making the request. Similarly, gRPC supports meta data attached to each remote call. In my current work project, every call must have a JWT in the meta data or else I don't process the request. Every call except the login endpoint that is.

I haven't looked into how to get details like a gRPC client's IP address or other information about a client's connection but chances are that anything provided by gRPC's generated code is something potentially faked by the client. When architected correctly, JWTs can offer cryptographic confidence that the client is who they claim to be.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐