当前位置:网站首页>云原生应用开发之 gRPC 入门
云原生应用开发之 gRPC 入门
2022-07-07 23:48:00 【InfoQ】
什么是 gRPC

特点
- gRPC 是一个高性能、开源和通用的 RPC 框架,面向移动和 HTTP/2 设计,带来诸如双向流、流控、头部压缩、单 TCP 连接上的多复用请求等特。这些特性使得其在移动设备上表现更好,更省电和节省空间占用。
- 在 gRPC 里客户端应用可以像调用本地对象一样直接调用另一台不同的机器上服务端应用的方法,使得您能够更容易地创建分布式应用和服务。
- gRPC 默认使用 protocol buffers,这是 Google 开源的一套成熟的结构数据序列化机制,它的作用与 XML、json 类似,但它是二进制格式,性能好、效率高(缺点:可读性差)。
gRPC 和 REST 区别
- gRPC 使用 HTTP/2 协议,而 REST 使用 HTTP 1.1
- gRPC 使用协议缓冲区数据格式,而不是通常在 REST API 中使用的标准 JSON 数据格式
- 使用 gRPC,您可以根据需要利用 HTTP/2 功能,例如服务器端流式传输、客户端流式传输甚至双向流式传输。
Go 建立一个 gRPC 服务器
- 安装 golang 的proto工具包:
go get -u github.com/golang/protobuf/proto- 在开始建立 gRPC 之前,确保已安装 Protocol Buffers v3:
go get -u github.com/golang/protobuf/protoc-gen-go- 在 Go 中安装 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)
}
}
添加一些功能
client.protosyntax = "proto3"; // 协议为proto3
package chat;
// 定义发送请求信息
message Message {
// 定义发送的参数
// 参数类型 参数名 标识号(不可重复)
string body = 1;
}
// 定义我们的服务(可定义多个服务,每个服务可定义多个接口)
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:8000在 Go 中构建 gRPC 客户端
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!安装问题
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
总结
- Go gRPC Beginners Tutorial
- 《gRPC 与云原生应用开发》
边栏推荐
- 2、TD+Learning
- 2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills
- The combination of relay and led small night light realizes the control of small night light cycle on and off
- 2021-03-06 - play with the application of reflection in the framework
- regular expression
- Basic realization of line chart (II)
- 2022 high voltage electrician examination skills and high voltage electrician reexamination examination
- 如果时间是条河
- How does Matplotlib generate multiple pictures in turn & only save these pictures without displaying them in the compiler
- 项目经理有必要考NPDP吗?我告诉你答案
猜你喜欢

4、策略學習

4. Apprentissage stratégique

The combination of relay and led small night light realizes the control of small night light cycle on and off

Basic implementation of pie chart

redis的持久化方式-RDB和AOF 两种持久化机制

4. Strategic Learning

Getting started STM32 -- how to learn stm32

3. Multi agent reinforcement learning

QT build with built-in application framework -- Hello World -- use min GW 32bit

About snake equation (2)
随机推荐
2022 examination for safety production management personnel of hazardous chemical production units and new version of examination questions for safety production management personnel of hazardous chem
Guojingxin center "friendship and righteousness" - the meta universe based on friendship and friendship, and the parallel of "honguniverse"
Application of state mode in JSF source code
Redis master-slave replication
About how USRP sets the sampling frequency below the minimum sampling frequency reached by the hardware
Probability distribution
How does Matplotlib generate multiple pictures in turn & only save these pictures without displaying them in the compiler
break net
The examination contents of the third batch of Guangdong Provincial Safety Officer a certificate (main person in charge) in 2021 and the free examination questions of the third batch of Guangdong Prov
Smart agricultural technology framework
Scalar / vector / matrix derivation method
common commands
qt--将程序打包--不要安装qt-可以直接运行
Mat file usage
液压旋转接头的使用事项
小金额炒股,在手机上开户安全吗?
3、多智能体强化学习
不算不知道,花呗分期的真实利率居然这么高
Redis cluster
2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills