php grpc中文文檔和實例


2020年10月21日15:23:53

官方文檔

https://www.grpc.io/docs/languages/php/quickstart/

先決條件

  • PHP 5.5或更高版本,7.0或更高版本
  • PECL
  • compsoer
  • PHPUnit(可選)

在Ubuntu / Debian上安裝PHP和PECL:

對於PHP5:

$ sudo apt-get install php5 php5-dev php-pear phpunit

對於PHP7:

$ sudo apt-get install php7.0 php7.0-dev php-pear phpunit

要么

$ sudo apt-get install php php-dev php-pear phpunit

在CentOS / RHEL 7上安裝PHP和PECL:

$ sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
$ sudo yum install php56w php56w-devel php-pear phpunit gcc zlib-devel

在Mac上安裝PHP和PECL:

$ brew install homebrew/php/php56-grpc
$ curl -O http://pear.php.net/go-pear.phar
$ sudo php -d detect_unicode=0 go-pear.phar

安裝Composer(Linux或Mac):

$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer

安裝PHPUnit(Linux或Mac):

$ wget https://phar.phpunit.de/phpunit-old.phar
$ chmod +x phpunit-old.phar
$ sudo mv phpunit-old.phar /usr/bin/phpunit

安裝gRPC PHP擴展

有兩種安裝gRPC PHP擴展的方法:

  • pecl
  • build from source

使用PECL

sudo pecl install grpc

或特定版本

sudo pecl install grpc-1.7.0
 

警告

不幸的是,此步驟不適用於CentOS / RHEL6。請按照以下說明從源代碼編譯PECL擴展。
在Windows上安裝

您可以從PECL網站下載預編譯的gRPC擴展

使用gRPC C核心庫從源代碼構建

在給定的發行標簽處克隆該存儲庫

$ git clone -b v1.32.0 https://github.com/grpc/grpc
構建並安裝gRPC C核心庫
$ cd grpc
$ git submodule update --init
$ make
$ sudo make install
構建並安裝gRPC PHP擴展

編譯gRPC PHP擴展

$ cd grpc/src/php/ext/grpc
$ phpize
$ ./configure
$ make
$ sudo make install

這會將gRPC PHP擴展編譯並安裝到標准PHP擴展目錄中。您應該能夠在安裝了PHP擴展的情況下運行單元測試。

更新php.ini

安裝GRPC擴展后,請確保您此行添加到您的php.ini文件(例如/etc/php5/cli/php.ini, /etc/php5/apache2/php.ini/usr/local/etc/php/5.6/php.ini),這取決於您的PHP安裝。

extension=grpc.so

將gRPC PHP庫添加為Composer依賴項

您需要將此添加到項目的composer.json文件中。

  "require": {
    "grpc/grpc": "v1.7.0"
  }

要使用.proto文件中生成的存根代碼運行測試,您還需要composerprotoc二進制文件。您可以在下面找到如何獲得這些信息。

為Mac OS X和Linux安裝其他先決條件

  • protoc: protobuf compiler
  • protobuf.so: protobuf runtime library
  • grpc_php_plugin: Generates PHP gRPC service interface out of Protobuf IDL

安裝Protobuf編譯器

如果尚未安裝,則需要protoc為當前gRPC版本安裝protobuf編譯器 3.4.0+版本(越新越好)。如果已經安裝,請確保protobuf版本與所安裝的grpc版本兼容。如果從源代碼構建grpc.so,則可以檢查package.xml文件中的grpc版本。

下表列出了grpc和protobuf版本之間的兼容性:

grpc 原蟲
v1.0.0 3.0.0(GA)
v1.0.1 3.0.2
v1.1.0 3.1.0
v1.2.0 3.2.0
v1.2.0 3.2.0
v1.3.4 3.3.0
v1.3.5 3.2.0
v1.4.0 3.3.0
v1.6.0 3.4.0

 

 

 

 

 

 

 

 

 

 

 

 

 

 

如果protoc尚未安裝,則可以protoc協議緩沖區GitHub存儲庫下載二進制文件 然后解壓縮該文件,並更新環境變量PATH以包含protoc二進制文件的路徑(/ protobuf / releases)。然后解壓縮該文件,並更新環境變量PATH以包括協議二進制文件的路徑。

如果確實必須protoc從源代碼進行編譯,則可以運行以下命令,但是這樣做很冒險,因為沒有簡便的方法可以將其卸載/升級到較新的版本。

$ cd grpc/third_party/protobuf
$ ./autogen.sh && ./configure && make
$ sudo make install

Protobuf運行時庫

有兩個protobuf運行時庫可供選擇。就提供的API而言,它們是相同的。C實現提供更好的性能,而本機實現更易於安裝。確保已安裝的protobuf版本與grpc版本兼容。

C實現(以獲得更好的性能)
$ sudo pecl install protobuf

或特定版本

$ sudo pecl install protobuf-3.4.0

