原文: https://www.cncf.io/blog/2018/10/24/grpc-web-is-going-ga

On behalf of the Cloud Native Computing Foundation, I’m excited to announce the GA release of gRPC-Web, a JavaScript client library that enables web apps to communicate directly with backend gRPC services, without requiring an HTTP server to act as an intermediary. This means that you now easily build truly end-to-end gRPC application architectures by defining your client- and server-side data types and service interfaces using .proto files. gRPC-Web thus provides a compelling new alternative to the entire REST paradigm of web development.

The basics

gRPC-Web enables you to define the service “contract” between client web applications and backend gRPC servers using .protodefinitions and auto-generate client JavaScript (you can choose between Closure compiler JavaScript or the more widely used CommonJS). What you get to leave out of the development process: creating custom JSON serialization and deserialization logic, wrangling HTTP status codes (which can vary across REST APIs), content type negotiation, etc.

From a broader architectural perspective, what gRPC-Web makes possible is end-to-end gRPC. The diagram below illustrates this:

In the gRPC-Web universe on the left, a client application speaks Protocol Buffers to a gRPC backend server that speaks Protocol Buffers to other gRPC backend services. In the REST universe on the right, the web app speaks HTTP to a backend REST API server that then speaks Protocol Buffers to backend services.

To be clear, there’s nothing wrong with the REST application on the right per se. Tons of highly successful applications have been built using REST API servers that communicate with backend services using non-HTTP protocols. But imagine those applications’ development processes united around a single protocol and a single set of .proto interfaces (and thus a single set of service contracts) and you can almost feel the countless hours saved and headaches avoided. The benefits of gRPC-Web aren’t “merely” technological; they’re organizational as well. That bright orange line isn’t just a different protocol—it’s an independent source of work and cognitive load that you can now easily turn bright green.

Advantages of using gRPC-Web

gRPC-Web will offer an ever-broader feature set over time. But I can see it offering a handful of big wins from the get-go:

  • End-to-end gRPC — As mentioned above, with gRPC-Web you can officially cut the REST component out of your stack and replace it with pure gRPC, enabling you to craft your entire RPC pipeline using Protocol Buffers. Imagine a scenario in which a client request goes to an HTTP server, which then interacts with 5 backend gRPC services. There’s a good chance that you’ll spend as much time building the HTTP interaction layer as you will building the entire rest of the pipeline.
  • Tighter coordination between frontend and backend teams — Think back to the diagram up above. With the entire RPC pipeline defined using Protocol Buffers, you no longer need to have your “microservices teams” alongside your “client team.” The client-backend interaction is just one more gRPC layer amongst others. I honestly have yet to fully grasp the implications for end-to-end testing, service mesh integration, continuous integration/delivery, and more.
  • Easily generate client libraries — With gRPC-Web, the server that interacts with the “outside” world, i.e. the membrane connecting your backend stack to the internet, is now a gRPC server instead of an HTTP server, that means that all of your service’s client libraries can be gRPC libraries. Need client libraries for Ruby, Python, Java, and 4 other languages? You no longer need to write HTTP clients for all of them.

A gRPC-Web example

The previous section illustrated some of the high-level advantages of gRPC-Web for large-scale applications. Now let’s get closer to the metal with an example: a simple TODO app. In gRPC-Web you can start with a simple todos.proto definition like this:

  syntax = “proto3”;
   
  package todos;
   
  message Todo {
  string content = 1;
  bool finished = 2;
  }
   
  message GetTodoRequest {
  int32 id = 1;
  }
   
  service TodoService {
  rpc GetTodoById (GetTodoRequest) returns (Todo);
  }
view rawtodos.proto hosted with  by GitHub

You can use that .proto definition to generate CommonJS client-side code using the protoc command-line tool:

  protoc echo.proto \
  --js_out=import_style=commonjs:./output \
  --grpc-web_out=import_style=commonjs:./output
view rawprotoc-gen.sh hosted with  by GitHub

