Thoroughly master the elegant gRPC interface debugging method

Currently, there are very limited interface debugging and management tools compatible with  gRPC  interfaces on the market, and gRPC has been widely used in microservice architectures, and it is foreseeable that it will become more and more popular.

As an industry-leading interface management tool, Apifox has launched the gRPC interface debugging capability , which is fully compatible with the following four call types:

  • Unary: unary call
  • Server Streaming: server stream
  • Client Streming: client stream
  • Bidirectional Streaming: bidirectional stream

Maybe you are not familiar with the gRPC interface, so we might as well have a brief understanding first!

What is gRPC?

gRPC is a modern open source high-performance RPC Remote Procedure  Calls (Remote Procedure Calls)  framework  developed by Google . It has good compatibility and can run in multiple development environments.

Application scenarios of gRPC

Compared with the current mainstream HTTP API interface, the gRPC interface adopts the leading HTTP/2 underlying architecture design as the underlying transmission protocol , which can be used in large-scale data transmission scenarios (such as video streaming) and microservice architecture scenarios where a large number of services call each other Next show your skills.

Data exchange adopts the lightweight Protobuf serialization protocol , which enables it to provide faster data processing speed in resource-constrained scenarios (commonly found in mobile devices such as mobile phones), while reducing the amount of data transmitted by the network and saving network bandwidth, thereby Reduce power consumption and improve battery life.

Before officially introducing gRPC, we might as well figure out what RPC is and what it does, which is very helpful for subsequent understanding.

What are RPCs?

The RPC  protocol is an implementation of remote procedure calls . Suppose there are two servers A and B now. A service deployed on server A wants to call another process running on server B. However, because the two services are not in the same memory space, they cannot be called directly, so the calling effect must be achieved through network communication.

To establish network communication is nothing more than initiating a TCP connection at the transport layer. TCP's handshake mechanism ensures that data packets can be reliably transmitted to the other party, and it has the following three characteristics: connection -oriented, reliable, and byte-based . Both of the previous two features are suitable for this scenario, but the only point that is based on byte streams may be questionable. Why?

Because it has no boundaries . The byte stream is essentially the data flowing in the two-way channel of the transport layer, that is, the binary 0 1 data that the computer can understand. So when the sender uses TCP to send the characters "Nanjing City" + "Yangtze River Bridge", the receiver may receive "Nanjing Mayor" + "Jiangjiang Bridge", or "Nanjing Yangtze River Bridge".

The oversimplified TCP connection process cannot guarantee the uniqueness and certainty of information, so it is necessary to define the message header and message body in the data , and the sender and receiver agree on this communication method, thus deriving the HTTP protocol and RPC calls and other schemes essentially define standardized data transfer and call methods to avoid information distortion .

For example, there is now a shopping website with  two microservices , order service and user service (such as account management) . The order service needs to query some data under the user service, but the two are isolated. At this time, the order service must obtain data through remote calls.

In the example in the figure below, the server (user service)  only needs to expose a getConsumerByld() function that can call the database, and the client (order service)  can use RPC to directly call the getConsumerByld() function and obtain all the desired response. RPC successfully hides the complexity of internal communication and provides a stable and unified interface for both parties, so that developers only need to pay attention to the business logic and not the details of the underlying network communication.

And gRPC is essentially an RPC framework released and open sourced by Google .

What are the advantages of gRPC?

Advanced Transport Protocol

HTTP/2 was released a little earlier than gRPC in 2015, so gRPC naturally uses this advanced transport protocol as the underlying foundation, so that it has more efficient transport performance , and can also support streaming calls for many real-time data It provides support for transmission scenarios (such as stock data, voice communication, game scenarios), and is naturally ahead of many ancient RPC frameworks released at the turn of the millennium. A connection pool is established when a connection is initiated, which can significantly improve network request performance when facing multiple connections.

Scientific Design Concept

At the same time, gRPC uses the smaller Protobuf serialization protocol to save structure data. Protocol Buffers is a cross-language, cross-platform data structure serialization protocol. It is more lightweight than JSON or XML protocols and therefore more efficient in transferring data .

