原文:https://hackernoon.com/rest-in-peace-grpc-for-micro-service-and-grpc-for-the-web-a-how-to-908cc05e1083

https://stackoverflow.com/questions/43682366/how-is-grpc-different-from-rest

------------------------------------------------------

REST is not the Best for Micro-Services GRPC and Docker makes a compelling case

June 7th 2017

For quite a long time, when Service Oriented Architecture (SOA) and WebService were the talk of the tech town, most of the software systems were built using SOAP and related WSDL technology. I was involved in multiple projects built using SOAP technology. I started first dabbling with REST when faced with the need to create a quick prototype northbound/external interface for a system.

I, like many others, fell in love with REST immediately ;more so after reading more about it, in the jewel of a paper (Architectural Styles and the Design of Network-based Software Architectures,) by Roy Fielding -principal author of HTTP protocol and consequently founder of the World Wide Web based on the internet. Soon along came JSON; and REST and JSON quickly replaced the cumbersome inefficient SOAP and associated WSDL in building out Service Oriented Architectures.

Programs or other components could now easily talk to each other efficiently through firewalls over HTTP; just like SOAP but much more efficiently. More so the versioning in WSDL, or the lack of it, and the problems it causes when modifying, or slightly extending a published interface, all but vanished. Though a lot of Web Services still uses SOAP, most new development and especially software systems became internally composed of REST based services.

RESTful architecture is what the world wide web is based on. It is as relevant to that context now, as it was when HTTP was designed. But for composing software systems by modern software development teams, there are better technologies that are available now.

Protobuffer was the technology Google was using for long, along with GRPC , in its software ecosystem — one of the largest micro service ecosystems in the world. Google open sourced GRPC in 2015.

Though few companies will operate at this scale, this is a technology that is very relevant in most enterprise SW; with globally distributed teams, working over different time-zones, languages and cultures. A strongly typed and versioned interface helps a lot in the SW development in such companies and generally even across multiple teams.

While technologies like Protobuffer and GRPC are unambiguous in their implementations, the term micro-service reminds me loosely of the SOA acronym. Other than people very close to SW development, to many this terms is confusing. However this is not just a hype promoted by commercial SW vendors. Many companies like Netflix have started using Micro Servicesas a way to compose SW systems successfully.

What makes a Service in SOA a Microservice ? Maybe to get a context of what a micro-service is, you may need to know a little about the Why Micro-Service part

Basically, a microservice does fewer things, a lot of excellent articles are written regarding micro-services, but more than what it is, is what it frees; developers are not constrained to add features to the existing services blowing up the best thought of services into a behemoth over time. Developers have more control and flexibility of the system they are building.

The advent and wide adoption of easy to use container technology like Docker has a lot to do with the success of microservice. Usually microservice systems are implemented as a set of Docker containers.The immutable Docker layers concept, and easy composition via DockerFile, has really taken the OpenClosedPrinciple from a source code structuring practice that few knew and followed, to wide adoption by applying the principle to the executable, the binary level. A released Docker image is immutable, it is closed for all modifications. Having a good versioning strategy for the images ensures that, what is released, stays forever immutable and extension can be released as a different image, with a different version.

Here are some of the top reasons which have attracted me to use GRPC and Protobuffer technology in micro-services.

1. Static Typed & Versioned Interface between Components

Have you ever missed while working in REST, the strongly typed interfaces of distributed technology that had come and gone over the years COM , CORBA IDL’s and SOAP’s WSDL ? Neither have I; barely noticed, maybe a bit glad of not needing to do or deal with the monstrosity of WSDL’s code generation and allied; at least not until the lack of type deficiency began to show on SW development and integration in a multi-team, multi-component system. Here the lack of a strong versioned typed interface gave to all sorts of coding horrors; miscommunication and mistrust between disparate distributed teams.

2. Efficiency & Stability

REST and JSON were way more efficient than the XML heavy SOAP payloads that get passed around between Web Services. GRPC uses Protobuffer for binary serialization of messages, which gives a lot more efficiency when compared to textual JSON used by REST. Also, GRPC works on HTTP/2.0 protocol, unlike most REST libraries which implicitly use the ubiquitous HTTP /1.1. HTTP/2.0 is multiplexed, has header compression and is way more efficient than HTTP/1.1. Till I started using and reading about GRPC, I did not even knew about HTTP/2 and the efficiency it offered.

3. Polygot Programming

GRPC and Protobuffer are available in popular languages like C++, Java, Node.js (Typescript), Go among others. This gives tremendous flexibility in building a system. For example, for some computer vision based tasks that require CPU, GPU resources, you can power-up a C++ -GRPC based micro-service, that interfaces with a Go based micro-service and a TypeScript based micro-service. In practice, composing a software system with the flexibility to use the right programming language is a pretty good advantage.

4. Request-Response Streaming, message-queue

There are many other reasons too. One example that I find pretty intriguing and testing out in production is the streaming part; you can either have the request streamed, the response streamed, or both request and responses streamed over. This is making the message queue implementations or Akka based remote actors obsolete, for many of the asynchronous message based communication that happens in a software system.

5. Almost Serverless

The other being the fact that the GRPC implementation is as server-less as you can get, without going full serverless. That is you don’t need Java’s Jetty/Jersey/Tomcat/Jboss (in the increasing order of server-side thickness), Scala’s Spray (and accompanying DSL monstrosity), C++ nothing/G-SOAP ? or any other full-fledged server or embedded servers. GRPC server library implementation in most of the languages I have tried seems to be mature and ripe for production. And you barely notice. It is almost like serverless, without having to sell your soul to the ‘server provider’. And the server in it is the efficient, multiplexed HTTP/2 based server.

