gRPC-Web:替代REST的gRPC的Javascript库包

解道jdon 2021-05-04 04:43:22
Web gRPC rest grpc-web


gRPC-Web是一个JavaScript客户端库,使Web应用程序能够直接与后端gRPC服务通信,而不需要HTTP服务器充当中介。这意味着你现在可以通过使用.proto 文件定义客户端服务器端数据类型和服务接口,轻松构建真正的端到端gRPC应用程序架构。因此,gRPC-Web为整个REST开发Web范例提供了一个引人注目的新选择。

需要明确的是,REST应用程序本身没有任何问题。使用REST API服务器构建了大量非常成功的应用程序,但是如果这些服务器使用非HTTP协议与后端服务进行通信,那么需要进行大量转换gRPC-Web的好处从“技术”上解决这个问题。

使用gRPC-Web的优点

随着时间的推移,gRPC-Web将提供更广泛的功能集。但我可以看到它从一开始就提供了一些巨大的胜利:

  • 端到端gRPC - 如上所述,使用gRPC-Web,可以正式从堆栈中删除REST组件并将其替换为纯gRPC,从而使能够使用Protocol Buffers创建整个 RPC管道。

  • 前端和后端团队之间更紧密的协调- 使用Protocol Buffers定义整个RPC管道,不再需要将“微服务团队”与“客户端团队”分离。客户端 - 后端交互只是一个gRPC层。

  • 轻松生成客户端库- 使用gRPC-Web,意味着后端服务器是gRPC服务器而不是HTTP服务器,这意味着您的所有服务都是客户端库也可以是gRPC库。需要Ruby,Python,Java和其他4种语言的客户端库吗?您不再需要为所有这些客户端编写HTTP客户端。

下面是一个Protocol Buffer格式定义:

syntax = “proto3”;
package todos;
message Todo {
  string content = 1;
  bool finished = 2;
}
message GetTodoRequest {
  int32 id = 1;
}
service TodoService {
  rpc GetTodoById (GetTodoRequest) returns (Todo);
}

可以使用protoc 命令行工具根据这个.proto 定义使用生成CommonJS的客户端代码:

protoc echo.proto \

--js_out = import_style = commonjs:./ output \

--grpc-web_out = import_style = CommonJS的:./输出

现在从后端gRPC服务器获取TODO列表可以这么简单:

const {GetTodoRequest} = require(‘./todos_pb.js’);
const {TodoServiceClient} = require(‘./todos_grpc_web_pb.js’);
const todoService = new proto.todos.TodoServiceClient(‘http://localhost:8080’);
const todoId = 1234;
var getTodoRequest = new proto.todos.GetTodoRequest();
getTodoRequest.setId(todoId);
var metadata = {};
var getTodo = todoService.getTodoById(getTodoRequest, metadata, (err, response) => {
 if (err) {
   console.log(err);
 } else {
   const todo = response.todo();
   if (todo == null) {
     console.log(`A TODO with the ID ${todoId} wasn’t found`);
   } else {
     console.log(`Fetched TODO with ID ${todoId}: ${todo.content()}`);
   }
 }
});

同样,没有HTTP代码或方法,没有JSON解析,没有头Header协商。您声明了数据类型和服务接口,并且gRPC-Web摘录了所有“hard wiring”样板代码,为您提供了一个干净且人性化的API(基本上与当前用于gRPC API的Node.js相同的API,刚刚转移到客户端)。

在后端,gRPC服务器可以用任何支持gRPC的语言编写,包括Go,Java,C ++,Ruby,Node.js等等。最后一块拼图是服务代理。从一开始,gRPC-Web将支持Envoy作为默认服务代理,它具有内置的envoy.grpc_web 过滤器,只需几行复制和可配置配置即可应用。

gRPC-Web

 

版权声明
本文为[解道jdon]所创,转载请带上原文链接,感谢
https://www.jdon.com/50508

  1. 21. Object oriented foundation "problems and solutions of object traversal"
  2. Discussion on hot micro front end: Google AdWords is a real micro front end
  3. Usecallback and usememo for real performance optimization
  4. 【前端圭臬】十一:从规范看 JavaScript 执行上下文(下)
  5. [front end standard] 11: Javascript execution context from the perspective of specification (2)
  6. Hexagonal六角形架构ReactJS的实现方式 - Janos Pasztor
  7. Transaction of spring's reactive / imperative relational database
  8. The implementation of hexagonal hexagonal reactjs Janos pasztor
  9. HTTP状态码:402 Payment Required需要付款 - mozilla
  10. HTTP status code: 402 payment required - Mozilla
  11. Factory mode, constructor mode and prototype mode
  12. Build the scaffold of react project from scratch (Series 1: encapsulating a request method with cache function based on Axios)
  13. Cocos Quick Start Guide
  14. Comparison of three default configurations of webpack5 modes
  15. A case study of the combination of flutter WebView and Vue
  16. CSS: BFC and IFC
  17. A common error report and solution in Vue combat
  18. JS: this point
  19. JS: prototype chain
  20. JavaScript series -- promise, generator, async and await
  21. JS: event flow
  22. Front end performance optimization: rearrangement and redrawing
  23. JS - deep and shallow copy
  24. JavaScript异步编程3——Promise的链式使用
  25. JavaScript asynchronous programming 3 -- chain use of promise
  26. Vue.js组件的使用
  27. The use of vue.js component
  28. How to judge whether a linked list has links
  29. Element UI custom theme configuration
  30. Text image parallax effect HTML + CSS + JS
  31. Spring的nohttp宣言:消灭http://
  32. Vue3 intermediate guide - composition API
  33. Analysis of URL
  34. These 10 widgets that every developer must know
  35. Spring's nohttp Manifesto: eliminate http://
  36. Learn more about JS prototypes
  37. Refer to await to JS to write an await error handling
  38. A short article will directly let you understand what the event loop mechanism is
  39. Vue3 uses mitt for component communication
  40. Characteristics and thinking of ES6 symbol
  41. Two way linked list: I'm no longer one-way driving
  42. Vue event and form processing
  43. Reactive TraderCloud实时外汇开源交易平台
  44. Reactive tradercloud real time foreign exchange open source trading platform
  45. Node.js REST API的10个最佳实践
  46. Ten best practices of node.js rest API
  47. Fiddler advanced usage
  48. Process from Vue template to render
  49. Promise up (asynchronous or synchronous)
  50. Principle and implementation of promise
  51. Vs code plug in sharing - run code
  52. Vue practical notes (1) introduction of Ant Design
  53. Vue actual combat notes (2) introduction of element plus
  54. Introduction to webpack
  55. Webpack construction process
  56. Vue notes
  57. The experience and lessons of moving from ruby megalith architecture to go microservice
  58. Using leancloud to add artitalk module to hexo blog
  59. Implementation of chrome request filtering extension
  60. Detailed introduction of beer import declaration elements and label quarantine [import knowledge]