当前位置:网站首页>Introduction to grpc for cloud native application development
Introduction to grpc for cloud native application development
2022-07-08 01:37:00 【InfoQ】
What is? gRPC

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
- 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
- install golang Of proto tool kit :
go get -u github.com/golang/protobuf/proto
- 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:
go get google.golang.org/grpc
main
package main
import (
"log"
"net"
)
func main() {
lis, err := net.Listen("tcp", ":8000")
if err != nil {
log.Fatalf("Fail to listen: %v", err)
}
}
package main
import (
"log"
"net"
"google.golang.org/grpc"
)
func main() {
lis, err := net.Listen("tcp", ":8000")
if err != nil {
log.Fatalf("Fail to listen: %v", err)
}
grpcServer := grpc.NewServer()
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("Fail to serve: %v", err)
}
}
Add some features
client.proto
syntax = "proto3"; // Agreement for proto3
package chat;
// Define send request information
message Message {
// Define the parameters to send
// Parameter type Parameter name Identification number ( Do not repeat )
string body = 1;
}
// Define our services ( Multiple services can be defined , Each service can define multiple interfaces )
service ChatService {
rpc SayHello(Message) returns (Message) {}
}
.proto
ChatService
SayHello
.proto
$ protoc --go_out=plugins=grpc:chat chat.proto
chat/chat.pb.go
server.go
package main
import (
"fmt"
"log"
"net"
"github.com/tutorialedge/go-grpc-beginners-tutorial/chat"
"google.golang.org/grpc"
)
func main() {
fmt.Println("Go gRPC Beginners Tutorial!")
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 9000))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := chat.Server{}
grpcServer := grpc.NewServer()
chat.RegisterChatServiceServer(grpcServer, &s)
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %s", err)
}
}
package chat
import (
"log"
"golang.org/x/net/context"
)
type Server struct {
}
func (s *Server) SayHello(ctx context.Context, in *Message) (*Message, error) {
log.Printf("Receive message body from client: %s", in.Body)
return &Message{Body: "Hello From the Server!"}, nil
}
chat.proto
$ go run server.go
Go gRPC Beginners Tutorial!
localhost:8000
stay Go Build gRPC client
client.go
package main
import (
"log"
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/tutorialedge/go-grpc-beginners-tutorial/chat"
)
func main() {
var conn *grpc.ClientConn
conn, err := grpc.Dial(":8000", grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %s", err)
}
defer conn.Close()
c := chat.NewChatServiceClient(conn)
response, err := c.SayHello(context.Background(), &chat.Message{Body: "Hello From Client!"})
if err != nil {
log.Fatalf("Error when calling SayHello: %s", err)
}
log.Printf("Response from server: %s", response.Body)
}
$ go run client.go
2022/07/07 23:23:01 Response from server: Hello From the Server!
Installation problems
go get google.golang.org/grpc

- 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
- Go gRPC Beginners Tutorial
- 《gRPC And cloud native application development 》
边栏推荐
- Redis集群
- 2021 tea master (primary) examination materials and tea master (primary) simulation test questions
- STM32GPIO口的工作原理
- Redis master-slave replication
- Redux使用
- The communication clock (electronic time-frequency or electronic time-frequency auxiliary device) writes something casually
- Basic implementation of pie chart
- 5、離散控制與連續控制
- Common operations of numpy on two-dimensional array
- NPDP在国内有认可度吗?看一看就明白了!
猜你喜欢
Getting started STM32 -- how to learn stm32
About how USRP sets the sampling frequency below the minimum sampling frequency reached by the hardware
写一个纯手写的qt的hello world
Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
Matlab code about cosine similarity
2022 safety officer-c certificate examination paper and safety officer-c certificate simulated examination question bank
2022 free test questions of fusion welding and thermal cutting and summary of fusion welding and thermal cutting examination
如何让导电滑环信号更好
4、策略学习
Led serial communication
随机推荐
3、多智能体强化学习
Leetcode notes No.7
How to get the first and last days of a given month
Redis cluster
Break algorithm --- map
GBASE观察 | 数据泄露频发 信息系统安全应如何守护
A little experience from reading "civilization, modernization, value investment and China"
跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
The beauty of Mathematics -- the principle of fine Fourier transform
Macro definition and multiple parameters
2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills
2022 chemical automation control instrument examination summary and chemical automation control instrument simulation examination questions
Mat file usage
Talk about smart Park
Write a pure handwritten QT Hello World
The Ministry of housing and urban rural development officially issued the technical standard for urban information model (CIM) basic platform, which will be implemented from June 1
Anaconda3 download address Tsinghua University open source software mirror station
Continued from the previous design
2022 free test questions of fusion welding and thermal cutting and summary of fusion welding and thermal cutting examination
Blue Bridge Cup embedded (F103) -1 STM32 clock operation and led operation method