【译】gRPC vs HTTP APIs

This translation from ASP.NET Blog | GRPC VS HTTP APIs , author James, translator Edison Zhou .

Written at the beginning

  Now, ASP.NET Core enables developers to build gRPC service. gRPC is a remote procedure call framework, focusing on performance and developer productivity. ASP.NET Core 3.0 is integrated gRPC, so you can use your existing ASP.NET Core logging system in conjunction with system configuration, authentication mode to build a new gRPC service.

 

  This article will be gRPC with JSON-based HTTP API compared to discuss the advantages and disadvantages of gRPC, and when you can use gRPC build applications.

gRPC advantage

1, enhanced developer productivity

  Use gRPC service, the client application can invoke methods on direct service applications on different computers, as if it were a local object. gRPC defined based on the idea of service, specified by passing parameters and return type of the remote method invocation. Server, implement this interface and run gRPC service to handle client calls. The client, using strongly typed gRPC client, the client provides the same method server.

  gRPC goal to achieve perfect support for code generation. gpro core file is developed .proto file , the file using Protobuf Interface Definition Language (IDL) and defined gRPC contract service messages, for example, as shown in the following document Greet.proto:

Greet.proto

// The greeting 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;
}

   Protobuf IDL is a language-independent syntax, so it can be shared between the client and service gRPC different language. gRPC frame using .proto file to generate serving base classes, messages, and full client's code encoding. For example, here use to generate strongly typed client to invoke the service Greeter:

Program.cs

var channel = GrpcChannel.ForAddress("https://localhost:5001")
var client = new Greeter.GreeterClient(channel);

var reply = await client.SayHelloAsync(new HelloRequest { Name = "World" });
Console.WriteLine("Greeting: " + reply.Message);

  By between the server and the client share .proto file , from end to end you can generate messages and client code. The client code generation eliminates duplicate messages on the client and server definition and create a strongly typed client for you. Without writing a client, it can save a lot of development time for developers in applications with many service.

2, High Performance

  gRPC message using Protobuf (an effective binary message format) serialization . Protobuf on the server and the client can be achieved very quickly serialization. Protobuf sequence of messages generated by the load is small, it is very important in the case of limited bandwidth mobile applications and the like.

  gRPC require HTTP / 2, which is the main version of HTTP, compared with HTTP 1.x, it has significant performance advantages:

  • Binary framing and compression. HTTP / 2 protocol to send and receive aspects are compact and efficient.
  • In a single TCP connection multiplexing a plurality of HTTP / 2 calls. Multiplexing to eliminate the application layer of the head of line blocking .

3, real-time services

  Long-term stream of real-time communication provides the basis for HTTP / 2 is, GRPC to provide good support for streaming HTTP / 2 through.

  gRPC service supports all stream combinations:

  • One yuan (no streaming)
  • The server to the client stream
  • Client-to-server stream
  • Bi-directional flow

  Please note that, the concept of broadcasting a message to a plurality of connection itself is not naturally present in the gRPC. For example, in a chat room, a new chat message should be sent to all clients in the chat room, the new call will require each gRPC chat messages are streamed to the client. SignalR is a suitable framework of this program, SignalR have the concept of a persistent connection, and built-in support for broadcast messages.

4, measure and cancel timeout mechanism

  gRPC allows the client to specify the maximum amount of time they are willing to wait for an RPC to complete. The time limit is sent to the server, the server can decide whether it exceeds the deadline to take any action. For example, the server may cancel an ongoing gRPC / HTTP / database request after a timeout.

  By calling the spread of child gRPC maximum time limit and cancel mechanisms to help enforce resource limits on behavior.

gRPC shortcomings

Limited browser support

  gRPC excellent cross-platform support! Today, gRPC have achieved a variety of programming languages. However, you still can not call gRPC services directly from the browser. gRPC is used extensively HTTP / 2, but no browser supports the client's Web request gRPC level of control required. For example, the browser does not allow the caller requires the use of HTTP / 2, or provide access to the frame under the HTTP / 2 protocol.

  gRPC-Web is another technology gRPC team provides limited support gRPC in your browser. gRPC-Web consists of two parts: a support for all modern browsers JavaScript client and a server on the gRPC-Web proxy. gRPC-Web client calls the agent, the agent will forward the request to gRPC gRPC server.

  gRPC-Web does not support all gRPC functions. For example, it does not support client-side and two-way flow, and support for streaming server is also very limited.

Unreadable

  HTTP API using JSON requests sent in text form, fit and beneficial to read and create.

  By default, gRPC Protobuf encoded message used. Although Protobuf possible to efficiently transmit and receive, but it is not very readable binary format. Protobuf requires .proto file message interface described in the specified order deserialized correctly. In addition, the need for additional tools to analyze Protobuf on the network payload and write requests manually.

  Fortunately, as it has been some servers reflection and gRPC command line tool functions like to assist Protobuf binary message. Further, Protobuf message supports conversion between a JSON . Built-in JSON conversion provides an effective method for debugging when Protobuf between the message and JSON-readable form of interconversion.

gRPC proposal

  gRPC very suitable for the following situations:

  • Micro-services  - gRPC designed for low latency and high throughput communication design. gRPC useful for efficient lightweight micro vital services.
  • Point real-time communication  - gRPC has excellent support for bidirectional flow. gRPC services in real-time push messages without polling.
  • Multilingual environment  - gRPC tool supports all popular development language, so gRPC is ideal for multi-language environments.
  • Network constrained environments  - gRPC message using a lightweight message format Protobuf serialization. gRPC size of the message is always less than the same level of JSON messages.

in conclusion

  gRPC is a ASP.NET Core developers powerful new tools. Although gRPC can not completely replace the HTTP API, but in some cases can provide higher productivity and performance advantages.

  GRPC on ASP.NET Core is now already available! If you would like more information about gRPC, check out the following resources:

  In addition, there also recommend Men translator big Chengdu Xiao Chen Master 's latest blog series: ASP.NET Core GRPC entry-family bucket  .

 

Guess you like

Origin www.cnblogs.com/edisonchou/p/edc_gRPC_vs_HTTPapis_translation.html