简体   繁体   English

Protocol Buffer 与 Json - 何时选择一个而不是另一个

[英]Protocol Buffer vs Json - when to choose one over another

Can anyone explain when to use protocol buffer instead of JSON for micro-services architecture?谁能解释一下微服务架构何时使用协议缓冲区而不是 JSON? And vice-versa?反之亦然? Both on synchronous and asynchronous communication.同步和异步通信。

When to use JSON何时使用 JSON

  • You need or want data to be human readable您需要或希望数据是人类可读的
  • Data from the service is directly consumed by a web browser来自服务的数据由 Web 浏览器直接使用
  • Your server side application is written in JavaScript您的服务器端应用程序是用 JavaScript 编写的
  • You aren't prepared to tie the data model to a schema您还没有准备好将数据模型绑定到架构
  • You don't have the bandwidth to add another tool to your arsenal您没有足够的带宽将另一个工具添加到您的武器库中
  • The operational burden of running a different kind of network service is too great运行不同类型的网络服务的运营负担太大

Pros of ProtoBuf ProtoBuf 的优点

  • Relatively smaller size尺寸相对较小
  • Guarantees type-safety保证类型安全
  • Prevents schema-violations防止架构违规
  • Gives you simple accessors为您提供简单的访问器
  • Fast serialization/deserialization快速序列化/反序列化
  • Backward compatibility向后兼容

While we are at it, have you looked at flatbuffers ?当我们在做的时候,你看过flatbuffers吗?

Some of the aspects are covered here google protocol buffers vs json vs XML这里涵盖了一些方面google protocol buffers vs json vs XML

Reference:参考:

https://codeclimate.com/blog/choose-protocol-buffers/ https://codeclimate.com/blog/choose-protocol-buffers/

https://codeburst.io/json-vs-protocol-buffers-vs-flatbuffers-a4247f8bda6f https://codeburst.io/json-vs-protocol-buffers-vs-flatbuffers-a4247f8bda6f

I'd use JSON when the consumer is or could possibly be written in a language with built-in native support for JSON (Javascript is an example), a web browser, or where human readability is wanted.当消费者使用或可能使用内置原生支持 JSON 的语言(例如 Javascript)、Web 浏览器或需要人类可读性的语言编写时,我会使用 JSON。 Speaking of which, at least for asynchronous calls, many developers enjoy the convenience of examining the contents of the queue directly for debugging and even during the normal course of development.说起来,至少对于异步调用来说,很多开发者都喜欢直接检查队列的内容进行调试,甚至在正常的开发过程中也很方便。 Depending on the tech stack used, it may or may not be worth the trade off to use protobuf just to reduce network load since any performance increase wont buy you much in the async world.根据所使用的技术堆栈,使用 protobuf 只是为了减少网络负载可能值得也可能不值得权衡,因为任何性能提升都不会在异步世界中为您带来太多好处。 And it's not like we need to write a bunch of boiler plate code anymore like we used to with JSON marshalling and unmarshalling in most languages.并且我们不再需要像过去在大多数语言中使用 JSON 编组和解组那样编写一堆样板代码。

I'd use protobuf for everything else... if there are any other use cases left for it with the considerations above.我会将 protobuf 用于其他所有事情……如果根据上述考虑还有其他用例可供使用。 There are advantages you might see, such as performance, network load, the backwards compatibility offered by its versioning scheme, the lovely documentation that magically comes with proto files, and some validation!您可能会看到一些优势,例如性能、网络负载、其版本控制方案提供的向后兼容性、原始文件神奇地附带的可爱文档以及一些验证! If for some reason you have a lot of REST or other synchronous calls between microservices, protobuf can be sent over the wire instead of JSON without many trade offs, if any at all, while offering a heap of advantages.如果由于某种原因你在微服务之间有很多 REST 或其他同步调用,protobuf 可以通过线路而不是 JSON 发送,没有很多权衡,如果有的话,同时提供很多优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM