Protocol Buffers学习笔记

本文介绍Protocol Buffers在Java中的应用,包括定义数据结构、编译生成Java代码及在网络通信中的实例。Protocol Buffers是Google提供的高效二进制数据格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Protocol Buffers学习笔记

1. 简介

Protocol Buffers是google发明的一种数据交换格式,独立于语言,独立于平台。与其他的数据交换格式有所不同,Protocol Buffers是一种二进制的格式,因此在网络传输的时候效率更高,相对于json它的文件体积更小,相对于xml,它的解析速度更快。

作为一种效率和兼容性都很优秀的二进制数据传输格式,可以用于网络传输、配置文件、数据存储等领域。

2. 在java中的使用

2.1 新建test.proto文件

syntax = "proto2";

package tutorial;

option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phones = 4;
}

message AddressBook {
  repeated Person people = 1;
}

2.2 编译protot文件

下载编译器:

https://github.com/google/protobuf/releases

一般在每个发布版本的protoc-{版本号}-{平台}.zip压缩包里面包含对应的编译器。由于LZ用的是win系统,故下载了protoc-3.6.1-win32.zip,解压之后在bin目录下会看到一个proto.exe文件。在proto.exe所在的目录下面就可以使用protoc命令进行编译了。

protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/addressbook.proto

实例:

C:\Users\wangjun\Downloads>protoc -I=./ --java_out=./ ./test.proto

编译成功后会在当前目录下生成对用的java文件:

C:\Users\wangjun\Downloads\com\example\tutorial\AddressBookProtos.java

2.3 在项目中使用编译好的java文件

新建一个java工程,目录结构如下:

com
    ├─example
    │  └─tutorial
    │          AddressBookProtos.java
    │
    └─wangjun
        └─protoc
                Client.java
                Constants.java
                Server.java

导入protobuf-java-3.5.1.jar(LZ使用的是此版本,读者请自行下载适用版本)。此包可以对ProtocolBuffer消息进行序列化和反序列化。

编写实例代码:

1)Constants.java
package com.wangjun.protoc;

public class Constants {

    public static final int PORT = 9988;
}
2)Server.java
package com.wangjun.protoc;

import java.net.ServerSocket;
import java.net.Socket;

import com.example.tutorial.AddressBookProtos;
import com.google.protobuf.ByteString;

public class Server {

    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(Constants.PORT);
        System.out.println("server started...");
        Socket socket = ss.accept();
        System.out.println("a client connected!");
        //从输入流中解析出Person对象
        AddressBookProtos.Person person = AddressBookProtos.Person.parseFrom(ByteString.readFrom(socket.getInputStream()));
        if(person != null) {
            System.out.println("server received data:\n" + person.toString());
        }
    }

}
3)Client.java
package com.wangjun.protoc;

import java.io.OutputStream;
import java.net.Socket;

import com.example.tutorial.AddressBookProtos;

public class Client {
    public static void main(String[] args) throws Exception {

        Socket socket = new Socket("localhost", Constants.PORT);
        //构造一个Person对象
        AddressBookProtos.Person person = AddressBookProtos.Person.newBuilder().setName("zhangsan")
                .setId(20).setEmail("zhangsan@xxxmail.com").build();
        OutputStream os = socket.getOutputStream();
        //将Person对象写到输出流中
        os.write(person.toByteArray());
        os.flush();
        //这里注意一定要关闭流,否则服务端会报错
        os.close();
        System.out.println("client send person");
    }

}

2.4 测试

先运行server。

server started...

再运行client。

client send person

server端已经接收到消息。

server started...
a client connected!
server received data:
name: "zhangsan"
id: 20
email: "zhangsan@xxxmail.com"

至此java中使用ProtocolBuffer的实例就完成了。

参考:

官方文档 https://developers.google.com/protocol-buffers/docs/javatutorial

Java中使用Protocol Buffer https://blog.youkuaiyun.com/yubo_725/article/details/52219489

