Java Google 协议缓冲区和 HTTP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1425912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:56:08  来源:igfitidea点击:

Google Protocol Buffers and HTTP

javac++httpprotocol-buffers

提问by Nazgob

I'm refactoring legacy C++ system to SOA using gSoap. We have some performance issues (very big XMLs) so my lead asked me to take a look at protocol buffers. I did, and it looks very cool (We need C++ and Java support). However protocol buffers are solution just for serialization and now I need to send it to Java front-end. What should I use from C++ and Java perspective to send those serialized stuff over HTTP (just internal network)?

我正在使用 gSoap 将遗留 C++ 系统重构为 SOA。我们有一些性能问题(非常大的 XML),所以我的领导让我看看协议缓冲区。我做了,它看起来很酷(我们需要 C++ 和 Java 支持)。然而,协议缓冲区只是用于序列化的解决方案,现在我需要将其发送到 Java 前端。从 C++ 和 Java 的角度来看,我应该使用什么来通过 HTTP(只是内部网络)发送这些序列化的东西?

PS. Another guy tries to speed-up our gSoap solution, I'm interested in protocol buffers only.

附注。另一个人试图加速我们的 gSoap 解决方案,我只对协议缓冲区感兴趣。

采纳答案by Sean Owen

You can certainly send even a binary payload with an HTTP request, or in an HTTP response. Just write the bytes of the protocol buffer directly into the request/response, and make sure to set the content type to "application/octet-stream". The client, and server, should be able to take care of the rest easily. I don't think you need anything more special than that on either end.

您当然可以使用 HTTP 请求或在 HTTP 响应中发送二进制负载。只需将协议缓冲区的字节直接写入请求/响应中,并确保将内容类型设置为“application/octet-stream”。客户端和服务器应该能够轻松处理其余的事情。我不认为你需要任何比这更特别的东西。

回答by Matthew

To my knowledge protocol buffers support is available in both C++ and Java, you should be able to exchange protocol buffer serialized data between both systems.

据我所知,C++ 和 Java 都支持协议缓冲区,您应该能够在两个系统之间交换协议缓冲区序列化数据。

That said, it seems your real question is "How do I send stuff over HTTP between a C++ backend and Java client"

也就是说,您真正的问题似乎是“如何在 C++ 后端和 Java 客户端之间通过 HTTP 发送内容”

It sound like you need to learn how to use gSOAP, read the docs.

听起来您需要学习如何使用 gSOAP,阅读文档

Alternatively you could host a RESTful web server from your C++ app: Look at this: https://stackoverflow.com/questions/298113/how-can-i-implement-a-restful-webservice-using-c++

或者,您可以从您的 C++ 应用程序托管一个 RESTful Web 服务器:看看这个:https: //stackoverflow.com/questions/298113/how-can-i-implement-a-restful-webservice-using-c++

Next you would need to access the data hosted on your new C++ RESTful server: Look at this: Rest clients for Java?

接下来,您需要访问托管在新 C++ RESTful 服务器上的数据:看看这个:Rest clients for Java?

回答by Vijay Mathew

You can serialize/de-serialize protobuf encoded data to/from strings. Send the serialized string as the body of an HTTP POST to Java and de-serialize it. That is one approach. Another way is to make use of the protobuf Service interface. Protobuf allows you to define a service interface in a .proto file and the protocol buffer compiler will generate service interface code and stubs in your chosen language. You only need to implement the protobuf::RpcChannel and protobuf::RpcController classes to get a complete RPC framework. Probably you can write an HTTP wrapper for these classes. See the following links for more information:

您可以将 protobuf 编码的数据序列化/反序列化到/从字符串。将序列化的字符串作为 HTTP POST 的正文发送到 Java 并对其进行反序列化。这是一种方法。另一种方法是利用 protobuf Service 接口。Protobuf 允许您在 .proto 文件中定义服务接口,协议缓冲区编译器将以您选择的语言生成服务接口代码和存根。你只需要实现 protobuf::RpcChannel 和 protobuf::RpcController 类就可以得到一个完整的 RPC 框架。也许您可以为这些类编写一个 HTTP 包装器。有关更多信息,请参阅以下链接:

http://code.google.com/apis/protocolbuffers/docs/proto.html#serviceshttp://code.google.com/apis/protocolbuffers/docs/reference/cpp-generated.html#servicehttp://code.google.com/apis/protocolbuffers/docs/reference/cpp/google.protobuf.service.html

http://code.google.com/apis/protocolbuffers/docs/proto.html#services http://code.google.com/apis/protocolbuffers/docs/reference/cpp-generated.html#service http:// code.google.com/apis/protocolbuffers/docs/reference/cpp/google.protobuf.service.html

回答by ZZ Coder

ProtoBuf is a binary protocol. It doesn't mix well with SOAP. I suggest you either stick with gSOAP or convert to ProtoBuf entirely.

ProtoBuf 是一个二进制协议。它不能与 SOAP 很好地混合。我建议您要么坚持使用 gSOAP,要么完全转换为 ProtoBuf。

With protoBuf, you define your protocol in a special format like this,

使用 protoBuf,你可以用这样的特殊格式定义你的协议,

message Product {
  required string id = 1;
  required string description = 2;
  required int32 quantity = 3;
  optional bool discontinued = 4;
}

The protoctool can generate code in C++/Java/Python so you can serialize it on one end and deserialize on another.

protoc工具可以在 C++/Java/Python 中生成代码,因此您可以在一端序列化它并在另一端反序列化。

As you can see, ProtoBuf is designed to serialize individual object. It doesn't provide all the facilities provided by SOAP, like headers. To get around this issue, we use ProtoBuf inside ProtoBuf. We define an Envelope like this,

如您所见,ProtoBuf 旨在序列化单个对象。它不提供 SOAP 提供的所有设施,如标头。为了解决这个问题,我们在 ProtoBuf 中使用了 ProtoBuf。我们像这样定义一个信封,

message Envelope {
  enum Type { 
    SEARCH = 1;
    SEARCH_RESPONSE = 2;
    RETRIEVE = 3;
    RETRIEVE_RESPONSE = 4; 
  }
  required Type type = 1;

  required bytes encodedMessage = 2;

  message Header {
    required string key = 1;
    required bytes value = 2;
  }    
  repeated Header headers = 3;
}

The encodedMessageis another serialized ProtoBuf message. All the stuff in SOAP header now goes to headers.

encodedMessage是另一个序列化的 ProtoBuf 消息。SOAP 标头中的所有内容现在都转到headers.

回答by rds

Google frontends prefer application/protobuf.

Google 前端更喜欢application/protobuf.

The ProtocolBufferModelof the Google API client uses application/x-protobuf.

Google API 客户端的ProtocolBufferModel使用application/x-protobuf.