当前位置:网站首页>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 》
边栏推荐
- Mat file usage
- Different methods for setting headers of different pages in word (the same for footer and page number)
- 2022 safety officer-b certificate examination question bank and safety officer-b certificate simulation test questions
- 从cmath文件看名字是怎样被添加到命名空间std中的
- 5. Contrôle discret et contrôle continu
- Gnuradio3.9.4 create OOT module instances
- Guojingxin center "friendship and righteousness" - the meta universe based on friendship and friendship, and the parallel of "honguniverse"
- 2022 high altitude installation, maintenance and demolition examination materials and high altitude installation, maintenance and demolition operation certificate examination
- Running OFDM in gnuradio_ RX error: gr:: Log: info: packet_ headerparser_ b0 - Detected an invalid packet at item ××
- A little experience from reading "civilization, modernization, value investment and China"
猜你喜欢

2021-03-06 - play with the application of reflection in the framework

Anaconda3 download address Tsinghua University open source software mirror station

Qt - - Packaging Programs - - Don't install Qt - can run directly

Js中forEach map无法跳出循环问题以及forEach会不会修改原数组
![[loss function] entropy / relative entropy / cross entropy](/img/bc/574a4745336b0baf1a4ca53af41a82.jpg)
[loss function] entropy / relative entropy / cross entropy

3. Multi agent reinforcement learning

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

Redis集群

nacos-微服务网关Gateway组件 +Swagger2接口生成

COMSOL - Construction of micro resistance beam model - final temperature distribution and deformation - establishment of geometric model
随机推荐
Euler Lagrange equation
regular expression
滑环使用如何固定
Mat file usage
Common fault analysis and Countermeasures of using MySQL in go language
跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
Deep learning website
Basic realization of line chart (II)
滑环在直驱电机转子的应用领域
NPDP在国内有认可度吗?看一看就明白了!
Connect to the previous chapter of the circuit to improve the material draft
common commands
Chapter improvement of clock -- multi-purpose signal modulation generation system based on ambient optical signal detection and custom signal rules
碳刷滑环在发电机中的作用
Running OFDM in gnuradio_ RX error: gr:: Log: info: packet_ headerparser_ b0 - Detected an invalid packet at item ××
How does Matplotlib and PIL image integrate and save multiple pictures into one picture
Macro definition and multiple parameters
common commands
ROS 问题(topic types do not match、topic datatype/md5sum not match、msg xxx have changed. rerun cmake)