• gPRC入门笔记


    This blog is about learning gRPC
    You are welcomed to chat about it and if you like this blog, do not forget to give me a like.
    
    • 1
    • 2

    Welcome to see my homepage and contact me: NicholasYe’s Homepage.


    0. Before study


    1. Introduction

    1. An overview
      • gRPC is a kind of tool which you can create a server and many client, while the server handle every call of the client and respond them.
      • You can easily create your server in Java with client in Go, Python and so on, which is a huge advantage.
      • gRPC use protocol buffers to transmit the message.

    2. What is protocol buffers?

    1. Introduction

      • Protocol buffers provide a language-neutral, platform-neutral, extensible mechanism for serializing structured data in a forward-compatible and backward-compatible way.
      • It’s like JSON, except it’s smaller and faster, and it generates native language bindings.
    2. Workflow:
      图片.png

    3. A very simple example

      1. In .proto definition, use Java to write down:
        message Person {
          optional string name = 1;
          optional int32 id = 2;
          optional string email = 3;
        }
        
        • 1
        • 2
        • 3
        • 4
        • 5
      2. After compiling this, you will get a Builder class:
        Person john = Person.newBuilder()
            .setId(1234)
            .setName("John Doe")
            .setEmail("jdoe@example.com")
            .build();
        output = new FileOutputStream(args[0]);
        john.writeTo(output);
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
      3. Then you can deserialize the data and use the methods protocol buffers creates in other languages, like C++:
        Person john;
        fstream input(argv[1], ios::in | ios::binary);
        john.ParseFromIstream(&input);
        int id = john.id();
        std::string name = john.name();
        std::string email = john.email();
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6

    3. Core concept

    1. In this section I will show you an simple but complete gPRC service, and explain what they mean:

      service HelloService {
        rpc SayHello (HelloRequest) returns (HelloResponse);
      }
      
      message HelloRequest {
        string greeting = 1;
      }
      
      message HelloResponse {
        string reply = 1;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • service: Define what your server want to receive and what to reply according to the message.
      • message: Define every message detail information.
    2. gPRC let you define four kinds of service method(you can also see the website for more detailed information):

      1. rpc SayHello(HelloRequest) returns (HelloResponse);
      2. rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);
      3. rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);
      4. rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);

    4. A guidance of gPRC in python


    Please clearly mark the source of the article in the process of reproducing it! Respect for originality and intellectual property rights, thanks!

  • 相关阅读:
    大数据专业学起来会不会很累?
    4种API性能恶化根因分析
    自从新来了个字节20K出来的,就见识到了什么是天花板
    Conda常用命令及Pycharm使用虚拟环境
    探索JDK8新特性,Stream 流:构建流的多种方式
    牛客小白月赛52 E 分组求对数和(容斥定理+二分)
    上周热点回顾(9.19-9.25)
    高创伺服驱动器CDHD2和sick伺服编码器hiperface通讯时的故障解决
    亚马逊对IP的要求是什么?
    c++ condition_variable使用场景
  • 原文地址:https://blog.csdn.net/NicholasYTZ/article/details/125888366