gPRC入门笔记

这篇博客介绍了gRPC的基本概念和工作流程,包括使用Protocol Buffers进行数据序列化,以及gRPC服务定义和核心概念。通过一个简单的例子展示了如何创建和解析Protocol Buffers消息。此外,还提到了gRPC支持的不同服务方法类型,并提供了一个Python的gRPC入门指南。
摘要由CSDN通过智能技术生成
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.

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;
      }
      
    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);
      
    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();
      

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;
    }
    
    • 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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值