简体   繁体   English

Google协议缓冲区和HTTP

[英]Google Protocol Buffers and HTTP

I'm refactoring legacy C++ system to SOA using gSoap. 我正在使用gSoap将遗留的C ++系统重构为SOA。 We have some performance issues (very big XMLs) so my lead asked me to take a look at protocol buffers. 我们有一些性能问题(非常大的XML),所以我的带领让我看看协议缓冲区。 I did, and it looks very cool (We need C++ and Java support). 我做了,它看起来很酷(我们需要C ++和Java支持)。 However protocol buffers are solution just for serialization and now I need to send it to Java front-end. 但协议缓冲区只是用于序列化的解决方案,现在我需要将它发送到Java前端。 What should I use from C++ and Java perspective to send those serialized stuff over HTTP (just internal network)? 我应该从C ++和Java的角度使用什么来通过HTTP(只是内部网络)发送那些序列化的东西?

PS. PS。 Another guy tries to speed-up our gSoap solution, I'm interested in protocol buffers only. 另一个人试图加速我们的gSoap解决方案,我只对协议缓冲区感兴趣。

You can certainly send even a binary payload with an HTTP request, or in an HTTP response. 您当然可以使用HTTP请求或HTTP响应发送二进制有效负载。 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". 只需将协议缓冲区的字节直接写入请求/响应,并确保将内容类型设置为“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. 我认为你不需要任何比这更特别的东西。

ProtoBuf is a binary protocol. ProtoBuf是一种二进制协议。 It doesn't mix well with SOAP. 它与SOAP混合不好。 I suggest you either stick with gSOAP or convert to ProtoBuf entirely. 我建议您坚持使用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 protoc tool 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. 如您所见,ProtoBuf旨在序列化单个对象。 It doesn't provide all the facilities provided by SOAP, like headers. 它不提供SOAP提供的所有功能,如标头。 To get around this issue, we use ProtoBuf inside ProtoBuf. 为了解决这个问题,我们在ProtoBuf中使用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. encodedMessage是另一个序列化的ProtoBuf消息。 All the stuff in SOAP header now goes to headers . SOAP标头中的所有内容现在都转到headers

Google frontends prefer application/protobuf . Google前端更喜欢application/protobuf

The ProtocolBufferModel of the Google API client uses application/x-protobuf . Google API客户端的ProtocolBufferModel使用application/x-protobuf

You can serialize/de-serialize protobuf encoded data to/from strings. 您可以将protobuf编码数据序列化/反序列化为字符串。 Send the serialized string as the body of an HTTP POST to Java and de-serialize it. 将序列化字符串作为HTTP POST的主体发送到Java并对其进行反序列化。 That is one approach. 这是一种方法。 Another way is to make use of the protobuf Service interface. 另一种方法是使用protobuf服务接口。 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. Protobuf允许您在.proto文件中定义服务接口,协议缓冲区编译器将以您选择的语言生成服务接口代码和存根。 You only need to implement the protobuf::RpcChannel and protobuf::RpcController classes to get a complete RPC framework. 您只需要实现protobuf :: RpcChannel和protobuf :: RpcController类来获得完整的RPC框架。 Probably you can write an HTTP wrapper for these classes. 您可以为这些类编写HTTP包装器。 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 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

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服务器上托管的数据:看看这个: 用于Java的Rest客户端?

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

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