protobuf的擴展是通過添加此行到您的安裝,更新后的php.iniphp.ini文件(例如/etc/php5/cli/php.ini, /etc/php5/apache2/php.ini/usr/local/etc/php/5.6/php.ini),這取決於您的PHP安裝。

extension=protobuf.so
PHP實現(為了更容易安裝)

將此添加到您的composer.json文件:

  "require": {
    "google/protobuf": "^v3.3.0"
  }

PHP Protoc插件

您需要使用gRPC PHP protoc插件來生成客戶端存根類。它可以根據.proto服務定義生成服務器和客戶端代碼。

make從此倉庫的根目錄運行時,它應該已經被編譯插件可以在bins/opt目錄中找到我們計划將來提供一種更好的方式來下載和安裝插件。

您還可以通過運行以下命令來構建gRPC PHP protoc插件:

$ git clone -b v1.32.0 https://github.com/grpc/grpc
$ cd grpc
$ git submodule update --init
$ make grpc_php_plugin

插件可能會使用新的protobuf版本的新功能,因此也請確保安裝的protobuf版本與您構建此插件的grpc版本兼容。

下載示例

您需要示例代碼的本地副本才能完成此快速入門。從我們的GitHub存儲庫下載示例代碼(以下命令將克隆整個存儲庫,但是您僅需要有關此快速入門和其他教程的示例):

請注意,當前,您只能在PHP中為gRPC服務創建客戶端。使用另一種語言創建gRPC服務器。

# Clone the repository to get the example code:
$ git clone -b v1.32.0 https://github.com/grpc/grpc
# Build grpc_php_plugin to generate proto files if not build before
$ cd grpc && git submodule update --init && make grpc_php_plugin
# Navigate to the "hello, world" PHP example:
$ cd examples/php
$ ./greeter_proto_gen.sh
$ composer install

運行gRPC應用程序

examples/node目錄:

  1. 運行服務器:

    $ npm install
    $ cd dynamic_codegen
    $ node greeter_server.js
    
  2. 在另一個終端的examples/php目錄中,運行客戶端:

    $ ./run_greeter_client.sh
    

恭喜你!您剛剛使用gRPC運行了客戶端服務器應用程序。

更新gRPC服務

現在讓我們看一下如何使用服務器上的附加方法更新應用程序,以供客戶端調用。我們的gRPC服務是使用協議緩沖區定義的;您可以.proto 在《基礎教程》中找到更多有關如何在文件中定義服務的信息現在,您只需要知道服務器和客戶端“存根”都有一個SayHelloRPC方法,該方法HelloRequest從客戶端獲取 參數並HelloResponse從服務器返回a ,並且該方法的定義如下:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

讓我們對其進行更新,以便該Greeter服務具有兩種方法。examples/protos/helloworld.proto使用SayHelloAgain 具有相同請求和響應類型的新方法來編輯 和更新它

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Sends another greeting
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

記住要保存文件!

生成gRPC代碼

接下來,我們需要更新應用程序使用的gRPC代碼以使用新的服務定義。grpc根目錄:

$ protoc --proto_path=examples/protos \
  --php_out=examples/php \
  --grpc_out=examples/php \
  --plugin=protoc-gen-grpc=bins/opt/grpc_php_plugin \
  ./examples/protos/helloworld.proto

或在grpc/example/php目錄下運行幫助程序腳本(如果您通過源代碼構建grpc-php-plugin):

$ ./greeter_proto_gen.sh

這將重新生成protobuf文件,其中包含我們生成的客戶端類,以及用於填充,序列化和檢索我們的請求和響應類型的類。

更新並運行應用程序

現在,我們有了新生成的客戶代碼,但是仍然需要在示例應用程序的人工編寫部分中實現並調用新方法。

更新服務器

在同一目錄中,打開greeter_server.js像這樣實現新方法:

function sayHello(call, callback) {
  callback(null, {message: 'Hello ' + call.request.name});
}

function sayHelloAgain(call, callback) {
  callback(null, {message: 'Hello again, ' + call.request.name});
}

function main() {
  var server = new grpc.Server();
  server.addProtoService(hello_proto.Greeter.service,
                         {sayHello: sayHello, sayHelloAgain: sayHelloAgain});
  server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
  server.start();
}
...

更新客戶端

在同一目錄中,打開greeter_client.php像這樣調用新方法:

$request = new Helloworld\HelloRequest();
$request->setName($name);
list($reply, $status) = $client->SayHello($request)->wait();
$message = $reply->getMessage();
list($reply, $status) = $client->SayHelloAgain($request)->wait();
$message = $reply->getMessage();


運行

就像我們之前所做的那樣,從examples/node/dynamic_codegen目錄:

  1. 運行服務器:

    $ node greeter_server.js
    
  2. 在另一個終端的examples/php目錄中,運行客戶端:

    $ ./run_greeter_client.sh


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM