dpz1983
2019-09-17 21:00
浏览 924
已采纳

如何响应多个gRPC客户端?

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?

图片转代码服务由CSDN问答提供 功能建议

我想知道,

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanqianruan8448 2019-09-18 02:30
    已采纳

    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?

    点赞 评论
  • douvcpx6526 2019-09-17 21:46

    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.

    点赞 评论

相关推荐 更多相似问题