Skip to content

Commit

Permalink
feat: update grpc verson to v1.60.1 and add grpc php tools
Browse files Browse the repository at this point in the history
  • Loading branch information
daheige committed Jan 10, 2024
1 parent d07782f commit be5fffa
Show file tree
Hide file tree
Showing 23 changed files with 1,630 additions and 93 deletions.
2 changes: 1 addition & 1 deletion example/bin/grpc_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
# google api link:https://github.com/googleapis/googleapis

# protoc inject tag
go install github.com/favadi/protoc-go-inject-tag@latest
go install github.com/favadi/protoc-go-inject-tag@latest
16 changes: 16 additions & 0 deletions example/bin/mac-install-grpc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# mac protoc安装
protocExec=`which protoc`
if [ ! -z $phpExec ]; then
echo "you has installed protoc"
exit 0
fi

brew install autoconf
brew install automake
brew install libtool
brew install curl
brew install make
brew install g++
brew install unzip
brew install cmake
brew install protobuf
92 changes: 92 additions & 0 deletions example/bin/mac-php-grpc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# mac protobuf安装
```shell
brew install protobuf
```
或者编译安装:
```shell
git clone https://github.com/google/protobuf.git
cd protobuf
sh ./autogen.sh
./configure
make
sudo make install
```
# php grpc_php_plugin 插件安装
参考文档:https://www.jianshu.com/p/bb15ad7532be

```shell
make protoc grpc_php_plugin
```

# php protobuf拓展安装
先判断是否安装php grpc.so and protobuf.so
```shell
php -m | grep proto
php -m | grep grpc
```
如果没有任何输出就需要安装grpc和protobuf拓展
```shell
pecl install grpc
pecl install protobuf
```
安装grpc拓展成功提示(这里我是php8.1版本):
```
Build process completed successfully
Installing '/usr/local/Cellar/[email protected]/8.1.25/pecl/20210902/grpc.so'
install ok: channel://pecl.php.net/grpc-1.59.1
Extension grpc enabled in php.ini
```

安装protobuf拓展成功提示:
```
Build process completed successfully
Installing '/usr/local/Cellar/[email protected]/8.1.25/pecl/20210902/protobuf.so'
install ok: channel://pecl.php.net/protobuf-3.25.1
Extension protobuf enabled in php.ini
```

查看php.ini路径目录
```shell
php --ini
```
将输出如下内容:
```
% php --ini
Configuration File (php.ini) Path: /usr/local/etc/php/8.1
Loaded Configuration File: /usr/local/etc/php/8.1/php.ini
Scan for additional .ini files in: /usr/local/etc/php/8.1/conf.d
Additional .ini files parsed: /usr/local/etc/php/8.1/conf.d/ext-opcache.ini
```

安装好后,在php.ini中添加对应的配置即可
(一般来说上面安装后,会自动添加依赖,如果没有添加,请手动添加即可)
```ini
extension=protobuf.so
extension=grpc.so
```
查看拓展是否安装成功:
```shell
php -m | grep grpc
php -m | grep protobuf
```

# php composer安装
```shell
cd ~
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
```
将安装好的composer.phar移动到/usr/local/bin/下面即可
```shell
sudo mv composer.phar /usr/local/bin/composer
```
设置镜像:
```shell
composer config -g repo.packagist composer https://packagist.org
```
6 changes: 5 additions & 1 deletion example/bin/nodejs-generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ jsProtoExec=$(which "grpc_tools_node_protoc")
if [ -z $jsProtoExec ]; then
grpc_node_plugin=$(which "grpc_node_plugin")
if [ -z $grpc_node_plugin ]; then
echo 'Please install grpc_node_plugin or grpc_tools_node_protoc';
echo 'Please install grpc_node_plugin or grpc_tools_node_protoc'
echo "npm install -g grpc-tools"
exit 0
fi
fi
Expand Down Expand Up @@ -49,10 +50,13 @@ if [ $os == "Darwin" ];then
sed -i "" 's/var google_api_annotations_pb/\/\/ var google_api_annotations_pb/g' `grep google_api_annotations_pb -rl $nodejs_pb_dir`
sed -i "" 's/let google_api_annotations_pb/\/\/ let google_api_annotations_pb/g' `grep google_api_annotations_pb -rl $nodejs_pb_dir`
sed -i "" 's/goog.object.extend(proto, google_api_annotations_pb)/\/\/ this code deleted/g' `grep google_api_annotations_pb -rl $nodejs_pb_dir`
# replace grpc to @grpc/grpc-js
sed -i "" "s/require('grpc')/require('@grpc\/grpc-js')/g" `grep "require('grpc')" -rl $nodejs_pb_dir`
else
sed -i 's/var google_api_annotations_pb/\/\/ var google_api_annotations_pb/g' `grep google_api_annotations_pb -rl $nodejs_pb_dir`
sed -i 's/let google_api_annotations_pb/\/\/ let google_api_annotations_pb/g' `grep google_api_annotations_pb -rl $nodejs_pb_dir`
sed -i 's/goog.object.extend(proto, google_api_annotations_pb)/\/\/ this code deleted/g' `grep google_api_annotations_pb -rl $nodejs_pb_dir`
sed -i "s/require('grpc')/require('@grpc\/grpc-js')/g" `grep "require('grpc')" -rl $nodejs_pb_dir`
fi

