In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article Xiaobian introduces in detail for you ".net Core microservice rpc framework GRPC communication method is", the content is detailed, the steps are clear, the details are handled properly, I hope this ".net Core microservice rpc framework GRPC communication method is what" the article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
What is rpc?
Rpc, the full name Remote Procedure Call, allows you to invoke remote services as if you were calling a local method. The front end can call the back-end method, and the back-end can also call the front-end method. In fact, this concept is not unfamiliar, the last article on the real-time application of web also mentioned this concept, and implemented the operation of front-end and back-end Intermodulation.
Before we can officially start using grpc, we also need to master the concept of protocol buffer.
What is grpc?
Grpc comes from the famous Google and is incubated by the CNCF Foundation (docker and K8s also come from the same foundation). It is a high-performance, open source, general-purpose rpc framework through which you can define rpc requests and responses. It is based on http/2, full-duplex communication, low latency, high efficiency, support flow, easy insertion of authentication, load balancing, monitoring logs and so on. The point is that it can also operate across languages. As long as the server side or client side is written in a language it supports, it can operate across languages. Officially supported development languages:
Why use protocol buffer?
Because grpc adopts the contract-first API development mode and defaults to protocol buffer as the interface design language, we can define messages and services in the proto file. Of course, as the default option, it naturally has advantages, mainly these:
1. The grammar is simple and easy to use.
2. It can generate code for all (officially) mainstream development languages.
3. Binary format, high efficiency and suitable for big data transmission at the same time.
After understanding the basics, let's refer to a small demo:
Syntax = "proto3"; import "aaa.proto"; option csharp_namespace = "gRPCApiDemo.Protos"; package Demo;service Math {rpc Add (AddRequest) returns (AddRespones);} message AddRequest {int32 astat1; int32 baked 2;} message AddRespones {int32 axi1; repeated int32 baked 2; reserved 3 to 4 to 10; reserved "phone";}
Read it:
Syntax: declares the syntax currently in use
Import: introducing other proto
Option csharp_namespace: the namespace of the packaged class with high priority
Package: the namespace of the packaged class, with low priority
Service: declare the service
Rpc Add (AddRequest) returns (AddRespones): declares a rpc call that receives the AddRequest message and returns the AddRespones message.
Message: declaration message
Int32: data type, commonly used are int64, float, string, bool and so on. If you are interested, you can check it out.
Aroom1: if you think of an as a field name, then 1 is equivalent to an alias for the field name.
Repeated: repeatable field. If you give more than one value to b, then b is almost equivalent to an array of type int, and the order is preserved.
Reserved: keep the field and declare that future field names and aliases are no longer allowed. Note that field names and aliases cannot be declared together.
Compile proto
The main purpose of proto compilation is to generate code.
The first is the tool. Click here to select the plug-in suitable for your system, as shown in the figure:
After downloading, randomly find a directory to extract, and then add the bin directory to the environment variable:
If you type protoc in the console and you can see a bunch of messages pop up, it means that the installation is successful:
Before we use protoc to compile the proto file into a c # file, we need to do this again:
1. Specify the proto project path.
You can use rotoc-- IPath (or-- proto_path) or simply open the console to go to the protoc installation directory.
2. Specify the type and path of the generated file.
Protoc-- csharp_out=cs grpcApi.proto / / if you have multiple proto files that you want to generate in batch, you can use * .proto
The cs above is the name of the folder I created (this folder is built under the proto project directory), and grpcApi.proto is a self-built proto file. The execution result is shown in the figure:
After success, the cs file will be generated in the specified directory, as shown in the figure:
The generated file has the same name as the proto file, except that the first letter is automatically capitalized. Open it and have a look:
Well, there's a limit to what you can understand, so just take a look.
However, there is one thing to pay attention to: do not change this cs file manually, do not change it manually!
Read here, this article ".net Core micro services rpc framework GRPC communication methods" article has been introduced, want to grasp the knowledge of this article also need to practice and use in order to understand, if you want to know more about the article, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.