zoukankan      html  css  js  c++  java
  • grpc(三)

    3 grpc的流

    用来接收大量的数据,支持3中模式:服务端流,客户端流,双端流

    来自书

    package ecommerce;
    
    service OrderManagement {
        rpc addOrder(Order) returns (google.protobuf.StringValue);
        rpc getOrder(google.protobuf.StringValue) returns (Order);
        rpc searchOrders(google.protobuf.StringValue) returns (stream Order);
        rpc updateOrders(stream Order) returns (google.protobuf.StringValue);
        rpc processOrders(stream google.protobuf.StringValue) returns (stream CombinedShipment);
    }
    
    message Order {
        string id = 1;
        repeated string items = 2;
        string description = 3;
        float price = 4;
        string destination = 5;
    }
    
    message CombinedShipment {
        string id = 1;
        string status = 2;
        repeated Order ordersList = 3;
    }
    

    来自鸟窝的grpc代码

    syntax = "proto3";
    
    package pb;
    
    import "github.com/gogo/protobuf/gogoproto/gogo.proto";
    
    option (gogoproto.gostring_all) = true;
    option (gogoproto.equal_all) = true;
    option (gogoproto.verbose_equal_all) = true;
    option (gogoproto.goproto_stringer_all) = false;
    option (gogoproto.stringer_all) =  true;
    option (gogoproto.populate_all) = true;
    option (gogoproto.testgen_all) = false;
    option (gogoproto.benchgen_all) = false;
    option (gogoproto.marshaler_all) = true;
    option (gogoproto.sizer_all) = true;
    option (gogoproto.unmarshaler_all) = true;
    
    
    // The greeting service definition.
    service Greeter {
      // Sends a greeting
      rpc SayHello1 (HelloRequest) returns (stream HelloReply) {}
      rpc SayHello2 (stream HelloRequest) returns (HelloReply) {}
      rpc SayHello3 (stream HelloRequest) returns (stream HelloReply) {}
    }
    
    // The request message containing the user's name.
    message HelloRequest {
      string name = 1;
    }
    
    // The response message containing the greetings
    message HelloReply {
      string message = 1;
    }
    

    流数据需要持续读取,直到遇到ioe错误

    for {
    		orderId, err := stream.Recv()
    		log.Printf("Reading Proc order : %s", orderId)
    		if err == io.EOF {
    			// Client has sent all the messages
    			// Send remaining shipments
    			log.Printf("EOF : %s", orderId)
    			for _, shipment := range combinedShipmentMap {
    				if err := stream.Send(&shipment); err != nil {
    					return err
    				}
    			}
    			return nil
    		}
    
  • 相关阅读:
    visualSVNYou don't have permission to access on this server
    怎样截取Http请求
    ASP.NET @page 指令详解
    我的XMLHelperC# XML操作基类(修改,删除,新增,创建)
    最新Visual Studio 2010 下载及学习资料
    redis主从哨兵和集群的区别
    uniapp nvue 支持iconfont
    Error (Error Code: 1175) during executing update command on table using MySQL
    org.apache.jasper.JasperException: Unable to compile class for JSP
    java.sql.SQLException: Access denied for user 'root'@'10.10.10.10' (using password: YES)
  • 原文地址:https://www.cnblogs.com/beckbi/p/14833856.html
Copyright © 2011-2022 走看看