php grpc - yaokun123/php-wiki GitHub Wiki

1、php-grpc-gen下载

php-grpc-gen下载不了怎么办?docker来解决。在安装docker的环境中执行如下命令

docker run --rm -it -v ~/proj:/proj llaoj/php-grpc-gen:1.30.0 bash

2、protoc命令使用php-grpc-gen插件生成php相关文件

protoc -I=.  --php_out=. --grpc_out=. --plugin=protoc-gen-grpc=/opt/grpc/bin/grpc_php_plugin *.proto

3、hystrix熔断

hystrix.CommandConfig{
  Timeout: 1000,
  //默认20 。熔断器请求阀值,意思是有20个请求才进行 错误百分比计算
  RequestVolumeThreshold:20,
  //就是错误百分比。默认50(50%)
   ErrorPercentThreshold:50,
   //过多长时间,熔断器再次检测是否开启。单位毫秒 (默认5秒)
  SleepWindow :5000,     
}

4、使用

// 使用composer https://github.com/dcarbone/php-consul-api
        /*$config = \DCarbone\PHPConsulAPI\Config::newDefaultConfig();
        $consul = new \DCarbone\PHPConsulAPI\Consul($config);
        $resp = $consul->Health()->service("es");
        $servers = $resp[0];
        if (empty($servers)){
            $this->log("服务不存在");
            exit();
        }
        $addr_list = [];
        foreach($servers as $server){
            $addr_list[] = sprintf("%s:%d",$server->Service->Address,$server->Service->Port);
        }
        $key = array_rand($addr_list,1); // 哈哈哈一个简单的负载均衡
        $hostname = $addr_list[$key];*/

        // 用于连接 服务端
        $hostname = '127.0.0.1:61000';
        $client = new Es\EsClient($hostname,[
            'credentials'=>Grpc\ChannelCredentials::createInsecure(),
        ]);

        //实例化 请求类
        $request = new Es\Request();
        $params = [
            "query_string" => "凭安征信"
        ];
        $params_str = json_encode($params,JSON_UNESCAPED_UNICODE);
        //$request->setName("demo");
        $request->setName($params_str);

        //调用远程服务
        list($reply, $status) = $client->Call($request,[],[
            'timeout'=> 1 * 1000 * 1000,
        ])->wait();

        // 响应处理
        if($status->code != Grpc\STATUS_OK){
            echo "ERROR:".$status->code. ',' . $status->details. PHP_EOL;
            exit();
        }

        $result = $reply->getMsg();
        $this->log($result);