At the same time, this also determines that the gRPC interface must follow the API First concept, because the service interface needs to be defined in the file before the development work starts, including how the client uses the service and the parameters that need to be passed when calling. The gRPC interface can automatically generate the communication method between the client and the server according to the defined .proto file, which greatly simplifies the development process .

good compatibility

gRPC does not depend on any specific technology or language stack, so it can be widely used in different development environments. It supports multiple programming languages, including Java, C++, Python, Go and other modern and popular programming languages. It can easily communicate between different languages, which provides strong support for multi-language microservice architecture.

Although gRPC has many advantages, most external users are more familiar with the HTTP interface (this also leads to the ecological prosperity of gRPC is not as good as the REST/HTTP protocol), so the main application scenario of gRPC is the communication between microservices within the team and Connections are not suitable for exposing services for external consumption . And if you want to successfully call gRPC, it may also involve the parameter passing process of the internal system. Another relatively big disadvantage is that browsers do not support gRPC service calls , and need to use extensions or professional interface calling tools (such as Apifox) to carry out interface collaboration.

Apifox gRPC interface debugging function

Apifox  has launched the gRPC interface debugging function. We will briefly demonstrate how to create a new gRPC project in Apifox and initiate debugging for the interface through a sample scenario.

Note: The gRPC interface management function requires Apifox version number to be greater than or equal to 2.3.0  .

Click the "New Project" button in Apifox, select the gRPC type, fill in the project name and tap the "New" button.

Import a .proto file that defines the services, methods, and messages used by the gRPC interface. You can drag and drop files into it or use the file's online URL to complete the import.

Apifox will generate corresponding interface information based on the content of the .proto file. The sample interface comes with four calling methods: unary call, server stream, client stream and bidirectional stream.

make a unary call

A unary call refers to sending a message in a request and receiving a message in response. Compared with JSON, gRPC uses Protocol Buffers (ProtoBuf) as the default serialization framework, which can express data more compactly and improve transmission speed. And gRPC uses HTTP/2 as the underlying transport protocol, and directly applies the HPACK compression algorithm to process the information in the Header, reducing the size of the header during network transmission, thereby saving bandwidth resources. This is very friendly for scenarios that transmit small data or require real-time response.
Select the SayHello method, and fill in grpcb.in:9000 in the interface address. Then click the "Automatically Generate" button to generate the request body, and tap "Call" to view the returned response.

In Apifox, you can easily extract the interface address to the "environment", so that other members of the team or other interfaces in the project can initiate call requests.

Initiate a streaming call

Server-side streams, client-side streams, and bidirectional streams are all referred to as streaming calls. It allows a persistent duplex streaming connection to be established between the client and the server, and data is read and written through streaming. The main feature of streaming calls is that they can send and receive multiple messages on one connection, support continuous interaction and large-scale data transmission, and are often used in exchange stock data, video and voice communication, and online game scenarios.

server stream

Just like what it means in the icon, the server stream means: send once and return multiple response data. For example, subscribe to the transaction price data of all stocks within one minute.

client stream

In this mode, the client can continuously send multiple request messages to the server without waiting for an immediate response from the server. After initiating the call, you can continue to fill in the request information in the Message, and then click the "Send" button. After the server has processed all requests, it sends a single response message to the client.

bidirectional flow

Bidirectional streams allow persistent bidirectional communication between a client and server, and multiple messages can be transmitted simultaneously. Commonly used in online games and real-time video call software, it is suitable for real-time communication and large-scale data transmission scenarios. After initiating the call, the client and the server will continue to maintain a session, and get a real-time response after sending different request content.

gRPC interface collaboration

Apifox can render gRPC interface documents that are more suitable for human reading based on .proto files, making interfaces easier to collaborate in teams. You can click the menu button on the right side of the interface to get the collaboration link and share it with other team members to align the debugging method of the interface.

Apifox's gRPC interface debugging function is in the Beta public testing stage . The current function is only the first step we have taken, and it will continue to be iteratively updated in the future. As an API integrated collaboration platform, Apifox  hopes to provide the same excellent experience for different API technologies.

Guess you like

Origin blog.csdn.net/m0_71808387/article/details/131592989