我正在使用 Java和Protoc 3.0编译器,我的proto文件在下面提到.
https://github.com/openconfig/public/blob/master/release/models/rpc/openconfig-rpc-api.yang
Syntax = "proto3";

package Telemetry;

// Interface exported by Agent
service OpenConfigTelemetry {
    // Request an inline subscription for data at the specified path.
    // The device should send telemetry data back on the same
    // connection as the subscription request.
    rpc telemetrySubscribe(SubscriptionRequest)                     returns (stream OpenConfigData) {}

    // Terminates and removes an exisiting telemetry subscription
    rpc cancelTelemetrySubscription(CancelSubscriptionRequest)      returns (CancelSubscriptionReply) {}

    // Get the list of current telemetry subscriptions from the
    // target. This command returns a list of existing subscriptions
    // not including those that are established via configuration.
    rpc getTelemetrySubscriptions(GetSubscriptionsRequest)          returns (GetSubscriptionsReply) {}

    // Get Telemetry Agent Operational States
    rpc getTelemetryOperationalState(GetoperationalStateRequest)    returns (GetoperationalStateReply) {}

    // Return the set of data encodings supported by the device for
    // telemetry data
    rpc getDataEncodings(DataEncodingRequest)                       returns (DataEncodingReply) {}
}

// Message sent for a telemetry subscription request
message SubscriptionRequest {
    // Data associated with a telemetry subscription
    SubscriptionInput input                                 = 1;

    // List of data models paths and filters
    // which are used in a telemetry operation.
    repeated Path path_list                                 = 2;

    // The below configuration is not defined in Openconfig RPC.
    // It is a proposed extension to configure additional
    // subscription request features.
    SubscriptionAdditionalConfig additional_config          = 3;
}

// Data associated with a telemetry subscription
message SubscriptionInput {
    // List of optional collector endpoints to send data for
    // this subscription.
    // If no collector destinations are specified,the collector
    // destination is assumed to be the requester on the rpc channel.
    repeated Collector  collector_list                      = 1;
}

// Collector endpoints to send data specified as an ip+port combination.
message Collector {
    // IP address of collector endpoint
    string address                                          = 1;

    // Transport protocol port number for the collector destination.
    uint32 port                                             = 2;
}

// Data model path
message Path {
    // Data model path of interest
    // Path specification for elements of OpenConfig data models
    string path                                             = 1;

    // Regular expression to be used in filtering state leaves
    string filter                                           = 2;

    // If this is set to true,the target device will only send
    // updates to the collector upon a change in data value
    bool suppress_unchanged                                 = 3;

    // Maximum time in ms the target device may go without sending
    // a message to the collector. If this time expires with
    // suppress-unchanged set,the target device must send an update
    // message regardless if the data values have changed.
    uint32 max_silent_interval                              = 4;

    // Time in ms between collection and transmission of the
    // specified data to the collector platform. The target device
    // will sample the corresponding data (e.g,. a counter) and
    // immediately send to the collector destination.
    //
    // If sample-frequency is set to 0,then the network device
    // must emit an update upon every datum change.
    uint32 sample_frequency                                 = 5;
}

// Configure subscription request additional features.
message SubscriptionAdditionalConfig {
    // limit the number of records sent in the stream
    int32 limit_records                                     = 1;

    // limit the time the stream remains open
    int32 limit_time_seconds                                = 2;
}

// Reply to inline subscription for data at the specified path is done in
// two-folds.
// 1. Reply data message sent out using out-of-band channel.
// 2. Telemetry data send back on the same connection as the
//    subscription request.

// 1. Reply data message sent out using out-of-band channel.
message SubscriptionReply {
    // Response message to a telemetry subscription creation or
    // get request.
    SubscriptionResponse response                           = 1;

    // List of data models paths and filters
    // which are used in a telemetry operation.
    repeated Path path_list                                 = 2;
}

// Response message to a telemetry subscription creation or get request.
message SubscriptionResponse {
    // Unique id for the subscription on the device. This is
    // generated by the device and returned in a subscription
    // request or when listing existing subscriptions
    uint32 subscription_id = 1;
}

