[] ASP.NET Core learning remote procedure call - gRPC use

 This article describes the gRPC use, will introduce the following aspects

  1. What is RPC
  2. When do you need RPC
  3. How to use gRPC

What is RPC

RPC is a Remote Procedure Call, translated over a remote procedure call. It is a process of communication between technology-based Client-Server mode, let the program use the same method as local calls, without having to go to the details of how to achieve it relationship.

The above is my understanding, understanding fear is wrong or inaccurate expression, the following quote from Wikipedia

维基百科 a remote procedure call (RPC) is when a computer program causes a procedure (subroutine) to execute in a different address space (commonly on another computer on a shared network), which is coded as if it were a normal (local) procedure call, without the programmer explicitly coding the details for the remote interaction

Internet to find a graph comparing the image described process RPC call

 

When do you need RPC

RPC is to solve inter-process communication (can be the same server, it can be different inter-process servers, but usually the process of communication between different servers within the network). 

Resolve inter-process communication, Web Api can be resolved, why do you need RPC? I think we can from the following points

1. Web Api is based on HTTP, RPC can be HTTP, can be TCP, or even based on Socket, RPC communications framework often hidden details, we perceive no use

2. Web Api usually based on JSON format, XML format, which is readable and strong, but the attendant is required to transfer process metadata data transmission also take into account

3. Web Api more scenarios is to provide an interface defined above the well, called by client demand, RPC is usually required to call and provider communicate with defined interfaces

So PRC more using the following scene

  1. High performance requirements of communications
  2. Micro Services
  3. Point to Point Communications

gRPC use

 gRPC Google is open source, high-performance RPC framework, has the following characteristics

  1. Modern high-performance lightweight RPC framework.
  2. Priority API development agreement, default protobuf, allows nothing to do with language. (This involves two, 1 for interface development, and not dependent on particular abstract, 2 can be implemented in different languages ​​cooperate)

  3. Protobuf using binary serialization reduce the use of the network. (To reduce network transmission)

  4. Multilingual tool can be used to generate a strong type server and the client.

  5. Support clients, servers, and two-way streaming call.

The following describes how to use gRPC start on the Net Core

 

A mounting dotnet-gRPC tools (for protobuf reference file to generate the client / server code)

dotnet tool install dotnet-grpc -g

Second, create a new file protobuf

syntax = "proto3";

option csharp_namespace = "gRPC.Services";

package sms;

// The greeting service definition.
service SmsSender {
  // Sends a greeting
  rpc SendSms (SmsRequest) returns (SmsResponse);
}

// The request message containing the user's name.
message SmsRequest {
  string tel = 1;
  string content = 2;
}

// The response message containing the greetings.
message SmsResponse {
  int32 code = 1;
  string message = 2;
}

Third, create a new server

1. Create a new project gRPC

dotnet new grpc -n gRPC.Services

2. The step of introducing protobuf generated two files (can use wildcards protobuf introducing multiple files)

dotnet-grpc add-file ..\gRPC.Protos\*.proto -s Serve

3. New Service category

public  class SmsService: SmsSender.SmsSenderBase
{
    private readonly ILogger<SmsService> _logger;
    public SmsService(ILogger<SmsService> logger)
    {
        _logger = log;
    }

    public override Task<SmsResponse> SendSms(SmsRequest request, ServerCallContext context)
    {
        return Task.FromResult(new SmsResponse
        {
            Code = 1,
            The Message = " sent successfully ."
        });
    }
}
SmsSender generated server code generated by the tool

4. endpoint service class configuration grpc

app.UseEndpoints(endpoints =>
{
    endpoints.MapGrpcService<SmsService>();
});

 

Fourth, the New Client

 1. Create a new console application

dotnet new console -n gRPC.Client

2. Add the package (Google.Protobuf)

dotnet add package Google.Protobuf

3. Step two is introduced protobuf generated files (can use wildcards protobuf introducing multiple files), Note: this is the client code needs to be generated, of course, also be used to generate Both parameters

dotnet-grpc add-file ..\gRPC.Protos\*.proto -s Client

 

6.3.5 Operating

1. Run the server

2. Start the client

The client output the following information

{"Code":1,"Message":"\u53D1\u9001\u6210\u529F"}

 

VI Summary

gRPC life cycle

Client (transmission request) -> Client stub (compression / decompression) -> Client RPC Transfer (transmission / reception) -> Server RPC Transfer (receive / transmit) -> Server stub (decompression / compression) -> Server (treatment response)

gPRC help us hide the middle part, leaving only two codes (commonly known as the business logic code)

protobuf It is a kind of serialized data structure, but it is more important to define the interface, so that the server and client can be separated

Guess you like

Origin www.cnblogs.com/WilsonPan/p/12000796.html