Как установить связь между python сервером и php клиентом, используя grp c? - PullRequest
1 голос
/ 28 февраля 2020

Я могу общаться, используя python клиент и сервер, но путаюсь с php клиентом. Я запутался по поводу протобуферов, небольшая программа, объясняющая весь процесс, была бы очень полезна.

Я просмотрел много документов, но все еще очень запутался в фактическом потоке.

calculator.proto

syntax = "proto3";

message Request {
    int32 num1 = 1;
    int32 num2 = 2;
}

message Response{
    int32 result = 1;
}

service Calculator {
    rpc Sum(Request) returns (Response) {}
}

calculator.py

def sum(x1,x2):
  y= x1+x2
  return y

server.py

import grpc
from concurrent import futures
import time

# import the generated classes
import calculator_pb2
import calculator_pb2_grpc

# import the original calculator.py
import calculator

# create a class to define the server functions, derived from
# calculator_pb2_grpc.CalculatorServicer
class CalculatorServicer(calculator_pb2_grpc.CalculatorServicer):

    # calculator.sum is exposed here
    def Sum(self, request, context):
        response = calculator_pb2.Response()
        response.result = calculator.sum(request.num1,request.num2)
        print 'Result:',response.result
        return response


# create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

# use the generated function `add_CalculatorServicer_to_server`
# to add the defined class to the server
calculator_pb2_grpc.add_CalculatorServicer_to_server(
        CalculatorServicer(), server)

# listen on port 50051
print('Starting server. Listening on port 50051.')
server.add_insecure_port('[::]:50051')
server.start()

# since server.start() will not block,
# a sleep-loop is added to keep alive
try:
    while True:
        time.sleep(86400)
except KeyboardInterrupt:
    server.stop(0)

client.py

import grpc

# import the generated classes
import calculator_pb2
import calculator_pb2_grpc

# open a gRPC channel
channel = grpc.insecure_channel('localhost:50051')

# create a stub (client)
stub = calculator_pb2_grpc.CalculatorStub(channel)
while True:
    try:
        # create a valid request message
        numbers = calculator_pb2.Request(num1=int(input("Enter number1: ")),num2=int(input("Enter number2: ")))
    # make the call
        response = stub.Sum(numbers)

        # print 'Result:',response.result
    except KeyboardInterrupt:
        print("KeyboardInterrupt")
        channel.unsubscribe(close)
        exit()

Эта установка возвращает добавление два числа на сервере (python). Мне нужна та же функциональность с python для сервера и php для клиента.

1 Ответ

0 голосов
/ 03 марта 2020

Я мог общаться с php клиентом и python сервером. Ниже приведены шаги и пример:

Шаги:


--------------------------------
           Python
--------------------------------

1. Make a folder named grpc

2. Install pip for python

3.Install virtual environment
  python -m pip install virtualenv

4. Run the commands
   virtualenv venv
   source venv/bin/activate
   python -m pip install --upgrade pip

5. Then install grpcio
   python -m pip install grpcio

6. Install grpc tools
   python -m pip install grpcio-tools

7. Generate protobuf
   python -m grpc_tools.protoc -I./proto --python_out=. --grpc_python_out=. ./proto/calculator.proto 

8. Make server.py


---------------------------------
           PHP
---------------------------------

1. Install composer
   composer install

2. Run these commands to install grpc using pecl
   sudo apt-get install php-dev
   pecl
   sudo pecl install grpc

3. Download protoc version > 3.7
commands:
PROTOC_ZIP=protoc-3.7.1-linux-x86_64.zip
curl -OL   https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/$PROTOC_ZIP
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
rm -f $PROTOC_ZIP  

4. Clone this repo
   git clone -b v1.27.0 https://github.com/grpc/grpc


5. Run this command
   cd grpc && git submodule update --init && make grpc_php_plugin
   cd examples/php/route_guide
   ./route_guide_proto_gen.sh

6. Move to root then run: make grpc_php_plugin

7.Generate protobuf
   protoc --proto_path=examples/protos --php_out=examples/php/route_guide --grpc_out=examples/php/route_guide --plugin=protoc-gen-grpc=bins/opt/grpc_php_plugin ./examples/protos/calculator.proto

8. Make index.php

index. php


<?php

include __DIR__ . '/vendor/autoload.php';
include __DIR__ . '/grpc/examples/php/route_guide/Calculator/CalculatorClient.php';
include __DIR__ . '/grpc/examples/php/route_guide/Calculator/SumRequest.php';
include __DIR__ . '/grpc/examples/php/route_guide/Calculator/SumResponse.php';
include __DIR__ . '/grpc/examples/php/route_guide/GPBMetadata/Calculator.php';

function add()
{
    // Listening to port
    $client = new \Calculator\CalculatorClient('localhost:6000', [
        'credentials' => Grpc\ChannelCredentials::createInsecure(),
    ]);
    while (true) {
        try {
            // request object
            $request = new \Calculator\SumRequest();
            echo " Num1: ";
            // num1
            $request->setNum1((int) rtrim(fgets(STDIN)));
            echo " Num2: ";
            // num2
            $request->setNum2((int) rtrim(fgets(STDIN)));

            list($res, $status) = $client->Sum($request)->wait();
            // result
            echo $res->getResult() . "\n";

        } catch (Exception $error) {
            echo $error;
        }
    }
}

try {
    add();
    //    phpinfo();
} catch (Exception $e) {
    echo $e;
}

calculator.py


def sum(x1,x2):
  y= x1+x2
  return y

server.py

import grpc
from concurrent import futures
import time

# import the generated classes
import calculator_pb2
import calculator_pb2_grpc

# import the original calculator.py
import calculator

# create a class to define the server functions, derived from
# calculator_pb2_grpc.CalculatorServicer
class CalculatorServicer(calculator_pb2_grpc.CalculatorServicer):

    # calculator.sum is exposed here
    def Sum(self, request, context):
        response = calculator_pb2.SumResponse()
        response.result = calculator.sum(request.num1,request.num2)
        print 'Result:',response.result
        return response


# create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

# use the generated function `add_CalculatorServicer_to_server`
# to add the defined class to the server
calculator_pb2_grpc.add_CalculatorServicer_to_server(
        CalculatorServicer(), server)

# listen on port 50051
print('Starting server. Listening on port 6000.')
server.add_insecure_port('[::]:6000')
server.start()

# since server.start() will not block,
# a sleep-loop is added to keep alive
try:
    while True:
        time.sleep(86400)
except KeyboardInterrupt:
    server.stop(0)

calculator.proto



syntax = "proto3";

package Calculator;
message SumRequest {
    int64 num1 = 1;
    int64 num2 = 2;
}

message SumResponse{
    int64 result = 1;
}

service Calculator {
    rpc Sum(SumRequest) returns (SumResponse) {}
}

Также go по этой ссылке для лучшего понимания: PHP - https://grpc.io/docs/tutorials/basic/php/ Python - https://grpc.io/docs/tutorials/basic/python/

...