golang http client 使用gzip_Grpc介绍 — Go-Service To PHP-Client
笔者现在公司项目大部分是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依赖
- grpcphpplugin 用来生成文件
1、先编译grpcphpplugin
> 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
最终得到了grpcphpplugin
2、安装PHP运行依赖
最简单的方式就是直接通过pecl进行安装:
pecl install grpc
pecl install protobuf
如果无法使用pecl可以使用编译的方式进行安装,PHP依赖的源文件已经存放在grpc中:
> 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里面增加如下内容:
extension=grpc.so
extension=protobuf.so
通过phpinfo(); 可以正常看到这两个模块正常即可
PS:protobuf可以不通过扩展的方式通过composer引入也可,对效率来说更加推荐通过SO扩展,demo中已经准备了7.1和7.2版本的so文件
二,生成 PHP protobuf 文件
> 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服务
拷贝依赖文件:
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获取依赖文件:
> 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
编写测试文件:
> vim client.phprequire '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 HelloworldGreeterClient('localhost:50051', ['credentials' => GrpcChannelCredentials::createInsecure(),]);$request = new HelloworldHelloRequest();$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";
结果:
> php client.php
holle world
golang http client 使用gzip_Grpc介绍 — Go-Service To PHP-Client相关推荐
- WCF Data Service 的.NET Client 的不支持原生类型服务操作的解决方法
WCF Data Service 的.NET Client 的不支持返回值为原生类型(string,int)的服务操作调用,例如我们用如下服务操作: [WebGet] public ObjectQu ...
- 从Client应用场景介绍IdentityServer4(一)
从Client应用场景介绍IdentityServer4(一) 原文:从Client应用场景介绍IdentityServer4(一) 一.背景 IdentityServer4的介绍将不再叙述,百度下可 ...
- 从Client应用场景介绍IdentityServer4(二)
从Client应用场景介绍IdentityServer4(二) 原文:从Client应用场景介绍IdentityServer4(二) 本节介绍Client的ClientCredentials客户端模式 ...
- golang性能分析工具pprof介绍
1 golang性能分析工具pprof介绍 文章目录 1 golang性能分析工具pprof介绍 1.1 pprof简介 1.2 pprof引入方法 1.3 使用pprof进行分析的方法 1.3.1 ...
- Web APIs介绍(四)——offset/client/scroll/轮播图/本地存储
1.1. 元素偏移量 offset 系列 1.1.1 offset 概述 offset 翻译过来就是偏移量, 我们使用 offset系列相关属性可以动态的得到该元素的位置(偏移).大小等. 获得元素距 ...
- oracle client 11.2.0.3 32位,oracle client 32位/64位下载(Oracl数据库)
今天给大家推荐的这款oracle client软件是一款功能丰富的数据库管理系统,支持32位/64位系统,用户可以通过该Oracl数据库工具执行OCI.OCCI.PRO*C.ODBC和JDBC程序,o ...
- iOS 中client和server的 Web Service 网络通信 (1)
当你打开你手机上新浪微博应用或者知乎应用是.你是否会去想这些显示在手机上的图片和数据时从哪里来的?又是通过如何的方法实现的?好.那么接下来就介绍是如何实现的.过程又是怎么样的. 当我们浏览着 ...
- ROS的学习(十六)用C++写一个简单的服务器(service)和客户端(client)
我们将创建一个服务器节点add_two_ints_server,它将会收到两个整数,并且返回它们的和.切换目录到之前建立的beginner_tutorials包下: cd ~/catkin_ws/sr ...
- 如何使用工业级AP—Client模式使用介绍
产品介绍 工业级AP,与普通的AP主要区别是面向工业应用环境做出适应,主要包括: (1) 适应-40℃-+75℃温度下严苛的工业级工作环境: (2) 冗余双路直流供电,以及标准PoE供电,适应工业环境 ...
最新文章
- openshift harp.js heroku react-router 4
- 常见的数据库管理系统排名(DBMS)
- 使用cron表达式 设置定时任务
- 解决用户控件循环引用的笨办法
- 微调︱caffe中fine-tuning模型三重天(函数详解、框架简述)+微调技巧
- java tsp问题_蚁群算法(Java)tsp问题
- windows 安装 dnw、fastboot 驱动的时候遇到问题(数字签名)
- CRNN论文阅读笔记
- html阅读caj文件格式,caj文件怎么打开,caj转换成word(解决方案)
- 贪心——国际象棋锦标赛
- Adobe Camera Raw v15.0.0.1264 增效工具
- 真的!这么写参数校验 (Validator) 就不会被劝退了。。。
- 移远BC28_opencpu方案_pin脚分配
- 计算机图形学笔记 || 基本图形的扫描转换
- android图标生成网址
- 无法打开用户默认数据库 登录失败
- 学会做笔记-子弹笔记学习概要三
- QT的Listwidget控件
- Flutter 项目实战 Dio网络请求 四
- VMware虚拟机——如何在VMware虚拟机环境下识别U盘
热门文章
- matlab repmat 函数的使用
- python 中 * 的使用和表示含义
- zeppelin k8s安装部署和基本使用教程(在线的分析工具)
- 知名互联网公司系统架构图[第3期]
- 数据库——关系数据理论学习笔记
- linux 渗透 密码,linux渗透中常用命令+Tip
- [理解] Linux 作为一个服务器是怎样的存在 (一)
- [2019.05.09]Linux 学习笔记(3)
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Korea
- 使用C++实现二叉搜索树的数据结构