gRPC之gRPC服务超时设置 |
您所在的位置:网站首页 › grpc调用接口后一直不返回 › gRPC之gRPC服务超时设置 |
1、gRPC服务超时设置
gRPC默认的请求的超时时间是很长的,当你没有设置请求超时时间时,所有在运行的请求都占用大量资源且可能 运行很长的时间,导致服务资源损耗过高,使得后来的请求响应过慢,甚至会引起整个进程崩溃。 为了避免这种情况,我们的服务应该设置超时时间。前面的入门教程提到,当客户端发起请求时候,需要传入上下 文context.Context,用于结束超时或取消的请求。 本篇介绍如何设置gRPC请求的超时时间。 1.1 proto文件和编译 syntax = "proto3"; package proto; option go_package = "./proto;proto"; // 定义我们的服务(可定义多个服务,每个服务可定义多个接口) service Simple{ rpc Route (SimpleRequest) returns (SimpleResponse){}; } message SimpleRequest{ // 定义发送的参数,采用驼峰命名方式,小写加下划线,如:student_name string data = 1;//发送数据 } message SimpleResponse{ // 定义接收的参数 // 参数类型 参数名 标识号(不可重复) int32 code = 1; //状态码 string value = 2;//接收值 } protoc --go_out=plugins=grpc:. simple.proto 1.2 客户端请求设置超时时间修改调用服务端方法: 1、把超时时间设置为当前时间+3秒 clientDeadline := time.Now().Add(time.Duration(3 * time.Second)) ctx, cancel := context.WithDeadline(ctx, clientDeadline) defer cancel()2、响应错误检测中添加超时检测 // 传入超时时间为3秒的ctx res, err := grpcClient.Route(ctx, &req) if err != nil { //获取错误状态 statu, ok := status.FromError(err) if ok { //判断是否为调用超时 if statu.Code() == codes.DeadlineExceeded { log.Fatalln("Route timeout!") } } log.Fatalf("Call Route err: %v", err) } // 打印返回值 log.Println(res.Value)3、完整的client.go代码 package main import ( "context" pb "demo/proto" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "log" "time" ) // Address 连接地址 const Address string = ":8000" var grpcClient pb.SimpleClient func main() { // 连接服务器 conn, err := grpc.Dial(Address, grpc.WithInsecure()) if err != nil { log.Fatalf("net.Connect err: %v", err) } defer conn.Close() ctx := context.Background() // 建立gRPC连接 grpcClient = pb.NewSimpleClient(conn) // 设置2秒超时时间 route(ctx, 2) } // route 调用服务端Route方法 func route(ctx context.Context, deadlines time.Duration) { //设置超时时间 clientDeadline := time.Now().Add(time.Duration(deadlines * time.Second)) ctx, cancel := context.WithDeadline(ctx, clientDeadline) defer cancel() // 创建发送结构体 req := pb.SimpleRequest{ Data: "grpc", } // 调用我们的服务(Route方法) // 传入超时时间的ctx res, err := grpcClient.Route(ctx, &req) if err != nil { //获取错误状态 statu, ok := status.FromError(err) if ok { //判断是否为调用超时 if statu.Code() == codes.DeadlineExceeded { log.Fatalln("Route timeout!") } } log.Fatalf("Call Route err: %v", err) } // 打印返回值 log.Println(res.Value) } 1.3 服务端判断请求是否超时当请求超时后,服务端应该停止正在进行的操作,避免资源浪费。 // Route 实现Route方法 func (s *SimpleService) Route(ctx context.Context, req *pb.SimpleRequest) (*pb.SimpleResponse, error) { data := make(chan *pb.SimpleResponse, 1) go handle(ctx, req, data) select { case res := case } const ( // Address 监听地址 Address string = ":8000" // Network 网络通信协议 Network string = "tcp" ) func main() { // 监听本地端口 listener, err := net.Listen(Network, Address) if err != nil { log.Fatalf("net.Listen err: %v", err) } log.Println(Address + " net.Listing...") // 新建gRPC服务器实例 grpcServer := grpc.NewServer() // 在gRPC服务器注册我们的服务 pb.RegisterSimpleServer(grpcServer, &SimpleService{}) //用服务器 Serve() 方法以及我们的端口信息区实现阻塞等待,直到进程被杀死或者 Stop() 被调用 err = grpcServer.Serve(listener) if err != nil { log.Fatalf("grpcServer.Serve err: %v", err) } } // Route 实现Route方法 func (s *SimpleService) Route(ctx context.Context, req *pb.SimpleRequest) (*pb.SimpleResponse, error) { data := make(chan *pb.SimpleResponse, 1) go handle(ctx, req, data) select { case res := case |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |