前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Grpc介绍 — Go-Service To PHP-Client

Grpc介绍 — Go-Service To PHP-Client

作者头像
喵了个咪233
发布2019-05-26 16:14:55
1.2K0
发布2019-05-26 16:14:55
举报
文章被收录于专栏:喵了个咪的博客空间

笔者现在公司项目大部分是PHP进行开发,要完成整体微服务化必须要考虑PHP使用上的可行性,Grpc也是支持PHP作为client端发起Grpc请求,但是依赖的扩展等都相对复杂(毕竟不是亲儿子),那么本文就接受怎么使用PHP调用Grpc服务。

附上:

喵了个咪的博客:w-blog.cn

博文实例demo:GitHub - sunmi-OS/grpc-php-to-golang-demo

grpc官网:grpc / grpc.io

protobuf代码仓库:Releases · protocolbuffers/protobuf · GitHub

一,初始化PHP环境

PHP在使用过程中依赖一下几项内容

  • grpc.so
  • protobuf.so 或 composer依赖
  • grpc_php_plugin 用来生成文件

1、先编译grpc_php_plugin

代码语言:javascript
复制
> git clone https://github.com/grpc/grpc.git
> cd grpc
> git pull --recurse-submodules && git submodule update --init --recursive
> make
> sudo make install
# make install 会在 /usr/local/bin 目录下生成以下文件
#grpc_cpp_plugin  
#grpc_csharp_plugin  
#grpc_node_plugin  
#grpc_objective_c_plugin  
#grpc_php_plugin  
#grpc_python_plugin  
#grpc_ruby_plugin

# 或者只变编译grpc_php_plugin
> make grpc_php_plugin
Package libcares was not found in the pkg-config search path.
Perhaps you should add the directory containing `libcares.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libcares' found
[C]       Compiling third_party/address_sorting/address_sorting.c
[C]       Compiling third_party/address_sorting/address_sorting_posix.c
[C]       Compiling third_party/address_sorting/address_sorting_windows.c
[AR]      Creating /Users/wenzhenxi/Downloads/grpc/libs/opt/libaddress_sorting.a
[HOSTCXX] Compiling src/compiler/cpp_generator.cc
[HOSTCXX] Compiling src/compiler/csharp_generator.cc
[HOSTCXX] Compiling src/compiler/node_generator.cc
[HOSTCXX] Compiling src/compiler/objective_c_generator.cc
[HOSTCXX] Compiling src/compiler/php_generator.cc
[HOSTCXX] Compiling src/compiler/python_generator.cc
[HOSTCXX] Compiling src/compiler/ruby_generator.cc
[AR]      Creating /Users/wenzhenxi/Downloads/grpc/libs/opt/libgrpc_plugin_support.a
[HOSTCXX] Compiling src/compiler/php_plugin.cc
Package libcares was not found in the pkg-config search path.
Perhaps you should add the directory containing `libcares.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libcares' found
Package libcares was not found in the pkg-config search path.
Perhaps you should add the directory containing `libcares.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libcares' found
[HOSTLD]  Linking /Users/wenzhenxi/Downloads/grpc/bins/opt/grpc_php_plugin

最终得到了grpc_php_plugin

2、安装PHP运行依赖

最简单的方式就是直接通过pecl进行安装:

代码语言:javascript
复制
pecl install grpc
pecl install protobuf

如果无法使用pecl可以使用编译的方式进行安装,PHP依赖的源文件已经存放在grpc中:

代码语言:javascript
复制
> cd grpc/src/php/ext/grpc
> phpize
> ./configure
> make
> sudo make install

> git clone https://github.com/allegro/php-protobuf
> phpize
> ./configure
> make
> sudo make install

最后需要在php.ini里面增加如下内容:

代码语言:javascript
复制
extension=grpc.so
extension=protobuf.so

通过phpinfo(); 可以正常看到这两个模块正常即可

PS:protobuf可以不通过扩展的方式通过composer引入也可,对效率来说更加推荐通过SO扩展,demo中已经准备了7.1和7.2版本的so文件

二,生成 PHP protobuf 文件

代码语言:javascript
复制
> cd $GOPATH/src/grpc-php-to-golang-demo/protobuf
> mkdir -p php-client/helloworld
> protoc --proto_path=./ --php_out=php-client/helloworld --grpc_out=php-client/helloworld --plugin=protoc-gen-grpc=../grpc_php_plugin-all/osx-64/grpc_php_plugin  helloworld.proto
> cd php-client/helloworld/
> ll
total 0
drwxr-xr-x  4 wenzhenxi  staff  128  2 15 15:09 ./
drwxr-xr-x  3 wenzhenxi  staff   96  2 15 15:09 ../
drwxr-xr-x  3 wenzhenxi  staff   96  2 15 15:09 GPBMetadata/
drwxr-xr-x  5 wenzhenxi  staff  160  2 15 15:09 Helloworld/

三,使用PHP调用go服务

拷贝依赖文件:

代码语言:javascript
复制
cd $GOPATH/src/grpc-php-to-golang-demo
mkdir -p php
cd php
mv $GOPATH/src/grpc-php-to-golang-demo/protobuf/php-client/helloworld helloworld

使用composer获取依赖文件:

代码语言:javascript
复制
> vim composer.json

{
  "name": "grpc/grpc-demo",
  "description": "gRPC example for PHP",
  "require": {
    "grpc/grpc": "^v1.3.0"
  },
  "autoload": {
    "psr-4": {
      "": "route_guide/"
    }
  }
}

>  composer install 

编写测试文件:

代码语言:javascript
复制
> vim client.php

require 'vendor/autoload.php';

include_once 'helloworld/Helloworld/GreeterClient.php';
include_once 'helloworld/Helloworld/HelloReply.php';
include_once 'helloworld/Helloworld/HelloRequest.php';
include_once 'helloworld/GPBMetadata/Helloworld.php';

function greet($name)
{
    $client = new Helloworld\GreeterClient('localhost:50051', [
        'credentials' => Grpc\ChannelCredentials::createInsecure(),
    ]);
    $request = new Helloworld\HelloRequest();
    $request->setName($name);
    list($reply, $status) = $client->SayHello($request)->wait();
    $message = $reply->getMessage();

    return $message;
}

$name = !empty($argv[1]) ? $argv[1] : 'world';
echo greet($name)."\n";

结果:

代码语言:javascript
复制
> php client.php 
holle world
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一,初始化PHP环境
    • 1、先编译grpc_php_plugin
      • 2、安装PHP运行依赖
      • 二,生成 PHP protobuf 文件
      • 三,使用PHP调用go服务
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档