GRPC是google开源的一个高性能、跨语言的RPC框架,基于HTTP2协议,基于protobuf 3.x,支持多种开发语言。
对于开发者而言:
1)需要使用protobuf定义接口,即.proto文件
2)然后使用compile工具生成特定语言的执行代码,比如JAVA、C/C++、Python等。类似于thrift,为了解决跨语言问题。
3)启动一个Server端,server端通过侦听指定的port,来等待Client链接请求。

本文以golang和php为例展开教程,使用golang做rpc server,php做rpc client,实现跨语言调用。

前期准备

golang的grpc包

golang的rpc包,作为golang rpc server需要这个包

go get google.golang/grpc

php的grpc扩展

下载地址: http://pecl.php/package/gRPC
安装过程:

phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
# 添加grpc.so到php.ini配置
vim /usr/local/php/etc/php.ini
extension = "grpc.so"
php -m | grep "grpc"
grpc

protoc

命令行下将proto协议文件转换为多种语言对应格式的工具

下载地址:https://github/google/protobuf
安装

./configure
make 
make install
protoc --help
Usage: protoc [OPTION] PROTO_FILES
Parse PROTO_FILES and generate output based on the options given:
...

尝试运行

golang的 rpcserver

  • (1). 可直接用go get 的例子
go get google.golang/grpc/examples/helloworld
#将会编译好greeter_server和greeter_client
greeter_server
#greeter_server启动后挂起...

在另一个命令行运行:

greeter_client "this is my message"
2017/08/30 21:55:25 Greeting: Hello this is my message
  • (2). 手动生成go文件运行
    假如我们有一个proto文件helloworld.proto:
syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

通过protoc将helloworld.proto转换生成对应的go文件:

protoc --go_out=plugins=grpc:. helloworld.proto

得到helloworld.pb.go,不用编辑这个文件
这个文件属于package helloworld,你要适当放好他所在目录。
手动编辑greeter_server.go:

package main

import (
    "log"
    "net"

    "golang/x/net/context"
    "google.golang/grpc"
    pb "/helloworld"
    "google.golang/grpc/reflection"
)

const (
    port = ":50051"
)

// server is used to implement helloworld.GreeterServer.
type server struct{}

// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
    return &pb.HelloReply{Message: "Hello " + in.Name}, nil
}

func main() {
    lis, err := net.Listen("tcp", port)
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }
    s := grpc.NewServer()
    pb.RegisterGreeterServer(s, &server{})
    // Register reflection service on gRPC server.
    reflection.Register(s)
    if err := s.Serve(lis); err != nil {
        log.Fatalf("failed to serve: %v", err)
    }
}

手动编辑greeter_client.go:

package main

import (
    "log"
    "os"

    "golang/x/net/context"
    "google.golang/grpc"
    pb "google.golang/grpc/examples/helloworld/helloworld"
)

const (
    address     = "localhost:50051"
    defaultName = "world"
)

func main() {
    // Set up a connection to the server.
    conn, err := grpc.Dial(address, grpc.WithInsecure())
    if err != nil {
        log.Fatalf("did not connect: %v", err)
    }
    defer conn.Close()
    c := pb.NewGreeterClient(conn)

    // Contact the server and print out its response.
    name := defaultName
    if len(os.Args) > 1 {
        name = os.Args[1]
    }
    r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
    if err != nil {
        log.Fatalf("could not greet: %v", err)
    }
    log.Printf("Greeting: %s", r.Message)
}

编译运行:

go build greeter_server.go && ./greeter_server &
go build greeter_client.go && ./greeter_client "Message from My Build"
2017/08/30 22:04:56 Greeting: Hello Message from My Build

php运行grpc client

参考 github/grpc/grpc/example/php
将此目录clone下来

git clone github/grpc/grpc.git
git submodule update --init
#如果只用php grpc client,此处不用make和make install
#make
#make install
#为了生成grpc client stub code,最好make grpc_php_plugin
make grpc_php_plugin

cd grpc/example/php
curl -sS https://getcomposer/installer | php
php composer.phar install

composer安装后会生成vendor和autoload.php文件,可自行选择添加到自己的项目中。
helloword.pb.php文件可以由protoc自定义生成:

protoc --php_out=./ helloworld.proto

上面的方法只生成所有的proto对应的类,不生成实现grpc接口的stub client类。
使用下面的方法生成:参考grpc官方

protoc --proto_path=./   --php_out=./   --grpc_out=./   --plugin=protoc-gen-grpc=/data/src/grpc/bins/opt/grpc_php_plugin   ./esselector.proto

/data/src/grpc/是git clone github/grpc/grpc.git的目录。
该命令在grpc/src/php/bin/generate_proto_php.sh中。
如果出现:

bins/opt/grpc_php_plugin: program not found or is not executable
--grpc_out: protoc-gen-grpc: Plugin failed with status code 1.

是没有编译grpc_php_plugin,需要在grpc下:

make grpc_php_plugin

php greeter_client.php编辑内容:

require dirname(__FILE__).'/vendor/autoload.php';

// The following includes are needed when using protobuf 3.1.0
// and will suppress warnings when using protobuf 3.2.0+
include_once dirname(__FILE__).'/helloworld.pb.php';
include_once dirname(__FILE__).'/helloworld_grpc_pb.php';

function greet($name)
{
    $client = new Helloworld\GreeterClient('127.0.0.1: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 "the greet: ".greet($name)."\n";

运行:

php ./greeter_client.php
the greet: Hello world

祝大家学习grpc顺利~

更多推荐

grpc简易教程 go server+php client