php安装 protobuf2.6.1扩展

原创
2018/12/26 11:28
阅读数 6.6K

首次实际项目中用到protobuf,找了下资料,弄了下环境并跑通,做下记录以免忘记

由于对接方使用的是proto2.4版本,所以也只能使用版本2的了。注意下,大版本间似乎是不支持的,我们试过安装版本3,但跟第三方用的版本2死活参数不匹配。(版本2官方没有提供php支持源码,所以找的是第三方支持的源码。版本3的官方已经提供了对php支持源码。)

--------protobuf是什么--------

官方解释:

What are protocol buffers?

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.

--------安装--------

1、protobuf安装:

编译源码地址 -- https://github.com/protocolbuffers/protobuf/releases?after=v3.0.0-alpha-1

wget https://github.com/protocolbuffers/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz

tar zxvf protobuf-2.6.1.tar.gz

cd protobuf-2.6.1

./configure --prefix=/usr/local/protobuf

make && make install

#export PATH=/usr/local/protobuf/bin:$PATH //按默认的可不执行

//验证是否安装ok,显示相应版本号
protoc --version

2、php的protobuf扩展安装:

wget https://github.com/allegro/php-protobuf/archive/master.zip
 
unzip master.zip
 
cd php-protobuf-master
 
#yum install php-devel(安装依赖--可跳过)
 
phpize
 
./configure --with-php-config=/usr/local/php/bin/php-config 
 
make && make install
 
//将在/your folder/php-protobuf-master/module/目录下生成protobuf.so文件
//然后在php.ini里面加一下extension = "protobuf.so",再重启php与nginx即可。

3、composer--为了后面可以执行.proto文件自动生成代码

//进入到php-protobuf-master目录
cd /your folder/php-protobuf-master

curl -s http://getcomposer.org/installer | php

php composer.phar install

--------protobuf使用--------

编译生成代码(一个message结构会生成一个包装类)

php ./php-protobuf-master/protoc-gen-php.php test_km.proto
//将会生成代码目录结构:/xxx/xxx/*

将生成的代码完整拷贝到自己的项目中

代码中类会继承ProtobufMessage类,它是protobuf.so中自动加载

------------------------------------------

这样就可以在项目里使用protobuf进行序列化/反序列化操作了

附:

protocol buffers官方:https://developers.google.com/protocol-buffers/?hl=zh-cn

protobuf github:https://github.com/protocolbuffers/protobuf

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部