linux下安装protobuf及其使用

    xiaoxiao2022-07-07  230

    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)