linux下安装protobuf及其使用

linux下安装protobuf及在python与php上的应用

转:https://segmentfault.com/a/1190000017144329?utm_source=tag-newest

下载解压源文件

github上的版本发布地址

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

下载&解压&进入源码目录

当前下载protobuf2的最后一个版本
wget https://github.com/google/protobuf/archive/v2.6.1.zip

unzip v2.6.1.zip -d ./

cd protobuf-2.6.1
请根据自己的需求下载对应的版本,当前操作演示版本为protobuf2,及.proto文件中 syntax = "proto2";开头的

安装依赖

sudo apt-get install autoconf automake libtool
其他非debian发行版的机器安装对应的依赖扩展即可

生成configure文件

./autogen.sh
如果无法连接google网站

修改

curl http://googletest.googlecode.com/files/gtest-1.5.0.tar.bz2 | tar jx
mv gtest-1.5.0 gtest

 wget https://github.com/google/googletest/archive/release-1.5.0.tar.gz
 tar xzvf release-1.5.0.tar.gz
 mv googletest-release-1.5.0 gtest
也可由手动删除google的连接,直接下载文件并解压

进行configure

查看帮助

./configure -h

指定安装目录

./configure prefix=/opt/protobuf2

安装

make && make install
权限不够的话,要使用sudo

建立命令软连接

ln -s /opt/protobuf2/bin/protoc /usr/local/bin/protoc
建立软连接,就可以直接使用protoc命令了

在Python中的应用

使用protobuf命令生成.py文件

protoc --proto_path=/home/zhangsan/protobuf --python_out=/home/zhangsan/python /home/zhangsan/protobuf/test.proto

参数说明

参数proto_path指明.proto文件所在位置,如果不写,会提示以下内容:

/home/zhangsan/protobuf/test.proto: File does not reside within any path specified using --proto_path (or -I). You must specify a --proto_path which encompasses this file. Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).
所以务必写上.proto文件所在目录路径

python_out即.py生成的目录

最后一个参数即为.proto文件的绝对路径

生成文件的优化修改

这行代码如果使用IDE,提示无相关的类库的话,直接删掉/注释即可

from google.protobuf import symbol_database as _symbol_database

生成的.py文件中,一下内容可能报类缺少之类的问题,也可删掉、注释

_sym_db.RegisterMessage

文件中包含的空的数组 oneofs也可能给导致异常,删掉、注释即可

oneofs=[],

最后调整一下文件的代码风格即可使用

应用(假设是根据用户信息获取用户的博客文章)

request = DemoRequest()

request.user.id = 1
request.user.name = 'zhangsan'
body = request.SerializeToString()
# body 既可以使用http发送

response = http_func_name(body)

message = response.getBody()
message.ParseFromString()

# message 即为对应的数据对象
print(message.blog.id)
print(message.blog.title)

在PHP中的应用

下载allegro/php-protobuf

  • github版本发布页面
https://github.com/allegro/php-protobuf/releases
  • 下载最新发布的版本(2018.11.26版本为v0.12.3)
wget https://github.com/allegro/php-protobuf/archive/v0.12.3.zip
  • 解压到当前目录
unzip v0.12.3.zip -d ./

安装php扩展

cd php-protobuf-0.12.3
phpize
./configure
make
make install

配置php.ini

extension=protobuf.so
在扩展配置部分加入以上内容,执行 php -ir | grep protobuf输出protobuf扩展信息即为正常,如:
protobuf
PWD => /home/qingliu/soft/php-protobuf-0.12.3
$_SERVER['PWD'] => /home/qingliu/soft/php-protobuf-0.12.3

在源码目录安装allegro/php-protobuf依赖的php类库

composer install 安装即可

使用protobuf生成.php文件

protoc --proto_path=/home/zhangsan/protobuf --plugin=protoc-gen-allegrophp=protoc-gen-php.php --allegrophp_out=/home/zhangsan/php /home/zhangsan/protobuf/test.proto
请在allegro/php-protobuf的源码目录执行,正常执行完上述命令后,可以在 /home/zhangsan/php目录中看到生成的php文件,php无需修改,即可使用,如不符合自己的项目规范,可以自行调节

IDE Helper

使用这个composer包就行啦

composer require ruoge3s/protobuf-message-helper
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值