mac环境下安装grpc的PHP客户端

前言

自己本地环境原本有个PHP7.1的版本,结果在装grpc时一直提示没有权限把grpc.so拷贝到PHP扩展目录下,就重新装了一次php

此次安装涉及的有:

  • 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及其扩展

安装grpc

brew install grpc 

安装PHP的grpc扩展

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的信息了

3.安装protobuf

pecl install protobuf

安装成功后,phpinfo里会有protobuf的信息.

4.demo

下载example

$ # 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做服务端。如下:

$ npm install
$ cd dynamic_codegen
$ node greeter_server.js	

运行PHP的客户端:

$ ./run_greeter_client.sh	

没问题的话,在服务端会显示:
$ 2019/10/18 16:03:50 Received: world

参考链接:
https://grpc.io/docs/quickstart/php/
go安装grpc链接

posted @ 2019-10-18 16:37  yeevan  阅读(1467)  评论(0编辑  收藏  举报