如何将gRPC定义的API引入网络浏览器 [英] How to bring a gRPC defined API to the web browser

查看:120
本文介绍了如何将gRPC定义的API引入网络浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想为我们的gRPC-microservices构建一个Javascript/HTML gui.由于浏览器端不支持gRPC,因此我们考虑使用Web套接字连接到node.js服务器,该服务器通过grpc调用目标服务. 我们很难找到一个完美的解决方案来做到这一点.特别是,由于我们使用gRPC流在微服务之间推送事件. 似乎我们需要第二个RPC系统,仅用于在前端和node.js服务器之间进行通信.这似乎有很多开销,必须维护其他代码.

We want to build a Javascript/HTML gui for our gRPC-microservices. Since gRPC is not supported on the browser side, we thought of using web-sockets to connect to a node.js server, which calls the target service via grpc. We struggle to find an elegant solution to do this. Especially, since we use gRPC streams to push events between our micro-services. It seems that we need a second RPC system, just to communicate between the front end and the node.js server. This seems to be a lot of overhead and additional code that must be maintained.

有人有做类似事情的经验吗,或者有解决办法的想法吗?

Does anyone have experience doing something like this or has an idea how this could be solved?

推荐答案

自2018年10月23日以来, gRPC-Web项目是GA ,它可能是解决问题的最正式/标准化的方法. (即使现在已经是2018年了...;))

Since Oct 23,2018 the gRPC-Web project is GA, which might be the most official/standardized way to solve your problem. (Even if it's already 2018 now... ;) )

在GA博客中:"gRPC-Web,就像gRPC一样,可让您使用协议缓冲区定义客户端(网络)和后端gRPC服务之间的服务合同".自动生成.[...]"

From the GA-Blog: "gRPC-Web, just like gRPC, lets you define the service "contract" between client (web) and backend gRPC services using Protocol Buffers. The client can then be auto generated. [...]"

我们最近构建了gRPC-Web( https://github.com/improbable-eng/grpc -web )-遵循建议的gRPC-Web协议的浏览器客户端和服务器包装.该回购中的示例应提供一个很好的起点.

We recently built gRPC-Web (https://github.com/improbable-eng/grpc-web) - a browser client and server wrapper that follows the proposed gRPC-Web protocol. The example in that repo should provide a good starting point.

如果您使用的是Golang,则它需要gRPC服务器的独立代理或包装器.代理/包装器修改响应,以将预告片打包在响应正文中,以便浏览器可以读取它们.

It requires either a standalone proxy or a wrapper for your gRPC server if you're using Golang. The proxy/wrapper modifies the response to package the trailers in the response body so that they can be read by the browser.

披露:我是该项目的维护者.

Disclosure: I'm a maintainer of the project.

这篇关于如何将gRPC定义的API引入网络浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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