gRPC的使用(PHP客户端)

去PHP官网下载gRPC扩展和protobuf扩展,使用/program/php/bin/pecl安装好这两个扩展并启用
下载地址:https://pecl.php.net/package/gRPC
下载地址:https://pecl.php.net/package/protobuf
 
 
 
使用composer下载grpc和protobuf这两个库
[root@localhost ~]# /program/php/bin/php /usr/local/bin/composer require grpc/grpc
[root@localhost ~]# /program/php/bin/php /usr/local/bin/composer require google/protobuf


 
安装grpc_php_plugin
[root@localhost src]# git clone -b v1.56.0 https://github.com/grpc/grpc
[root@localhost src]# cd grpc
[root@localhost grpc]# git submodule update --init
[root@localhost grpc]# mkdir -p cmake/build
[root@localhost grpc]# cd cmake/build
[root@localhost build]# cmake ../..
[root@localhost build]# make grpc_php_plugin
[root@localhost build]# cp grpc_php_plugin /usr/local/bin/


 
本文主题是实现PHP版的gRPC客户端,编写*.proto文件以及启动gRPC服务端可参考文章『gRPC的使用(Go客户端)』,这里不再赘述。



使用grpc_php_plugin生成客户端需要的*.php文件
[root@localhost grpc]# protoc \
--proto_path=. \
--php_out=. \
--grpc_out=. \
--plugin=protoc-gen-grpc=/usr/local/bin/grpc_php_plugin \
proto/calculator.proto
[root@localhost grpc]# tree # 执行完上面的命令后会生成4个*.php文件,这些文件无需做任何修改,客户端直接require使用即可
.
├── GPBMetadata
│   └── Proto
│       └── Calculator.php
└── proto
    ├── CalculatorClient.php
    ├── calculator.proto
    ├── Nums.php
    └── Result.php
 
3 directories, 5 files
[root@localhost grpc]#


 
客户端代码

<?php
// 引入composer下载的库
require_once __DIR__ . '/composer/grpc/vendor/autoload.php';
require_once __DIR__ . '/composer/protobuf/vendor/autoload.php';

// 引入grpc_php_plugin生成的4个*.php文件
require_once __DIR__ . '/GPBMetadata/Proto/Calculator.php';
require_once __DIR__ . '/proto/CalculatorClient.php';
require_once __DIR__ . '/proto/Nums.php';
require_once __DIR__ . '/proto/Result.php';


$hostname = '192.168.***.***:10086'; // gRPC服务器主机地址,无需写tcp://协议

$client = new Proto\CalculatorClient($hostname, [
    'credentials' => Grpc\ChannelCredentials::createInsecure(),
]);

$nums = new Proto\Nums();
$result = new Proto\Result();


//========== 加法运算 ==========//
$nums->setNum1(5);
$nums->setNum2(2);
[$result, $status] = $client->Add($nums)->wait();
if ($status->code !== 0) {
    echo '调用Calculator.Add()失败:' . $status->details . PHP_EOL;
} else {
    $num1 = $nums->getNum1();
    $num2 = $nums->getNum2();
    $num = $result->getNum();
    echo "调用Calculator.Add()成功:{$num1} + {$num2} = {$num}" . PHP_EOL;
}


//========== 减法运算 ==========//
$nums->setNum1(5);
$nums->setNum2(2);
[$result, $status] = $client->Subtract($nums)->wait();
if ($status->code !== 0) {
    echo "调用Calculator.Subtract()失败:" . $status->details . PHP_EOL;
} else {
    $num1 = $nums->getNum1();
    $num2 = $nums->getNum2();
    $num = $result->getNum();
    echo "调用Calculator.Subtract()成功:{$num1} - {$num2} = {$num}" . PHP_EOL;
}


//========== 乘法运算 ==========//
$nums->setNum1(5);
$nums->setNum2(2);
[$result, $status] = $client->Multiply($nums)->wait();
if ($status->code !== 0) {
    echo "调用Calculator.Multiply()失败:" . $status->details . PHP_EOL;
} else {
    $num1 = $nums->getNum1();
    $num2 = $nums->getNum2();
    $num = $result->getNum();
    echo "调用Calculator.Multiply()成功:{$num1} * {$num2} = {$num}" . PHP_EOL;
}


//========== 除法运算(除数不为零) ==========//
$nums->setNum1(5);
$nums->setNum2(2);
[$result, $status] = $client->Divide($nums)->wait();
if ($status->code !== 0) {
    echo "调用Calculator.Divide()失败:" . $status->details . PHP_EOL;
} else {
    if ($result->getError() !== '') {
        echo "调用Calculator.Divide()出错:" . $result->getError() . PHP_EOL;
    } else {
        $num1 = $nums->getNum1();
        $num2 = $nums->getNum2();
        $num = $result->getNum();
        echo "调用Calculator.Divide()成功:{$num1} / {$num2} = {$num}" . PHP_EOL;
    }
}


//========== 除法运算(除数为零) ==========//
$nums->setNum1(5);
$nums->setNum2(0);
[$result, $status] = $client->Divide($nums)->wait();
if ($status->code !== 0) {
    echo "调用Calculator.Divide()失败:" . $status->details . PHP_EOL;
} else {
    if ($result->getError() !== '') {
        echo "调用Calculator.Divide()出错:" . $result->getError() . PHP_EOL;
    } else {
        $num1 = $nums->getNum1();
        $num2 = $nums->getNum2();
        $num = $result->getNum();
        echo "调用Calculator.Divide()成功:{$num1} / {$num2} = {$num}" . PHP_EOL;
    }
}


//========== 输出结果·开始 ==========//
// 调用Calculator.Add()成功:5 + 2 = 7
// 调用Calculator.Subtract()成功:5 - 2 = 3
// 调用Calculator.Multiply()成功:5 * 2 = 10
// 调用Calculator.Divide()成功:5 / 2 = 2.5
// 调用Calculator.Divide()出错:除数不能为零
//========== 输出结果·结束 ==========//

Copyright © 2024 码农人生. All Rights Reserved