// 2. Telemetry data send back on the same connection as the
//    subscription request.
message OpenConfigData {
    // router name:export IP address
    string system_id                                        = 1;

    // line card / RE (slot number)
    uint32 component_id                                     = 2;

    // PFE (if applicable)
    uint32 sub_component_id                                 = 3;

    // Path specification for elements of OpenConfig data models
    string path                                             = 4;

    // Sequence number,monotonically increasing for each
    // system_id,component_id,sub_component_id + path.
    uint64 sequence_number                                  = 5;

    // timestamp (milliseconds since epoch)
    uint64 timestamp                                        = 6;

    // List of key-value pairs
    repeated keyvalue kv                                    = 7;
}

// Simple Key-value,where value Could be one of scalar types
message keyvalue {
    // Key
    string key                                              =  1;

    // One of possible values
    oneof value {
        double double_value                                 =  5;
        int64  int_value                                    =  6;
        uint64 uint_value                                   =  7;
        sint64 sint_value                                   =  8;
        bool   bool_value                                   =  9;
        string str_value                                    = 10;
        bytes  bytes_value                                  = 11;
    }
}

// Message sent for a telemetry subscription cancellation request
message CancelSubscriptionRequest {
    // Subscription identifier as returned by the device when
    // subscription was requested
    uint32 subscription_id                                  = 1;
}

// Reply to telemetry subscription cancellation request
message CancelSubscriptionReply {
    // Return code
    ReturnCode code                                         = 1;

    // Return code string
    string     code_str                                     = 2;
};

// Result of the operation
enum ReturnCode {
    SUCCESS                                                 = 0;
    NO_SUBSCRIPTION_ENTRY                                   = 1;
    UNKNowN_ERROR                                           = 2;
}

// Message sent for a telemetry get request
message GetSubscriptionsRequest {
    // Subscription identifier as returned by the device when
    // subscription was requested
    // --- or ---
    // 0xFFFFFFFF for all subscription identifiers
    uint32 subscription_id                                  = 1;
}

// Reply to telemetry subscription get request
message GetSubscriptionsReply {
    // List of current telemetry subscriptions
    repeated SubscriptionReply subscription_list            = 1;
}

// Message sent for telemetry agent operational states request
message GetoperationalStateRequest {
    // Per-subscription_id level operational state can be requested.
    //
    // Subscription identifier as returned by the device when
    // subscription was requested
    // --- or ---
    // 0xFFFFFFFF for all subscription identifiers including agent-level
    // operational stats
    // --- or ---
    // If subscription_id is not present then sent only agent-level
    // operational stats
    uint32 subscription_id                                  = 1;

    // Control verbosity of the output
    VerbosityLevel verbosity                                = 2;
}

// Verbosity Level
enum VerbosityLevel {
    DETAIL                                                  = 0;
    TERSE                                                   = 1;
    BRIEF                                                   = 2;
}

// Reply to telemetry agent operational states request
message GetoperationalStateReply {
    // List of key-value pairs where
    //     key      = operational state deFinition
    //     value    = operational state value
    repeated keyvalue kv                                    = 1;
}

// Message sent for a data encoding request
message DataEncodingRequest {
}

// Reply to data encodings supported request
message DataEncodingReply {
    repeated EncodingType  encoding_list                    = 1;
}

// Encoding Type Supported
enum EncodingType {
    UNDEFINED                                               = 0;
    XML                                                     = 1;
    JSON_IETF                                               = 2;
    PROTO3                                                  = 3;
}

为了进行服务调用(rpc TelemetrySubscribe),我首先需要读取具有订阅ID的头,然后开始阅读消息.现在,使用Java我能够连接服务,我确实介绍了拦截器但是当我打印/检索头时它是null.我的调用拦截器代码如下,

ClientInterceptor interceptor = new HeaderClientInterceptor();
      originChannel = OkHttpChannelBuilder.forAddress(host,port)
        .usePlaintext(true)
        .build();
     Channel channel =  ClientInterceptors.intercept(originChannel,interceptor);
      telemetryStub = OpenConfigTelemetryGrpc.newStub(channel);

这是读取元数据的拦截器代码.