However this same use of HTTP/2 is one sticking problem in adoption. This means that unlike REST, you cannot just type the GRPC API in browser view and expect it to work. So this integration with front-end is usually a problem that sticks out. There are options to work around that, one having a backend for front end component, to orchestrate among the multiple back-end micro-services and expose a REST API for the GUI layer. The other is using a proxy to proxy the HTTP/2 based GRPC to HTTP/1.1 via grpc-web. I have written a how-to for the latter part. You can read about this here.

Update: August 2018

A blog post I found was mirroring the exact same problems regarding the problems of Interface Semantics with REST. Other than experiencing this yourself with time, or reading about it from other peoples experience, it is hard to prove or disprove design decisions.

Our existing systems have traditionally used REST APIs with JSON payloads for communicating synchronously. This choice was made based on the overwhelming maturity, familiarity, and tooling available, but as our cross-continent engineering teams grew, we needed to design a consistent, agreed upon RESTful API. …Eventually, the complications with our REST API led us to search out alternatives https://blog.bugsnag.com/grpc-and-microservices-architecture/

REST is not the Best for Micro-Services GRPC and Docker makes a compelling case的更多相关文章

  1. 微服务 Micro services

    微服务 (Microservices) 是一种软件架构风格,它是以专注于单一责任与功能的小型功能区块 (Small Building Blocks) 为基础,利用模组化的方式组合出复杂的大型应用程序, ...

  2. 安装composer slim(php web api micro services)

    1. 安装php7 2. 下载 https://getcomposer.org/composer.phar 3. 开启ssh, 在 php.ini中.extension=C:\Program File ...

  3. Secure services with TLS ---Docker配置https

    官方文档:https://docs.docker.com/ee/ucp/interlock/usage/tls/

  4. 【Linux】【Services】【Docker】Docker File

    Docker Images: docker commit Dockerfile:文本文件,镜像文件构建脚本: Dockerfile:由一系列用于根据基础镜像构建新的镜像文件的专用指令序列组成: 指令: ...

  5. 【Linux】【Services】【Docker】网络

    容器的网络模型: closed container: 仅有一个接口:loopback 不参与网络通信,仅适用于无须网络通信的应用场景,例如备份.程序调试等: --net none bridged co ...

  6. 【Linux】【Services】【Docker】应用

    1. Docker应用: 镜像:包含了启动Docker容器所需要的文件系统层级及其内容:基于UnionFS采用分层结构实现: bootfs,rootfs registry:保存docker镜像及镜像层 ...

  7. 【Linux】【Services】【Docker】基础理论

    1. 名称空间:NameSpace 内核级别,环境隔离: 1.1. 名称空间的历史 PID NameSpace:Linux 2.6.24 ,PID隔离 Network NameSpace:Linux ...

  8. Micro Frontends & microservices

    Micro Frontends & microservices https://micro-frontends.org/ https://github.com/neuland/micro-fr ...

  9. Docker入门(四):服务(Services)

    这个<Docker入门系列>文档,是根据Docker官网(https://docs.docker.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指 ...

随机推荐

  1. 修正线性单元(Rectified linear unit,ReLU)

    修正线性单元(Rectified linear unit,ReLU) Rectified linear unit 在神经网络中,常用到的激活函数有sigmoid函数f(x)=11+exp(−x).双曲 ...

  2. python 爬虫实例(一)

    一个简单的爬虫工程 环境: OS:Window10 python:3.7 安装一些库文件 pip install requests pip install beautifulsoup4 pip ins ...

  3. 使用静态方法CreateInstance()创建数组

    Array类是一个抽象类,所以不能使用构造函数来创建数组.但除了可以使用C#语法创建数组的实例之外,还可以使用静态方法CreateInstance()创建数组. 如果事先不知道元素的类型,该静态方法就 ...

  4. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  5. jar 常用操作

    查看 jar 包中的文件列表,并进行重定向 jar -tvf a.jar > a.txt 更新文件到 jar 中,目录需对应 jar -uf a.jar com/a.class a.class ...

  6. 6.66 分钟,一文Python爬虫解疑大全教入门!

    我收集了大家关注爬虫最关心的  16 个问题,这里我再整理下分享给大家,并一一解答. 1. 现在爬虫好找工作吗? 如果是一年前我可能会说爬虫的工作还是挺好找的,但现在已经不好找了,一市场饱和了,二是爬 ...

  7. Python04之数据类型

    Python的数据类型主要有四类:整型.浮点型.字符串类型.布尔类型 整型:所有整数都属于整型(长整型和整型)         如:-121,0,765,89,12306 浮点型:数字上有小数点的数 ...

  8. 机器学习之主成分分析PCA原理笔记

    1.    相关背景 在许多领域的研究与应用中,通常需要对含有多个变量的数据进行观测,收集大量数据后进行分析寻找规律.多变量大数据集无疑会为研究和应用提供丰富的信息,但是也在一定程度上增加了数据采集的 ...

  9. 使用Docker搭建svn服务器教程

    svn简介 SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subv ...

  10. MySQL中的case when 中对于NULL值判断的坑

    sql中的case when 有点类似于Java中的switch语句,比较灵活,但是在Mysql中对于Null的处理有点特殊 Mysql中case when语法: 语法1: CASE case_val ...