当前位置:网站首页>Introduction to grpc
Introduction to grpc
2022-07-28 15:26:00 【Liupig Liu pige】
https://grpc.io/docs/what-is-grpc/introduction/
Introduce gRPC And protocol buffer .
This page introduces you gRPC And protocol buffer .gRPC The protocol buffer can be used as its interface definition language ( IDL ) And the underlying message exchange format . If you are not familiar with it gRPC and / Or protocol buffer , Please read this ! If you just want to know more about gRPC Operating condition , please Choose a language and try it Quick start.
summary
stay gRPC in , Client applications can directly call methods on server applications on different machines , It's like it's a local object , Make it easier for you to create distributed applications and services . And many RPC The system is the same ,gRPC The idea of service definition , Specify the methods that can be called remotely and their parameters and return types . On the server , The server implements this interface and runs a gRPC The client calls the server to handle . On the client side , The client has a stub ( In some languages, it's just called the client ), It provides the same method as the server .
Concept map
gRPC Clients and servers can run and communicate with each other in a variety of environments —— from Google Internal server to your own desktop —— And you can use any gRPC Supported languages . therefore , for example , You can use Go、Python or Ruby The client in easily in Java Created in gRPC The server . Besides , Abreast of the times Google API Will have its interface gRPC edition , So that you can easily Google Build functionality into your application .
Use protocol buffer
By default ,gRPC Use protocol buffer , Google's mature open source mechanism for structured data serialization ( Although it can be used with other data formats such as JSON Use it together ). This is a quick introduction to how it works . If you are already familiar with protocol buffers , Please feel free to jump to the next section .
The first step in using the protocol buffer is to define what to do in proto The structure of the serialized data in the file : This is a .proto Ordinary text file with extension . The protocol buffer data is constructed as news , Each message is a small logical record of information , Contains a series of names called fields - It's worth it . This is a simple example :
message Person {
string name = 1;
int32 id = 2;
bool has_ponycopter = 3;
}
then , Once you specify your data structure , You can use the protocol buffer compiler protoc Generate data access classes from your prototype definition in your favorite language . These provide a simple accessor for each field , for example name()and set_name(), And serializing the entire structure / The method of parsing to the original bytes . therefore , for example , If the language you choose is C++, Running the compiler in the above example will generate a file named Person. then , You can use this class in your application to fill in 、 Serializing and retrieving Person Protocol buffer message .
You are in the ordinary proto The document defines gRPC service , take RPC Method parameters and return types are specified as protocol buffer messages :
// The greeter service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {
}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
gRPC Use protoc special gRPC Plug in from your proto File generation code : You will get the generated gRPC Client and server code , And for filling 、 General protocol buffer code for serializing and retrieving message types . You'll see an example below .
To learn more about protocol buffers , Including how to protoc Install... In the language of your choice gRPC plug-in unit , Please refer to the protocol buffer documentation .
Protocol buffer version
The protocol buffer has been available to open source users for some time , Most examples of this site use the protocol buffer version 3 (proto3), It has a slightly simplified syntax 、 Some useful new features , And support more languages .Proto3 Currently in Protocol buffer GitHub The repository provides Java、C++、Dart、Python、Objective-C、C#、lite-runtime (Android Java)、Ruby and JavaScript edition , As well as from golang/protobuf Official package Go Language generator , More languages are being developed . You can go to proto3 More information and reference documents can be found in the language guide for each language . The reference document also includes a formal specification of .proto File format .
Generally speaking , Although you can use proto2( Current default protocol buffer version ), But we suggest that you will proto3 And gRPC Use it together , Because it allows you to use gRPC All supported languages , And avoid contact with proto2 Compatibility of client dialog proto3 The server , vice versa .
边栏推荐
猜你喜欢

3588. Permutation and binary

【通道注意力机制】SENet

听说crmeb多商户增加了种草功能?

MySQL 8.0 common (continuous update)

Install MOSEK, license installation location search

Jwy-32b voltage relay

Xiaobai can understand the 35 necessary questions in MySQL interview

Compilation learning

In 2022, the average salary of global programmers was released, and China ranked unexpectedly

Slider restore and validation (legal database)
随机推荐
3564. 日期类
For loop
汇编学习
volatile原理
3477. 简单排序
每日一题(回溯)
Have you ever used the single merchant mall, which is smooth enough to make people feel numb?
Compilation learning
Here comes the full open source free customer service system
3511. 倒水问题
Daily question (retrospective)
苹果iPhone手机APP应用图标隐藏怎么找回恢复显示在iPhone苹果手机桌面显示被隐藏的应用APP图标到iPhone苹果手机桌面?
Knowledge payment open source system
[Game Testing Engineer] get to know game testing for the first time - do you know it?
Learn PHP reflection classes from ThinkPHP remote code execution
MySQL 8.0 common (continuous update)
Ry-d1/1 voltage relay
软件测试的流程规范有哪些?具体要怎么做?
Jwy-32b voltage relay
Stack expression