GO-Grpc微服务开发四 服务调用for php

栏目: PHP · 发布时间: 7年前

内容简介:修改php.ini文件 添加以mac为例:参考链接 https://www.jianshu.com/p/f8b789280df4

参考文档列表

  • PHP grpc官方文档 https://grpc.io/docs/quickstart/php.html
  • PHP grpc官方案例 https://grpc.io/docs/tutorials/basic/php.html
  • PHP grpc官方案例之github下载 https://github.com/grpc/grpc/tree/master/examples/php

一.环境搭建

1.安装grpc扩展

pecl install grpc

修改php.ini文件 添加 extension=grpc.so 通过 php -m | grep grpc 查看grpc.so是否安装成功

2.下载protoc命令

以mac为例:参考链接 https://www.jianshu.com/p/f8b789280df4

protoc --version 查看是否安装成功

3.安装PHP GRpc SDK

git clone -b v1.15.0 https://github.com/grpc/grpc

需要grpc-php-plugin来生成proto对应的 php 可执行文件。

cd grpc && git submodule update --init && make grpc_php_plugin

可与将 grpc_php_plugin 命令加入环境变量

二.通过protoc文件生成PHP文件

编写proto文件

syntax = "proto3";
package yunpian;


service YunPian {
    rpc Send(SendSmsReq) returns (SendSmsRes);
}

message SendSmsReq {
    string code = 1;
}

message SendSmsRes {
    int32 code = 1;
    string message = 2;
    map<string, string> data = 3; 
}

protoc命令生成PHP文件

参考链接: https://www.cnblogs.com/ghj1976/p/5435565.html

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

proto_path对应proto文件的位置,php_out指定生成PHP文件的目录,grpc_out和php_out指定相同的目录,plugin对应上面安装的grpc_php_plugin命令路径,最后跟上具体proto的文件地址。

三.PHP客户端调用微服务 例子1

proto文件编辑

syntax = "proto3";
package yunpian;


service YunPian {
    rpc Send(SendSmsReq) returns (SendSmsRes);
}

message SendSmsReq {
    string code = 1;
}

message SendSmsRes {
    int32 code = 1;
    string message = 2;
    map<string, string> data = 3; 
}

通过protoc命令生成PHP文件

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

通过composer安装consule-php-sdk等包

"require": {
    "grpc/grpc": "^v1.3.0",
    "google/protobuf": "^v3.3.0",
    "sensiolabs/consul-php-sdk": "^3.0"
  },
require dirname(__FILE__).'/../vendor/autoload.php';
define('COORD_FACTOR', 1e7);

$serviceFactory = new SensioLabs\Consul\ServiceFactory();
$cl = $serviceFactory->get("catalog"); //采用cataLog的服务方式
$service = $cl->service("yunpian"); //参数传入和服务端约定的服务名
$microServiceData = \GuzzleHttp\json_decode($service->getBody(), true)[0];  //请求微服务的具体地址
$host = $microServiceData["ServiceAddress"];
$port = $microServiceData["ServicePort"];
$client = new Yunpian\YunPianClient($host.":".$port, [
    'credentials' => Grpc\ChannelCredentials::createInsecure(),
]);
$reqObj = new Yunpian\SendSmsReq();  //自定义请求
$reqObj->setCode("1234");
list($data, $status) = $client->Send($reqObj)->wait();
var_dump($data->getCode());
var_dump($data->getMessage());
var_dump($data->getData());

源码: go-grpc-getway


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web 2.0 Architectures

Web 2.0 Architectures

Duane Nickull、Dion Hinchcliffe、James Governor / O'Reilly / 2009 / USD 34.99

The "Web 2.0" phenomena has become more pervasive than ever before. It is impacting the very fabric of our society and presents opportunities for those with knowledge. The individuals who understand t......一起来看看 《Web 2.0 Architectures》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具