링크 ux 에 protobuf 및 사용 설치

Liux 아래 protobuf 설치 및 python 및 phop 에서 의 응용
압축 해제 원본 파일 다운로드
github 버 전 게시 주소
https://github.com/protocolbuffers/protobuf/releases

다운로드&압축 풀기&원본 디 렉 터 리 진입
현재 protobuf 2 의 마지막 버 전 을 다운로드 합 니 다.
wget https://github.com/google/protobuf/archive/v2.6.1.zip

unzip v2.6.1.zip -d ./

cd protobuf-2.6.1

원 하 는 버 전 을 다운로드 하 십시오.현재 작업 프 리 젠 테 이 션 버 전 은 protobuf 2 및.proto 파일 에 있 습 니 다.
syntax = "proto2";시작의
설치 의존
sudo apt-get install autoconf automake libtool

다른 debian 발행 판 이 아 닌 기계 설치 에 대응 하 는 의존 확장 을 설치 하면 됩 니 다.
configure 파일 생 성
./autogen.sh

구 글 사 이 트 를 연결 할 수 없다 면
수정 하 다.
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

구 글 의 연결 을 수 동 으로 삭제 하고 파일 을 직접 다운로드 하여 압축 을 풀 수도 있 습 니 다.
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

매개 변수 설명
파라미터 protopath 는.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 버 전 v 0.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/phop-protobuf 가 의존 하 는 phop 라 이브 러 리 를 설치 합 니 다.
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/phop-protobuf 의 소스 디 렉 터 리 에서 실행 하 십시오.상기 명령 을 정상적으로 실행 한 후/home/zhangsan/phop 디 렉 터 리 에서 생 성 된 phop 파일 을 볼 수 있 습 니 다.phop 는 수정 하지 않 아 도 사용 할 수 있 습 니 다.자신의 프로젝트 규범 에 부합 되 지 않 으 면 스스로 조절 할 수 있 습 니 다.
IDE Helper
이 composer 가방 을 사용 하 시 면 됩 니 다.
composer require ruoge3s/protobuf-message-helper

좋은 웹페이지 즐겨찾기