当前位置:网站首页>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/grpcmainpackage 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.protosyntax = "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) {}
}.protoChatServiceSayHello.proto$ protoc --go_out=plugins=grpc:chat chat.protochat/chat.pb.goserver.gopackage 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:8000stay Go Build gRPC client
client.gopackage 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 》
边栏推荐
- How does Matplotlib generate multiple pictures in turn & only save these pictures without displaying them in the compiler
- Coordinate conversion of one-dimensional array and two-dimensional matrix (storage of matrix)
- Guojingxin center "APEC education +" Shanghai Jiaotong University Japan Cooperation Center x Fudan philosophy class "Zhe Yi" 2022 New Year greetings
- 2021-03-06 - play with the application of reflection in the framework
- 从cmath文件看名字是怎样被添加到命名空间std中的
- Connect to the previous chapter of the circuit to improve the material draft
- Gnuradio3.9.4 create OOT module instances
- Transportation, new infrastructure and smart highway
- Content of one frame
- Probability distribution
猜你喜欢

QT -- package the program -- don't install qt- you can run it directly

4. Strategic Learning

2021 tea master (primary) examination materials and tea master (primary) simulation test questions

About snake equation (1)

Gnuradio transmits video and displays it in real time using VLC

Redis master-slave replication

4、策略學習

Content of one frame

用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟

QT build with built-in application framework -- Hello World -- use min GW 32bit
随机推荐
2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
4. Strategic Learning
5、离散控制与连续控制
After modifying the background of jupyter notebook and adding jupyterthemes, enter 'JT -l' and the error 'JT' is not an internal or external command, nor a runnable program
2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills
regular expression
On the concept and application of filtering in radar signal processing
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
碳刷滑环在发电机中的作用
城市土地利用分布数据/城市功能区划分布数据/城市poi感兴趣点/植被类型分布
Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
Anaconda3 tutorial on installing and adding Tsinghua image files
Understanding of expectation, variance, covariance and correlation coefficient
Redis cluster
npm 內部拆分模塊
Kafka connect synchronizes Kafka data to MySQL
2022 safety officer-c certificate examination summary and safety officer-c certificate reexamination examination
Redis master-slave replication
Connect to the previous chapter of the circuit to improve the material draft
Transportation, new infrastructure and smart highway