Using ProtoBuf in PHP

Foreword

  RPC is an RPC framework led by Google and uses protobuf as the data transmission format. With the maturity of the gRPC framework and the increase in the number of users, protobuf is also receiving more and more attention to the data format used by the bottom layer. In other words, the relevant ProtoBuf introduction documents and usage materials are relatively few, so writing a brief text hopes to help some students in need.
  ProtoBuf (Google Protocol Buffer) is a sequence structured data format used by Google for data exchange. It has cross-platform, cross-language, and extensible features. It is of the type commonly used in XML and JSON, but has a smaller transmission volume and higher The encoding and decoding capabilities are particularly suitable for data storage, network data transmission and other fields that require high storage volume and real-time performance.
  At present, the latest version of the official ProtoBuf ProtoBuf3, already supports multiple languages: C ++ \ C # \ Go \ Java \ Python \ Ruby \ Object C \ Javascript \ PHP, and provides tools to easily generate ProtoBuf class libraries according to different languages.

  The following will use the Person data format as an example to describe how to use ProtoBuf3 in a PHP environment.

First, define a message type

Create a definition file for Person (with .proto as the suffix), for example, person.proto, the content of the file is as follows:

 

syntax="proto3";
package test;
message Person{
    string name=1;//姓名
    int32  age=2;//年龄
    bool sex=3;//性别
}

1. Syntax = "proto3": indicates that the proto3 format is used, or proto2 if it is not specified
2. package test: defines the package name as test, and when the class is generated, a directory is generated as test
3. message Person: message body content , Which defines the various fields

Second, generate the corresponding PHP class

After defining the format of Person, if the format does not generate the class library we need, it is meaningless. Google also provides a tool protoc to generate the class library we want.

1. Install protoc

Installation address: protobuf-php-3.5.1.tar.gz , currently the latest is 3.5.1
official release address: https://github.com/google/protobuf/releases/tag/v3.5.1
Unzip and install:

 

tar -zxvf protobuf-php-3.5.1.tar.gz
cd protobuf-3.5.1
./configure --prefix=/opt/soft/protobuf
make
make install

2. Generate a class library

 

/opt/soft/protobuf/bin/protoc --php_out=./ person.proto

After generation, the following files will be generated in the current directory:
GPBMetadata / Person.php
Test / Person.php

Third, use ProtoBuf in PHP

The use of ProtoBuf in PHP depends on a protobuf extension, and currently provides two ways to use it, 1: php c extension, 2: php lib extension package, both of which can be found in the download package just now.
In addition, you can also use composer to install the dependency extension: composer require google/protobuf
here I mainly use composer to install, it should help me generate autoload to
install the dependencies, we can start using protobuf in the php environment
1, serialization

 

<?php
include 'vendor/autoload.php';
include 'GPBMetadata/Person.php';
include 'Test/Person.php';

$person = new Test\Person();
$person->setName("lailaiji");
$person->setAge("28");
$person->setSex(true);
$data = $person->serializeToString();
file_put_contents('data.bin',$data);

After running, the resulting data.bin is only 14Byte
2. Deserialize

 

<?php
include 'vendor/autoload.php';
include 'GPBMetadata/Person.php';
include 'Test/Person.php';
$bindata = file_get_contents('./data.bin');
$person = new Test\Person();
$person->mergeFromString($bindata);
echo $person->getName();

Lailaiji can be output normally

Commonly used methods of PHP:

Serialization:
1, serializeToString: serialize into binary string
2, serializeToJsonString: serialize into JSON string
deserialization:
1, mergeFromString: binary string deserialization
2, mergeFromJsonString: Json string deserialization

.proto message parsing

1. Definition:
type variable name = position; for
example: int32 age = 1;
need to distinguish here, the number behind the variable name means the position of the variable content in the binary sequence instead of the value of the variable, the number must be the only and can not be repeated use.
2. Currently supported types:
double, float, int32, int64, uint32, uint64, sint32, sint64
fixed32, fixed64, fixed32, fixed64, bool, bytes

Performance Testing

https://github.com/eishay/jvm-serializers/wiki
1. Serial number + anti-serial number time:

image.png


2. Storage size:

 

image.png



Author: a virtual long road
link: https: //www.jianshu.com/p/ce098058edf0
Source: Jane books
are copyrighted by the author. For commercial reproduction, please contact the author for authorization, and for non-commercial reproduction, please indicate the source.

Published 23 original articles · won praise 2 · Views 5239

Guess you like

Origin blog.csdn.net/bianlitongcn/article/details/104990658