Now fetching a list of TODOs from a backend gRPC server can be as simple as this:

  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()}`);
  }
  }
  });
view rawtodo-service.js hosted with  by GitHub

Again, no HTTP codes or methods, no JSON parsing, no header negotiation. You declare data types and a service interface and gRPC-Web abstracts away all the “hard wiring” boilerplate, leaving you with a clean and human-friendly API (essentially the same API as the current Node.js for gRPC API, just transferred to the client).

On the backend, the gRPC server can be written in any language that supports gRPC, including Go, Java, C++, Ruby, Node.js, and many others (see language-specific docs in the official gRPC docs). The last piece of the puzzle is the service proxy. From the get-go, gRPC-Web will support Envoy as the default service proxy, which has a built-in envoy.grpc_web filter that you can apply with just a few lines of copy-and-pastable configuration. I’ll be saying more about this on the Envoy blog soon.

Next steps

Going GA means that the core building blocks are firmly in place and ready for usage in production web applications. But there’s still much more to come for gRPC-Web. Check out the official roadmap to see what the core team envisions for the near future.

If you’re interested in contributing to gRPC-Web, there are a few things that the core team would love community help with:

  • Front-end framework integration — Commonly used front-end frameworks like ReactAngular, and Vue don’t yet offer official support for gRPC-Web. But we would love to see these frameworks support it, as each of them would greatly benefit from gRPC.
  • Language-specific proxy support — As of the GA release, Envoy is the default proxy for gRPC-Web, offering support via a special module. But we’d also love to see development of in-process proxies for specific languages. In-process proxies obviate the need for special proxies—such as Envoy and nginx—and would make using gRPC-Web even easier.

We’d also love to get feature requests from the community. Currently the best way to make feature requests is to fill out the gRPC-Web roadmap features survey. Just list features you’d like to see and also let us know if you’d like to contributing to those features’ development in the I’d like to contribute to section. The gRPC-Web engineers will be sure to take that information to heart over the course of the project’s development.

 

gRPC-Web is going GA的更多相关文章

  1. Blazor WebAssembly + Grpc Web = 未来?

    Blazor WebAssembly是什么 首先来说说WebAssembly是什么,WebAssembly是一个可以使C#,Java,Golang等静态强类型编程语言,运行在浏览器中的标准,浏览器厂商 ...

  2. 进行API开发选gRPC还是HTTP APIs?

    上一篇文章我带着大家体验了一把<ASP.NET Core 3.0 上的gRPC服务模板初体验(多图)>,如果有兴趣的可以点击链接进行查看,相信跟着做的你,也是可以跑起来的.这篇文章我们将一 ...

  3. net core 3.0 之Grpc新特性小试牛刀

      相信微服务大家伙都有听说和知道,好处弊端咱也不多说了,Grpc算是一个比较全面的微服务框架,也得到微软的支持 总结下来就是,跨平台,可靠,通信快,扩展性强,网络消耗小,模板多语言通用 光说好处,没 ...

  4. [系列] Go gRPC 调试工具

    目录 概述 写一个 gRPC API grpcui 使用 go-gin-api 系列文章 概述 最近这段时间工作挺忙的,发现已经 3 周没更文了... 感谢你们还在,今天给大家分享一款 gRPC 的调 ...

  5. Go gRPC 调试工具

    概述 最近这段时间工作挺忙的,发现已经 3 周没更文了... 感谢你们还在,今天给大家分享一款 gRPC 的调试工具. 进入正题. 当我们在写 HTTP 接口的时候,使用的是 Postman 进行接口 ...

  6. 一个新实验:使用gRPC-Web从浏览器调用.NET gRPC服务

    今天给大家翻译一篇由ASP.NET首席开发工程师James Newton-King前几天发表的一篇博客,文中带来了一个实验性的产品gRPC-Web.大家可以点击文末的讨论帖进行相关反馈.我会在文章末尾 ...

  7. grpc调试工具

    grpcurl 和 grpcui 都是调试grpc的利器,前者用于命令行,类似curl工具:后者是以web的形式进行调试的,类似postman工具. 有了这两款工具,我们不用写任何客户端代码,也能方便 ...

  8. 旧 WCF 项目成功迁移到 asp.net core web api

    背景 接上一篇,放弃了 asp.net core + gRPC 的方案后,我灵光一闪,为什么不用 web api 呢?不也是 asp.net core 的吗?虽然 RESTful 不是强约束,客户端写 ...

  9. Golang gRPC调试工具

    目录 Golang gRPC调试工具 1. 命令行工具 grpcurl 1.1 安装 1.2 验证 1.3 注册反射 1.4 使用示例 2. web调试工具grpcui 2.1 安装 2.2 验证 2 ...

  10. 如何基于gRPC沟通微服务框架

    本文我们来讲解一下如何使用 gRPC构建微服务,gRPC是一个开源框架,可用于构建可扩展且高性能的微服务并创建服务之间的通信. 背景 随着企业越来越多地转向微服务,对构建这些微服务的低延迟和可扩展框架 ...

随机推荐

  1. Java基础-集合(12)

    存储数据的容器有数组和StringBuilder.StringBuilder的结果是一个字符串,不满足要求,所以只能选择数组,这就是对象数组.而对象数组又不能适应变化的需求,因为数组的长度是固定的,这 ...

  2. python-day13--装饰器

    1.开放封闭的原则: 1.对扩展是开放的 为什么要对扩展开放呢? 我们说,任何一个程序,不可能在设计之初就已经想好了所有的功能并且未来不做任何更新和修改.所以我们必须允许代码扩展.添加新功能. 2.对 ...

  3. POJ-2251 Dungeon Master (BFS模板题)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  4. 78. Subsets 90. Subsets II

    1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...

  5. OC 对象调用属性或实例变量或方法的细节。

    1.成员变量可以理解为所有在类的头上声明的,无论是@interface.@implementation下用大括号括起来或者是用@property声明的变量都可以称作这个类的 成员变量,只是在@impl ...

  6. iOS-UI篇—简单的浏览器查看程序和Tomcat简单实现

    #import "ViewController.h" @interface ViewController () @property (retain, nonatomic) NSAr ...

  7. JavaScript运算符:递增递减运算符前置和后置的区别

    从两段代码说起 var num1 = 2; var num2 = 20; var num3 = --num1 + num2; var num4 = num1 + num2; console.log(n ...

  8. en_java去重排序

    @Test public void tes5() { String[] strs ={"e", "ee", "ea", "ei&q ...

  9. python随机数,随机选择……random

    import random from random import random, uniform, randint, randrange, choice, sample, shuffle list = ...

  10. 数据结构(C语言)关于树、二叉树、图的基本操作。

    1) 编写算法函数int equal(tree t1, tree t2),判断两棵给定的树是否等价: int equal(tree t1,tree t2) { int k; if(t1==NULL&a ...