linux下安裝protobuf及其使用

linux下安裝protobuf及在python與php上的應用

下載解壓源文件

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網站

修改php

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

python

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文件所在位置,若是不寫,會提示如下內容:linux

/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生成的目錄git

最後一個參數即爲.proto文件的絕對路徑github

生成文件的優化修改

這行代碼若是使用IDE,提示無相關的類庫的話,直接刪掉/註釋便可數組

from google.protobuf import symbol_database as _symbol_database

生成的.py文件中,一下內容可能報類缺乏之類的問題,也可刪掉、註釋composer

_sym_db.RegisterMessage

文件中包含的空的數組 oneofs也可能給致使異常,刪掉、註釋便可curl

oneofs=[],

最後調整一下文件的代碼風格便可使用ide

應用(假設是根據用戶信息獲取用戶的博客文章)

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
相關文章
相關標籤/搜索