须要如下软件反对:

  • php-grpc扩大
  • php-protobuf扩大
  • protoc命令行工具
  • grpc_php_plugin模板生成工具
  • swoole开启openssl,http2

一、装置

1. 编译装置 grpc 扩大 (留神 grpc 版本对应的 protobuf 版本号)

2. 下载解压缩

wget http://pecl.php.net/get/grpc-1.30.0.tgztar xvf grpc-1.30.0.tgzcd grpc-1.30.0

编译装置

/usr/local/php/72/bin/phpize./configure --with-php-config=/usr/local/php/72/bin/php-configmake && make install

配置php.ini

vim /usr/local/php/etc/php.iniextension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/grpc.so

重启php服务

/etc/init.d/php-fpm reload

2. 编译装置 protobuf 扩大

下载解压缩protobuf

wget http://pecl.php.net/get/protobuf-3.12.2.tgztar xvf protobuf-3.12.2.tgzcd protobuf-3.12.2

编译装置

/usr/local/php/72/bin/phpize./configure --with-php-config=/usr/local/php/72/bin/php-configmake && make install

配置php.ini

vim /usr/local/php/etc/php.iniextension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/protobuf.so

重启php服务

/etc/init.d/php-fpm reload

3. 从新编译装置swoole

 ./configure --enable-openssl --with-openssl-dir=/usr/local/opt/openssl  --enable-http2 --with-php-config=/usr/local/php/bin/php-config

4. grpc_php_plugin模板生成工具(用源码装置会有翻墙问题,间接搜编译好的文件)

5. 最初一步,测试

mkdir grpc目录

创立grpc.proto文件

syntax = "proto3";package grpc;service Hi {    rpc sayHello (HiUser) returns (HiReply) {    }}message HiUser {    string name = 1;    int32 sex = 2;}message HiReply {    string message = 1;    HiUser user = 2;}

执行生成命令:

protoc -I=. grpc.proto --proto_path=grpc/ --php_out=grpc/ --grpc_out=grpc/ --plugin=protoc-gen-grpc=/usr/bin/grpc_php_plugin

最初,tree grpc,看成果,胜利啦!

grpc├── GPBMetadata│   └── Grpc.php└── Grpc    ├── HiClient.php    ├── HiReply.php    └── HiUser.php

二、gRPC压测工具

下载安装:ghz工具包:https://github.com/bojand/ghz/releases

压测命令:

ghz -n 100 -c 10 --insecure --proto ./api.proto --call api.HelloWorld.Say  -d '[{"Message":"hello"},{"Message":"hi"}]' 127.0.0.1:9092

看成果:

Summary:  Count:    10000  Total:    1.80 s  Slowest:    39.19 ms  Fastest:    0.52 ms  Average:    11.16 ms  Requests/sec:    5545.06Response time histogram:  0.515  [1]    |  4.383  [610]  |∎∎∎∎∎∎∎  8.250  [1881] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎  12.118 [3328] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎  15.985 [2895] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎  19.853 [1145] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎  23.720 [93]   |∎  27.588 [8]    |  31.455 [17]   |  35.323 [14]   |  39.191 [8]    |Latency distribution:  10 % in 5.38 ms  25 % in 8.27 ms  50 % in 11.23 ms  75 % in 13.87 ms  90 % in 16.52 ms  95 % in 17.75 ms  99 % in 20.77 msStatus code distribution:  [OK]   10000 responses