echo "generating nodejs code success"
Expand Down
26 changes: 26 additions & 0 deletions example/bin/php-grpc-plugin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# php grpc工具安装
# grpc_php_plugin check.
grpc_php_plugin=$(which "grpc_php_plugin")
if [ -z $grpc_php_plugin ]; then
mkdir ~/web/
cd ~/web
git clone -b v1.60.0 https://github.com/grpc/grpc
cd ~/web/grpc
git checkout -b v1
git submodule update --init --recursive
mkdir -p cmake/build
cd cmake/build
cmake ../..
make
make grpc_php_plugin
make protoc grpc_php_plugin
cp grpc_php_plugin /usr/local/bin/
chmod +x /usr/local/bin/grpc_php_plugin

# 安装php grpc拓展(推荐pecl方式安装)
# pecl install grpc
# 安装完成后,修改php.ini文件,增加配置 extension = grpc.so
else
echo "grpc_php_plugin exists!"
fi
exit 0
14 changes: 14 additions & 0 deletions example/bin/protoc-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# mac 源码安装protoc,当然你可以使用 mac-install-grpc.sh 快速安装
protoExec=$(which "protoc")
if [ -z $protoExec ]; then
mkdir ~/web/
cd ~/web/
git clone https://github.com/google/protobuf.git
cd protobuf
sh ./autogen.sh
./configure
make
sudo make install
fi

# centos7 安装参考地址 https://github.com/daheige/rs-rpc?tab=readme-ov-file#centos7-install-protoc
2 changes: 1 addition & 1 deletion example/bin/ubuntu-php7.2-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ubuntu php7.2 install
phpExec=`which php`
if [ ! -z $phpExec ]; then
echo "you has install php"
echo "you has installed php"
exit 0
fi

Expand Down
4 changes: 2 additions & 2 deletions example/clients/go/pb/hello.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 13 additions & 11 deletions example/clients/go/pb/hello.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions example/clients/go/pb/hello_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/clients/nodejs/hello.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let services = require('./pb/hello_grpc_pb.js');
let messages = require('./pb/hello_pb.js');
let grpc = require('grpc');
let grpc = require('@grpc/grpc-js');

let request = new messages.HelloReq();
request.setName('heige');
Expand Down
6 changes: 3 additions & 3 deletions example/clients/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"dependencies": {
"google-protobuf": "^3.20.1",
"grpc": "^1.24.11",
"grpc-tools": "^1.11.2"
"google-protobuf": "^3.21.2",
"@grpc/grpc-js": "^1.9.11",
"grpc-tools": "^1.12.4"
}
}
2 changes: 1 addition & 1 deletion example/clients/nodejs/pb/hello_grpc_pb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// GENERATED CODE -- DO NOT EDIT!

'use strict';
var grpc = require('grpc');
var grpc = require('@grpc/grpc-js');
var hello_pb = require('./hello_pb.js');
// var google_api_annotations_pb = require('./google/api/annotations_pb.js');

Expand Down
8 changes: 7 additions & 1 deletion example/clients/nodejs/pb/hello_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@

var jspb = require('google-protobuf');
var goog = jspb;
var global = Function('return this')();
var global = (function() {
if (this) { return this; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
if (typeof self !== 'undefined') { return self; }
return Function('return this')();
}.call(null));

// var google_api_annotations_pb = require('./google/api/annotations_pb.js');
// this code deleted;
Expand Down
16 changes: 13 additions & 3 deletions example/clients/nodejs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@
```json
{
"dependencies": {
"google-protobuf": "^3.20.1",
"grpc": "^1.24.11",
"grpc-tools": "^1.11.2"
"google-protobuf": "^3.21.2",
"@grpc/grpc-js": "^1.9.11",
"grpc-tools": "^1.12.4"
}
}
```

# yarn install
```shell
npm config set registry https://registry.npmmirror.com/
sudo npm install -g yarn
# 设置国内镜像
yarn config set registry https://registry.npmmirror.com/
```

# run nodejs

First start the server server.go
% cd gmicro/example/server
% go run server.go
2022/05/13 23:09:27 Starting http server and grpc server listening on 8081

执行如下命令调用nodejs grpc请求
% yarn install
% node hello.js
{
wrappers_: null,
Expand Down
Loading

0 comments on commit be5fffa

Please sign in to comment.