Python 简单案例

安装

  1. python -m pip install grpcio #安装grpc
  2. python -m pip install grpcio-tools #安装grpc tools

简单proto文件

  1. syntax = "proto3";
  2. service Greeter{
  3. rpc SayHello(HelloRequest) returns (HelloReply){}
  4. }
  5. message HelloRequest{
  6. string name =1;
  7. }
  8. message HelloReply{
  9. string message = 1;
  10. }

文件生成

python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. helloworld.proto

server

from grpc_demo.proto import helloworld_pb2, helloworld_pb2_grpc
import grpc
from concurrent import futures


class Greeter(helloworld_pb2_grpc.GreeterServicer):
    def SayHello(self, request, context):
        return helloworld_pb2.HelloReply(message="你好啊 " + request.name)


if __name__ == '__main__':
    # 实例化server
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    # 注册逻辑到server
    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
    # 启动
    server.add_insecure_port('127.0.0.1:50051')
    server.start()
    server.wait_for_termination()

client

from grpc_demo.proto import helloworld_pb2_grpc,helloworld_pb2
import grpc
if __name__ == '__main__':
    with grpc.insecure_channel("localhost:50051") as channel:
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        resp = stub.SayHello(helloworld_pb2.HelloRequest(name="ZYF111"))
        print(resp.message) // 你好啊 ZYF111

Go 简单案例

安装工具

https://github.com/protocolbuffers/protobuf/releases/tag/v3.14.0-rc1
解压把可执行文件放入环境变量即可 protoc.exe
image.png

下载依赖

go get github.com/golang/protobuf/protoc-gen-go

简单proto文件

syntax = "proto3";
option  go_package=".;proto";

service Greeter{

  rpc SayHello(HelloRequest) returns (HelloReply){}
}

message HelloRequest{
  string name =1;
}

message HelloReply{
  string message = 1;
}

文件生成

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

server

package main

import (
    "context"
    "fmt"
    "google.golang.org/grpc"
    "grpc_demo/proto"
    "net"
)

type Server struct {

}

func (this *Server) SayHello(ctx context.Context,res *proto.HelloRequest)(resp *proto.HelloReply,err error){
    return &proto.HelloReply{
        Message: res.Name +"您好",
    },nil
}

func main()  {
    g:=grpc.NewServer()
    s := Server{}
    proto.RegisterGreeterServer(g,&s)
    lis, err := net.Listen("tcp", fmt.Sprintf(":8080"))
    if err != nil {
        panic("failed to listen: "+err.Error())
    }
    g.Serve(lis)
}

client

package main

import (
    "context"
    "fmt"
    "google.golang.org/grpc"
    "grpc_demo/proto"
)

func main()  {
    conn,err:=grpc.Dial("127.0.0.1:8080",grpc.WithInsecure())
    if err!=nil {
    fmt.Println("net.Dial err"+err.Error())
    }


    c := proto.NewGreeterClient(conn)
    r,err := c.SayHello(context.Background(),&proto.HelloRequest{Name:"ZYF"})
    if err!=nil{
        panic(err)
    }
    fmt.Println(r.Message)  // 输出 ZYF您好
    defer conn.Close()
}

Python Go 相互调用

上面代码相互用各自client调用server即可

grpc流模式

简单模式

客户端发送一次请求,服务端响应一个数据

服务端数据流模式

客户端发起一次请求,服务端返回一段连续的数据流

客户端数据流模式

客户端源源不断的向服务器发送数据,在发送结束后,由服务端返回一个响应。物联网终端向服务器发送数据

双向数据流模式

客户端服务端发送数据是相互的,可以实时交互。聊天