Google 协议缓冲区和 HTTP

发布于 2024-08-05 07:48:38 字数 248 浏览 6 评论 0 原文

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

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

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)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

预谋 2024-08-12 07:48:38

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

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.

慕烟庭风 2024-08-12 07:48:38

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

使用 protoBuf,您可以像这样以一种特殊的格式定义协议,

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

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

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

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;
}

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

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

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

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

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

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,

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 encodedMessage is another serialized ProtoBuf message. All the stuff in SOAP header now goes to headers.

假装爱人 2024-08-12 07:48:38

Google 前端更喜欢 application/protobuf

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

Google frontends prefer application/protobuf.

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

尾戒 2024-08-12 07:48:38

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

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

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:

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

冷月断魂刀 2024-08-12 07:48:38

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

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

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

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

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

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.

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

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

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++

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文