@Override
  public <ReqT,RespT> ClientCall<ReqT,RespT> interceptCall(MethodDescriptor<ReqT,RespT> method,CallOptions callOptions,Channel next) {
    return new SimpleForwardingClientCall<ReqT,RespT>(next.newCall(method,callOptions)) {

      @Override
      public void start(Listener<RespT> responseListener,Metadata headers) {

        super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
          @Override
          public void onHeaders(Metadata headers) {

             Key<String> CUSTOM_HEADER_KEY = Metadata.Key.of("responseKEY",Metadata.ASCII_STRING_MARSHALLER);

            System.out.println("Contains Key?? "+headers.containsKey(CUSTOM_HEADER_KEY));

想知道有没有其他方法来读取元数据或第一个有订阅ID的消息?所有我需要阅读第一条有订阅ID的消息,并将相同的订阅ID返回给服务器以便流可以启动我使用相同的原型文件使用相同的Python代码,它通过下面的代码提供与服务器通信仅供参考:

sub_req = SubscribeRequestMsg("host",port)
     data_itr = stub.telemetrySubscribe(sub_req,_TIMEOUT_SECONDS)
     Metadata = data_itr.initial_Metadata()

                   if Metadata[0][0] == "responseKey":
                    Metainfo = Metadata[0][1]
                    print Metainfo

                    subreply = agent_pb2.SubscriptionReply()
                    subreply.SetInParent()
                    google.protobuf.text_format.Merge(Metainfo,subreply)

                    if subreply.response.subscription_id:
                    SUB_ID = subreply.response.subscription_id

从上面的python代码我可以轻松检索元数据对象,不知道如何使用Java检索它?

在阅读MetaData之后,我得到的是:元数据({content-type = [application / grpc],grpc-encoding = [identity],grpc-accept-encoding = [identity,deflate,gzip]})

但我知道从元数据到它还有一条线,也就是说

response {
  subscription_id: 2
}

如何从Header中提取包含订阅ID的最后一个响应.我确实尝试了很多选项,我迷失在这里.

解决方法

您使用的方法是请求元数据,而不是响应元数据:
public void start(Listener<RespT> responseListener,Metadata headers) {

对于响应元数据,您将需要ClientCall.Listener并等待onHeaders回调:

public void onHeaders(Metadata headers)

我觉得你提到的元数据的使用似乎很奇怪.元数据通常用于其他错误详细信息或不特定于RPC方法的交叉功能(如身份验证,跟踪等).

如何在客户端使用Java读取gRPC中的元数据的更多相关文章

  1. ios – Swift:自定义相机使用图像保存修改后的元数据

    我试图保存图像样本缓冲区中的一些元数据以及图像.我需要:>将图像旋转到元数据的方向>从元数据中删除方向>将日期保存到元数据中>将包含元数据的图像保存到文档目录我试过从数据创建一个UIImage,但是删除了元数据.我已经尝试使用数据中的CIImage来保存元数据,但是我无法将其旋转然后将其保存到文件中.这是我保存图像的代码.解决方法我最终弄清楚如何让一切按照我需要的方式工作.对我帮助最大的事情是发现

  2. ios – 如何为多个目标提供传送(fastlane)下载元数据?

    附:这是一个很大的遗留项目,所以将它分成六个不同的项目会很棒,但不幸的是,这不是一个选择.解决方法我一直在努力解决这个问题,使用.env文件设置提交很容易.但是检索初始数据很困难,但并非不可能.要获取元数据,它运行此命令:并为截图:

  3. ios – 从MPMoviePlayerController获取MetaData

    代码!

  4. ios – 从音频流中获取元数据

    我想获取文件名,如果可能的话,可以从我在AVQueuePlayer上播放的AVPlayerItem中的流式URL中的相册图像,但是我不知道该怎么做.另外如果事实证明,我的流网址没有任何元数据,我可以将元数据放入我的NSURL*,然后传递给AVPlayerItem?

  5. 在iOS上保存修改后的元数据(无需重新编码)的原始图像数据

    我想在temp文件夹中保存一些元数据更改的图像,而无需重新编码实际的图像数据.我发现能够做到这一点的唯一方法是ALAssetsLibrary/writeImageDataToSavedPhotosAlbum:metadata:completionBlock:,但是,这个方法将图像保存到照片库中.相反,我想将图像保存到临时文件夹(例如,通过电子邮件分享,而不填充照片库).我试过使用CGImageDe

  6. iOS应用程序审核队列,如果您更改元数据,是否重置?

    我知道更新并重新提交您的应用程序二进制文件让您回到iOS应用程序市场审核队列,并将时钟重新设置为6–8天的等待时间...但有人知道如果更改元数据也会使您处于队列的后面?我的第一个二进制文件在5天内被批准和发布(是),但是我提交的更新已经在那里等待10天了...也许是因为我已经添加了一些描述翻译,并更改了截图?

  7. OpenStack 对象存储 Swift 简单介绍

    Swift最适合的就是永久类型的静态数据的长期存储。提供账号验证的节点被称为AccountServer。Swift中由Swauth提供账号权限认证服务。ProxyserveracceptsincomingrequestsviatheOpenStackObjectAPIorjustrawHTTP.Itacceptsfilestoupload,modificationstoMetadataorcontainercreation.Inaddition,itwillalsoservefilesorcontaine

  8. swift对象存储

    Swift最初是由Rackspace公司开发的高可用分布式对象存储服务,并于2010年贡献给OpenStack开源社区作为其最初的核心子项目之一,为其Nova子项目提供虚机镜像存储服务。Swift为账户,容器和对象分别定义了的环,查找账户和容器的是同样的过程。

  9. 让你真正明白cinder与swift、glance的区别

    Cinder——提供块存储,类似于Amazon的EBS块存储服务,目前仅给虚机挂载使用。Cinder包含以下三个主要组成部分APIservice:Cinder-api是主要服务接口,负责接受和处理外界的API请求,并将请求放入RabbitMQ队列,交由后端执行。Cinder目前提供VolumeAPIV2Schedulerservice:处理任务队列的任务,并根据预定策略选择合适的VolumeService节点来执行任务。目前版本的cinder仅仅提供了一个SimpleScheduler,该调度器选择卷数量

  10. Openstack Swift 原理、架构与 API 介绍

    数据一致性模型按照EricBrewer的CAP理论,无法同时满足3个方面,Swift放弃严格一致性,而采用最终一致性模型,来达到高可用性和无限水平扩展能力。Swift为账户,容器和对象分别定义了的环,查找账户和容器的是同样的过程。

随机推荐

  1. 如何在客户端使用Java读取gRPC中的元数据

    所有我需要阅读第一条有订阅ID的消息,并将相同的订阅ID返回给服务器以便流可以启动我使用相同的原型文件使用相同的Python代码,它通过下面的代码提供与服务器通信仅供参考:从上面的python代码我可以轻松检索元数据对象,不知道如何使用Java检索它?

  2. Windows上的gRPC C.

    我已经尝试了3天,现在找到如何在没有运气的情况下在Windows上安装和使用gRPC.我使用的是VisualStudio2015,Win764位.为了安全起见,我会一步一步地写下我正在做的事情.它可能没有必要,但我是C和VS的初学者,所以我不确定我是否正确地做到了:(按照指南http://www.infopulse.com/blog/grpc-framework-by-google-tutoria

  3. grpc2:Centos 安装 nghttp2 做 grpc 的http2 代理

    1,nghttp2和Nginx名字比较像,但是是一个c的llib库。而haproxy是支持tcp做代理的。2,下载安装官方网站:https://nghttp2.org/https://github.com/nghttp2/nghttp2官方文档是在ubuntu或者debian上面进行安装的。安装依赖库:安装nghttp2服务。3,启动服务网上的文档比较少https://nghttp2.org/documentation/package_README.html配置就直接按照proxy进行配置即可。https

  4. grpc1:Centos 安装java的grpc服务,使用haproxy进行负载均衡,nginx不支持

    目前提供C、Java和Go语言版本,分别是:grpc,grpc-java,grpc-go.其中C版本支持C,C++,Node.js,Python,Ruby,Objective-C,PHP和C#支持。自动进行proto编译,转换成几个java文件。4,启动server启动server。使用client进行测试:5,不能使用Nginx进行grpc代理虽然Nginx已经支持了http2,但是不能适应Nginx进行负载均衡。grpc想使用Nginx做代理。https://github.com/grpc/grpc-

返回
顶部