[英]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.同步和异步通信。
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.