Bring gRPC:gRPC and Related Introduction

  golang, grpc, javascript, php, protobuf

Bring gRPC:gRPC and Related Introduction

Original address:Bring gRPC:gRPC and Related Introduction

Project address:go-grpc-example

As the opening chapter, some knowledge related to gRPC will be introduced. Simply speaking, gRPC is an RPC framework designed based on HTTP/2 protocol, which uses Protobuf as IDL

Have you ever wondered what they are? This article will introduce some common knowledge and concepts, and give the manual address in more detail to go further.

I. rpcs

What is RPC

RPC refers to Remote Procedure Call, whose call includes transport protocol and coding (object sequence number) protocol, etc. Allows programs running on one computer to call subroutines of another computer without the developer having to program the interaction additionally.

Actual scenario:

There are two servers, a and b respectively. Application c on a wants to call application d on b server, can they be called directly locally?
The answer is no, but RPC is very convenient. Therefore, it is often said that using RPC is as simple as calling a function locally.

RPC framework

In my opinion, a complete RPC framework should include load balancing, service registration and discovery, service governance and other functions, and it should be expandable to facilitate access to traffic monitoring systems, etc.
Then it is complete, of course. Some relatively single RPC frameworks can achieve this standard by combining multiple components.

what do you reckon ?

Common RPC framework

Compare

\ Cross-language Multi IDL Service governance Registration center Service management
gRPC × × × ×
Thrift × × × ×
Rpcx ×
Dubbo ×

Why RPC

Simple, universal, safe and efficient

Can rpcs be HTTP-based

RPC refers to remote procedure call instead and can be based on HTTP protocol

Some people will definitely say that efficiency is superior. I can tell you that it is based on HTTP/1.1. HTTP/2 optimizes many problems (of course, there are also new problems), so you can see the theme gRPC of this article

Protobuf

Introduction

Protocol Buffers is an extensible method for serializing structured data regardless of language and platform. It is commonly used in communication protocols, data storage and so on. Compared with JSON and XML, it is smaller, faster and simpler, so it is also more popular with developers.

Grammar

syntax = "proto3";

service SearchService {
    rpc Search (SearchRequest) returns (SearchResponse);
}

message SearchRequest {
  string query = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
}

message SearchResponse {
    ...
}

1. The first line (non-empty, non-comment line) is declared for useproto3Grammar. If it is not declared, it will be used by defaultproto2Grammar. At the same time, I suggest to use v2 or v3, which version should be declared

2. DefinitionSearchServiceAn RPC service that contains RPC methodsSearch, with reference toSearchRequestNews, the parameter isSearchResponseMessage

3. DefinitionSearchRequestSearchResponseMessage, the former defines three fields, and each field contains three attributes: type, field name, and field number

4. Protobuf compiler will generate Service Interface Code and Stubs of corresponding languages according to the different languages selected.

Finally, here is only a brief introduction of grammar, please turn right in detail.Language Guide (proto3)

Data type

.proto Type C++ Type Java Type Go Type PHP Type
double double double float64 float
float float float float32 float
int32 int32 int int32 integer
int64 int64 long int64 integer/string
uint32 uint32 int uint32 integer
uint64 uint64 long uint64 integer/string
sint32 int32 int int32 integer
sint64 int64 long int64 integer/string
fixed32 uint32 int uint32 integer
fixed64 uint64 long uint64 integer/string
sfixed32 int32 int int32 integer
sfixed64 int64 long int64 integer/string
bool bool boolean bool boolean
string string String string string
bytes string ByteString []byte string

The main difference between v2 and v3

  • There is logic to delete the field of the original value field
  • Delete required field
  • To delete the optional field, the default is
  • Delete default field
  • Delete the extended feature and add Any type to replace it
  • Support for Removing unknown Fields
  • NewJSON Mapping
  • Add support for Map types
  • Repair unknown type of enum
  • Repeated uses packed encoding by default.
  • New language implementations (C #, JavaScript, Ruby, Objective-C) are introduced

The above are common functions involved in daily life. If you want to know more about them, you can read them.Protobuf Version 3.0.0

Compared with Protobuf, why not use XML?

  • Simpler
  • The data description file only needs the original 1/10 to 1/3
  • The resolution speed is 20 to 100 times of the original speed.
  • The ambiguity is reduced
  • A more user-friendly data access class is generated.

Iii. gRPC

Introduction

GRPC is a high performance, open source and universal RPC framework, designed for mobile and HTTP/2

Multilingual

  • C++
  • C#
  • Dart
  • Go
  • Java
  • Node.js
  • Objective-C
  • PHP
  • Python
  • Ruby

Characteristics

1、HTTP/2

2、Protobuf

3. Client and server are based on the same IDL

4. Good Support for Mobile Networks

5. Support multilingual

Overview

image

explain

1. the client (gRPC Sub) calls method a to initiate RPC call

2. use Protobuf for object serialization compression (IDL) on request information

3. After receiving the request, the server (gRPC Server) decodes the request body, performs service logic processing and returns it

4. use Protobuf to serialize and compress the response results (IDL)

5. The client receives the response from the server and decodes the request body. Callback the called A method, wake up the client that is waiting for a response (blocking) and return the response result

Example

In this section, we will simply show you the sample code of gRPC’s client and server. I hope you have a basic impression first and will introduce it in detail in the next section.

Build and start the server

lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
        log.Fatalf("failed to listen: %v", err)
}

grpcServer := grpc.NewServer()
...
pb.RegisterSearchServer(grpcServer, &SearchServer{})
grpcServer.Serve(lis)

1. Monitor the designated TCP port for accepting client requests

2. Create an instance object of gRPC Server

3. Registration of gRPC Server Internal Service and Routing

4. Serve () calls the server to perform blocking wait until the process is terminated or called by Stop ()

Create client

var opts []grpc.DialOption
...
conn, err := grpc.Dial(*serverAddr, opts...)
if err != nil {
    log.Fatalf("fail to dial: %v", err)
}

defer conn.Close()
client := pb.NewSearchClient(conn)
...

1. Create gRPC Channel to communicate with gRPC Server (server address and port are required as parameters)

2. Set DialOptions credentials (for example, TLS, GCE credentials, JWT credentials)

3. create Search Client Stub

4. Call the corresponding service method

Thinking questions

1. What scenarios are not suitable for Protobuf, but JSON and XML?

2. what is the packed code mentioned in Protobuf section?

Summary

In the beginning, I used the shortest possible description to introduce you to the necessary knowledge points for the next step.
I hope you can get something. It is best to go to the reference office I gave you for in-depth study.

Series catalog

Reference material