当前位置:网站首页>Introduction to grpc for cloud native application development
Introduction to grpc for cloud native application development
2022-07-08 02:04:00 【51CTO】
gRPC is a modern, open source remote procedure call (RPC) framework that can run anywhere.
What is? gRPC
gRPC Developed by Google , It's a language neutral 、 Platform neutrality 、 Open source remote call procedure .
What is a remote call procedure ? The simple understanding is to expose the methods that local applications call to other applications .gRPC Is an interprocess communication technology , Can be used to connect 、 call 、 Operate and tune distributed heterogeneous applications . Also like RPC The characteristics of the application are the same : Just like calling a local function .
characteristic
- gRPC It's a high performance 、 Open source and generic RPC frame , Facing mobile and HTTP/2 Design , Bring about things like two-way flow 、 Flow control 、 The head of compression 、 single TCP Multiple reuse requests on the connection . These features make it better on mobile devices , More power and space saving .
- stay gRPC The client application can directly call the method of the server application on a different machine just like calling the local object , Make it easier for you to create distributed applications and services .
- gRPC By default protocol buffers, This is a Google A set of mature structure data serialization mechanism of open source , Its function and XML、json similar , But it is in binary format , Good performance 、 Efficient ( shortcoming : Poor readability ).
gRPC and REST difference
It is similar to REST API signal communication , Through it , You can effectively expose the functions in the application to use HTTP Connect to other applications as communication media .
although REST and gRPC A bit similar , But you should note that there are differences in the way they work :
- gRPC Use HTTP/2 agreement , and REST Use HTTP 1.1
- gRPC Use protocol buffer data format , Not usually in REST API Standards used in JSON data format
- Use gRPC, You can use HTTP/2 function , For example, server-side streaming 、 Client streaming or even two-way streaming .
Go Build a gRPC The server
We started at Go Define a very simple gRPC The server starts . Once we have a simple server up and running , We can start to create an interactive gRPC client .
gRPC Microservices can be implemented , Split a large project into several small and independent business modules , That is service , Efficient communication between services protobuf Agreement to proceed RPC call ,gRPC By default protocol buffers , This is a google A set of mature structure data serialization mechanism of open source ( Of course, other data formats such as JSON ). It can be used proto files establish gRPC service , use message Type to define method parameters and return types
- install golang Of proto tool kit :
- At the beginning of establishing gRPC Before , Make sure it is installed Protocol Buffers v3:
go get -u github.com/golang/protobuf/protoc-gen-go
- stay Go Install in gRPC:
Then write a server , By monitoring TCP Port of connection . As follows main
function :
Next , We need to golang.org Import official gRPC package , So that we can create a new gRPC The server , Then register the endpoint we want to expose , Then through the existing..., which we defined above TCP Connect to provide services :
This is the use of go The most basic preparation gRPC The server , Now the function is still very limited .
Add some features
Then write a client to interact with the previous server , Create a client.proto
file :
This .proto
The document discloses our ChatService
, It has a separate SayHello
function , It can be written by anyone in any language gRPC Client calls .
these .proto
Definitions are usually shared between clients of various shapes and sizes , So that they can generate their own code to interact with our gRPC Server communication .
Let's use protoc Tool generation Go specific gRPC Code :
You will see that this will generate a chat/chat.pb.go
file , This file will contain the generated code , So that we can easily call . Let's update our server.go
To register our ChatService, As shown below :
Then we will have to define SayHello Method , This method will receive a message , Read the body of the message , Then return its own message :
If we want to work for our gRPC The server defines more advanced functions , Then we can define a based on us Server A new method of structure construction , Then add the name of the function to our chat.proto
In file , So that our application can expose it as something else gRPC The client can access .
After these final changes , Let's try running our server :
breathtaking ! We are now on our machine localhost:8000
A brand new 、 Shiny new gRPC The server !
stay Go Build gRPC client
Now our server is up and running , Let's see how to build a simple client that can interact with it . Update client.go
file :
When we run it , We should see that our client received a very good message from the server Hello news , As shown below :
We have successfully created a very simple gRPC client , It can now work with our new gRPC Server communication !
Installation problems
If you encounter go get google.golang.org/grpc
Installation error , as follows :
Replace :
- git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
- git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net
- git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text
- go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
- git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto
- cd $GOPATH/src/
- go install google.golang.org/grpc
summary
Through this article, we have learned how to Go Build a simple gRPC Client and server . We built a basic server , It accepts incoming messages from clients , Then return the response to these clients .
Reference link :
边栏推荐
- JVM memory and garbage collection-3-object instantiation and memory layout
- 剑指 Offer II 041. 滑动窗口的平均值
- #797div3 A---C
- Keras' deep learning practice -- gender classification based on inception V3
- Dataworks duty table
- 神经网络与深度学习-5- 感知机-PyTorch
- 云原生应用开发之 gRPC 入门
- Why did MySQL query not go to the index? This article will give you a comprehensive analysis
- 谈谈 SAP 系统的权限管控和事务记录功能的实现
- Is NPDP recognized in China? Look at it and you'll see!
猜你喜欢
Why did MySQL query not go to the index? This article will give you a comprehensive analysis
JVM memory and garbage collection-3-direct memory
Flutter 3.0框架下的小程序运行
adb工具介绍
系统测试的类型有哪些,我给你介绍
nmap工具介紹及常用命令
How to fix the slip ring
谈谈 SAP 系统的权限管控和事务记录功能的实现
很多小伙伴不太了解ORM框架的底层原理,这不,冰河带你10分钟手撸一个极简版ORM框架(赶快收藏吧)
【SolidWorks】修改工程图格式
随机推荐
[recommendation system paper reading] recommendation simulation user feedback based on Reinforcement Learning
Ml self realization / linear regression / multivariable
Many friends don't know the underlying principle of ORM framework very well. No, glacier will take you 10 minutes to hand roll a minimalist ORM framework (collect it quickly)
Version 2.0 of tapdata, the open source live data platform, has been released
Redission源码解析
进程和线程的退出
分布式定时任务之XXL-JOB
XXL job of distributed timed tasks
Nanny level tutorial: Azkaban executes jar package (with test samples and results)
burpsuite
How to make enterprise recruitment QR code?
Ml self realization / logistic regression / binary classification
Node JS maintains a long connection
EMQX 5.0 发布:单集群支持 1 亿 MQTT 连接的开源物联网消息服务器
剑指 Offer II 041. 滑动窗口的平均值
魚和蝦走的路
数据链路层及网络层协议要点
Ml backward propagation
Introduction à l'outil nmap et aux commandes communes
Application of slip ring in direct drive motor rotor