mac環境下安裝grpc的PHP客戶端

前言

本身本地環境本來有個PHP7.1的版本,結果在裝grpc時一直提示沒有權限把grpc.so拷貝到PHP擴展目錄下,就從新裝了一次phpphp

這次安裝涉及的有:html

  • PHP
  • composer
  • pecl
  • grpc
  • php的grpc擴展
  • protobuf
  • php的protoc插件

安裝

1.覆蓋原來的php

# 安裝PHP
brew intall php

# 優先使用新安裝的PHP7.2,即配置環境變量,這一步要注意本身的PHP目錄,如下是個人目錄
# 個人shell使用的是zsh,因此導入到~/.zshrc下,要注意本身的shell是什麼,導入到本身使用的shell配置文件裏
echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.zshrc

# 使用配置文件,使其生效
source ~/.zshrc
# 查看PHP的版本是不是本身安裝的
php -v 
php-fpm -v

2.安裝grpc及其擴展

安裝grpcnode

brew install grpc

安裝PHP的grpc擴展git

git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc

cd grpc
git submodule update --init
make
sudo make install
#這一步我本地報錯找不到autolocal,查後須要安裝automake,用brew install automake便可

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

以上都OK的話,在phpinfo裏應該能夠看到grpc的信息了github

3.安裝protobuf

pecl install protobuf

安裝成功後,phpinfo裏會有protobuf的信息.shell

4.demo

下載examplenpm

$ # Clone the repository to get the example code:
$ git clone -b v1.24.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

我本地跑的是go的grpc服務端,因此不安裝服務端了。demo裏是用node作服務端。以下:composer

$ npm install
$ cd dynamic_codegen
$ node greeter_server.js

運行PHP的客戶端:curl

$ ./run_greeter_client.sh   

沒問題的話,在服務端會顯示:
$ 2019/10/18 16:03:50 Received: world

參考連接:
https://grpc.io/docs/quickstart/php/
go安裝grpc連接php-fpm

相關文章
相關標籤/搜索