【问题标题】:How to define a protobuf message to pass an active record protobuf message directly as an input parameter into a ruby grpc code by compiling protomsg?如何定义 protobuf 消息以通过编译 protomsg 将活动记录 protobuf 消息直接作为输入参数传递到 ruby​​ grpc 代码中?
【发布时间】:2017-08-14 04:08:24
【问题描述】:

我们正在使用 proto3 创建一个 grpc 服务器。并将其编译为 ruby​​ 函数。我们使用 ActiveRecord-Protobuf gem 将 Active Record 消息转换为 protobuf 消息(通过调用“activerecord”.to_proto 方法获得)。然而,在创建 protobuf 消息以创建 ruby​​ 服务器时,我们无法传递“activerecord”.to_proto 消息,因为在定义输入值的类型时我们别无选择,只能将其定义为 proto3 中的消息,因此它只接受一个哈希值。为此,我们必须将 .to_proto 对象转换为“activerecord”.to_proto.to_hash。这是徒劳的,并且降低了 grpc 的最优性。具有讽刺意味的是,我们正在转向 grpc 以获得最优性。您能否建议如何定义 protobuf 消息(使用 proto3)以确保“activerecord”.to_proto 消息与 proto3 输入值定义兼容。

这是活动记录对象。

class AppPreferenceMessage < ::Protobuf::Message
optional ::Protobuf::Field::Int64Field, :id, 1
optional ::Protobuf::Field::StringField, :preference_key, 2
optional ::Protobuf::Field::StringField, :value, 3
optional ::Protobuf::Field::StringField, :value_type, 4
optional ::Protobuf::Field::Int64Field, :vaccount_id, 5  
end

这被转换为 AppPreference.last.to_proto,这是一个按类的 protobuf 消息。

我的ruby输入参数的protobuf定义如下。

syntax="proto3";
service App_Preferences{
rpc Index(Empty) returns (Index_Output){}
}
message Index_Output{
int64 id=1;
string preference_key=2;
string value=3;
string value_type=4;
int64 vaccount_id = 5;  
}

这个参数“Index_Output”只接受 AppPreference.last.to_proto.to_hash 但我希望它接受 AppPreference.last.to_proto 作为输入。我如何更改我的 protobuf 代码。

【问题讨论】:

    标签: ruby-on-rails ruby protocol-buffers grpc proto3


    【解决方案1】:

    我想您想将此 AppPreferenceMessage 对象转换为 protobuf 消息,然后将其传递给从单独的 proto 文件创建的 rpc 方法?另外我不熟悉 AppPreferenceMessage.to_proto 返回的内容,是序列化字节吗?

    我不确定我是否完全清楚,但听起来您可能不想使用 grpc-protobuf 存根和服务代码生成器,因为它们在设计时考虑到了 ruby​​ protobuf 对象的传递。

    如果您需要跳过 grpc-protobuf 代码生成,https://github.com/grpc/grpc/tree/master/examples/ruby/without_protobuf 中有一些示例可能会很有用。

    【讨论】:

      猜你喜欢
      • 2019-07-07
      • 2012-06-29
      • 1970-01-01
      • 2022-06-30
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      相关资源
      最近更新 更多