来源:https://github.com/google/protobuf/releases protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台。google 提供了多种语言的实现,如:java、c#、c++、javascript、go 、python、ruby和php等,每一种实现都包含了相应语言的编译器以及库文件。由于它是一种二进制的格式,比使用 xml 进行数据交换快许多。可以把它用于分布式应用之间的数据通信或者异构环境下的数据交换。作为一种效率和兼容性都很优秀的二进制数据传输格式,可以用于诸如网络传输、配置文件、数据存储等诸多领域。 本资源包含: protobuf-all-3.6.0.tar.gz 8.25 MB protobuf-all-3.6.0.zip 4.25 MB protobuf-cpp-3.6.0.tar.gz 5.18 MB protobuf-cpp-3.6.0.zip 4.57 MB protobuf-csharp-3.6.0.tar.gz 5.66 MB protobuf-csharp-3.6.0.zip 4.7 MB protobuf-java-3.6.0.tar.gz 5.86 MB protobuf-java-3.6.0.zip 4.4 MB protobuf-js-3.6.0.tar.gz 5.43 MB protobuf-js-3.6.0.zip 4.59 MB protobuf-objectivec-3.6.0.tar.gz 5.69 MB protobuf-objectivec-3.6.0.zip 4.6 MB protobuf-php-3.6.0.tar.gz 5.64 MB protobuf-php-3.6.0.zip 4.53 MB protobuf-python-3.6.0.tar.gz 5.57 MB protobuf-python-3.6.0.zip 4.52 MB protobuf-ruby-3.6.0.tar.gz 5.5 MB protobuf-ruby-3.6.0.zip 1.46 MB protoc-3.6.0-linux-aarch_64.zip 1.31 MB protoc-3.6.0-linux-x86_32.zip 1.36 MB protoc-3.6.0-linux-x86_64.zip 2.44 MB protoc-3.6.0-osx-x86_32.zip 2.39 MB protoc-3.6.0-osx-x86_64.zip 984 KB protoc-3.6.0-win32.zip Source code (zip) Source code (tar.gz)
### C++ gRPC 学习教程和示例代码 #### 一、环境搭建 为了能够顺利运行C++中的gRPC程序,需要先完成开发环境的配置。这通常涉及到安装必要的依赖库以及设置编译工具链。具体来说,可以按照官方文档指导来准备所需的软件包,比如Protocol Buffers编译器`protoc`及其对应的C++插件,还有gRPC核心库等[^1]。 ```bash sudo apt-get install build-essential autoconf libtool pkg-config git clone https://github.com/protocolbuffers/protobuf.git cd protobuf ./autogen.sh && ./configure && make -j$(nproc) && sudo make install ``` 对于gRPC本身,则可以通过如下命令获取并构建: ```bash git clone --recurse-submodules -b v1.48.x https://github.com/grpc/grpc cd grpc mkdir -p cmake/build && cd cmake/build cmake ../.. make -j$(nproc) sudo make install ``` #### 二、创建Protobuf文件 接下来就是定义服务接口,在`.proto`文件里描述消息结构和服务方法。这里给出一个简单的例子——HelloWorld服务,其中包含了一个名为SayHello的方法用于接收请求并向客户端返回响应信息。 ```protobuf syntax = "proto3"; option cc_enable_arenas = true; package helloworld; // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} } // The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings message HelloReply { string message = 1; } ``` 保存上述内容到`helloworld.proto`之后,利用之前提到过的`protoc`命令行工具将其转换成相应的头文件与源码文件以便后续使用。 #### 三、编写服务器端逻辑 基于前面所生成的服务类模板,现在可以在项目中实现具体的业务处理函数了。下面展示的是如何继承自动生成出来的基类,并重写虚函数以提供实际功能的部分代码片段。 ```cpp #include <iostream> #include "helloworld.grpc.pb.h" using namespace std; using grpc::Server; using grpc::ServerBuilder; using grpc::ServerContext; using grpc::Status; using helloworld::Greeter; using helloworld::HelloReply; using helloworld::HelloRequest; class GreeterServiceImpl final : public Greeter::Service { public: Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override { string prefix("Hello "); reply->set_message(prefix + request->name()); return Status::OK; } }; ``` 这段代码实现了当接收到客户端发起的调用时会执行的操作:拼接字符串形成回复文本并通过参数传递给对方。 #### 四、启动监听进程 有了完整的协议声明加上对应的功能模块后就可以着手建立网络连接等待远端访问啦! ```cpp void RunServer() { string server_address("0.0.0.0:50051"); GreeterServiceImpl service; ServerBuilder builder; // Listen on the given address without any authentication mechanism. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); // Register "service" as the instance through which we'll communicate with // clients. In this case it corresponds to an *synchronous* service. builder.RegisterService(&service); // Finally assemble the server. unique_ptr<Server> server(builder.BuildAndStart()); cout << "Server listening on " << server_address << endl; // Wait for the server to shutdown. Note that some other thread must be // responsible for shutting down the server for this call to ever return. server->Wait(); } ``` 此部分负责初始化HTTP/2传输层设施并将指定地址开放出去供外界联系;同时注册好先前定义好的处理器对象使得每次有新链接进来都能找到合适的地方去解析数据流进而触发相应动作。 #### 五、客户端编程指南 最后一步自然是要让应用程序具备主动出击的能力咯~即构造出能向远程主机发出请求的消息体格式化为wire format再经由socket发送过去得到回应为止的过程。 ```cpp void RunClient() { string target_str("localhost:50051"); // Instantiate the client. It requires a channel, out of which the actual RPCs // are created. This channel models a connection to an endpoint specified by // the argument; you may provide extra arguments to indicate credentials, // compression Level etc. shared_ptr<Channel> channel = CreateChannel(target_str, InsecureChannelCredentials()); // Stub acts like a proxy object representing remote side entity. unique_ptr<Greeter::Stub> stub(Greeter::NewStub(channel)); // Data we are sending to the server. HelloRequest request; request.set_name("you"); // Container for the data we expect from the server. HelloReply reply; // Context for the client. It could be used to convey extra information to // the server and/or tweak certain RPC behaviors. ClientContext context; // The actual RPC. Status status = stub->SayHello(&context, request, &reply); // Act upon its status. if (status.ok()) { cout << "Greeter received: " << reply.message() << endl; } else { cerr << status.error_code() << ": " << status.error_message() << endl; } } ``` 以上便是整个流程的大致介绍,当然这只是冰山一角而已,更多高级特性和最佳实践还需要读者朋友们自行探索学习哦~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值