当前位置:网站首页>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 》
边栏推荐
- Common fault analysis and Countermeasures of using MySQL in go language
- MATLAB R2021b 安装libsvm
- Is it safe to open an account on your mobile phone for small amount of stock speculation?
- Kaptcha generates verification code on Web page
- 2022 safety officer-b certificate examination question bank and safety officer-b certificate simulation test questions
- Kafka connect synchronizes Kafka data to MySQL
- Understanding of maximum likelihood estimation
- 2022 free test questions of fusion welding and thermal cutting and summary of fusion welding and thermal cutting examination
- About snake equation (5)
- How does Matplotlib generate multiple pictures in turn & only save these pictures without displaying them in the compiler
猜你喜欢
Gnuradio3.9.4 create OOT module instances
Gnuradio operation error: error thread [thread per block [12]: < block OFDM_ cyclic_ prefixer(8)>]: Buffer too small
Different methods for setting headers of different pages in word (the same for footer and page number)
2022 free test questions of fusion welding and thermal cutting and summary of fusion welding and thermal cutting examination
Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
如何让导电滑环信号更好
break net
Running OFDM in gnuradio_ RX error: gr:: Log: info: packet_ headerparser_ b0 - Detected an invalid packet at item ××
Content of one frame
MATLAB R2021b 安装libsvm
随机推荐
2021-04-12 - new features lambda expression and function functional interface programming
break net
How does Matplotlib and PIL image integrate and save multiple pictures into one picture
Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list
Different methods for setting headers of different pages in word (the same for footer and page number)
Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
Leetcode notes No.21
2021 tea master (primary) examination materials and tea master (primary) simulation test questions
Understanding of expectation, variance, covariance and correlation coefficient
子矩阵的和
碳刷滑环在发电机中的作用
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
Matlab code on error analysis (MAE, MAPE, RMSE)
LaTeX 中 xcolor 颜色的用法
Four digit nixie tube display multi digit timing
2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
Basic realization of line graph
Smart agricultural technology framework
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布