Protobuf interface Call method for the company's new entry front end

Source: Internet
Author: User

This is a document that is guided by the company's internal guidelines, not a tutorial.

How the front end of the data plane is docked to the backend

This article contains the following content:

1. Introduction to Interface Technology

2, how to connect with the back end

I. Introduction of INTERFACE Technology

We use the GRPC to perform the data docking of the front and rear tables. Look at the official introduction

GRPC is a modern open source high performance The RPC framework that can run with any environment.

——-https://grpc.io/about/

Initially developed by Google, Grpc is a language-neutral, platform-neutral, open-source Remote procedure Call (RPC) system.

——-http://doc.oschina.net/grpc?t=58008

(not found in the civil network, if found please add to this)

Well above these ... To understand, can also be seen, below is our daily use of the protagonist.

GRPC default use protocol buffers, this is Google open source of a set of mature structure data serialization mechanism, don't worry he is like JSON is a data serialization protocol, GRPC can also use JSON as the data transfer format.

Yes, we are using protobuf, so the point is to understand this.

can go to the official website or other places to find a tutorial to see, overall is relatively simple, need to focus on the PROTOBUF protocol data type corresponding JS data type.

The way to use Protobuf is also simple:

"You only need to define the data structure that will be serialized (using the. proto file definition), and you can use the specially generated source code (using the Build tool provided by PROTOBUF) Easily use different data streams to perform read and write operations on these structured data, even if you use different languages (Protobuf's cross-language support features) "

Simply put, a protocol is developed in the. proto file and then used by the compiler to generate a language-specific data access class that contains serialization, deserialization, Toobject, set/Get parameters, and conversion into binary data.

Depending on the language version, these methods generate different files, which can be invoked according to the syntax of different languages, which is very convenient to use.

tutorial See "Reference url" at the end of the article

Second, how to connect with the back end

According to the above, the docking basic steps are as follows:

View protocol assembly parameters > methods for calling Interface classes > sending requests > Resolving return values.

The project protocol file is back-end has been written, JS Call class is also compiled, we only work on the good


is the proto library file directory, go is Golang, back-end use, Grpc-web and JS folder is JS, the front-end use. Inside the proto folder is the protocol file.

We need to view the protocol files such as API and common inside the protocol file.



If you open the Api>v1>account.proto file, you will see this


RPC behind these are the method names, the corresponding method name can be found within the account.js (usually the name and then the first letter lowercase)

The method name is followed by a parameter, the parameter is defined within the file, or it is defined within another file, and can be viewed according to the path. For example, finding registerreq within Accout.proto will find the following definition


The parameter has 5 fields, email,passkey,sig,passwd_level,verify.

The first three are either string or byte array types, the passwd_level behind is the Passwdlevel type, verify is the Verifyreq type, and the two types are custom.

Then find this class as follows:


This is the definition of passwdlevel and you can see that there are so many values. The enum at the front shows that this is an enumeration type, which can be selected based on the corresponding level.

The type of the verify is defined as follows, the context field is the back end of the return results brought to the contextual, the file is also defined, you can continue to look down to see the specific content.


Depending on the view steps above, it is OK to assemble the parameters on the layer.

The compiler generates the corresponding class based on the proto file.

For example Api>v1>account.proto compiled will correspond to generate Api>v1>account.js, JS inside will give a class, open account.js can see the first line of code goog.provide (' Proto.biss.api.v1.account.AccountClient ');

This is the class of the generated account, so we'll instantiate this class later when I call the method under account.

The method defined in Account.proto also appears in this file (usually the name and then the first letter lowercase)

The request parameter and the return data of the method need to be viewed within the corresponding file under Js>api>v1. For example registerreq the definition of this class. It also contains the action methods for the parameters (Toobject, serialization of these)

Calling the interface class and sending the request in the project already encapsulates the Send method that can be used directly. See the API's instructions for how to use it.

After returning the result, it is possible to invoke different methods based on different data return types. Previously mentioned in the inverse sequence, toobject, these methods.

Reference Address:

GRPC website Address: Https://grpc.io

GRPC Chinese document Address: http://doc.oschina.net/grpc?t=58008

PROTOBUF protocol git repository address: https://github.com/protocolbuffers/protobuf

Protobuf Document Address: https://developers.google.com/protocol-buffers/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.