【问题标题】:GRPC Message StructuresGRPC 消息结构
【发布时间】:2019-09-25 19:00:58
【问题描述】:

我正在将一个遗留应用程序(微服务和单体应用程序)迁移到使用 GRPC 的过程中。整个代码库目前都在 GO 中。

我已经开始为我的消息建模,它们看起来与我在应用程序代码中使用的结构非常相似。我定义了两次相同的对象似乎很奇怪,但使用消息对象作为核心结构似乎也很奇怪。不过,似乎我会有大量的内存密集型数据封送处理。下面是消息和结构的示例以及它们的相似程度。

在决定如何为我的消息建模方面是否有任何建议/最佳做法?它们应该与我的核心结构对齐吗?我是否应该不关心从我的 Golang 结构到消息中必须发生的所有编组?

如果我问这样一个基本问题,请原谅我,因为我对协议缓冲区和 GRPC 很陌生。

消息原型定义:

message Profile {
    string UID = 1;
    string ContactEmail = 2;
    google.protobuf.Timestamp DateOfBirth = 3;
    float WeightInKilos = 4;
    string Gender = 5;
    string Unit = 6;
    string CurrentStatus = 7;
    string Country = 8;
    string ExperienceType = 9;
    google.protobuf.Timestamp DateJoined = 10;
    repeated Ability Abilities = 11;
    repeated Role Roles = 12;
    repeated TermsAndConditionsAcceptance TermsAndConditionsAcceptances = 13;
    string TimeZone = 14;
    repeated BaselineTestResults BaselineTests =15;
    //Excluded UpdatedDate as other domains shouldn't need it
    string FirstName =16;
    string LastName =17;
    string DisplayName = 18;
    string State = 19;
    repeated google.protobuf.Any Preferences = 20;
    Thresholds Thresholds = 21;
    string StripeCustomerID = 22;
}

结构定义:

type Profile struct {
    UID                           string                         `json:"UID" firestore:"UID"`
    ContactEmail                  string                         `json:"ContactEmail,omitempty" firestore:"ContactEmail"`
    DateOfBirth                   time.Time                      `json:"DateOfBirth,omitempty" firestore:"DateOfBirth"`
    WeightInKilos                 float64                        `json:"WeightInKilos,omitempty" firestore:"WeightInKilos"`
    Gender                        string                         `json:"Gender,omitempty" firestore:"Gender"`
    Unit                          string                         `json:"Unit,omitempty" firestore:"Unit"`
    CurrentStatus                 string                         `json:"CurrentStatus,omitempty" firestore:"CurrentStatus"`
    Country                       string                         `json:"Country,omitempty" firestore:"Country"`
    ExperienceType                string                         `json:"ExperienceType,omitempty" firestore:"ExperienceType"`
    DateJoined                    time.Time                      `json:"DateJoined,omitempty" firestore:"DateJoined"`
    Abilities                     []Ability                      `json:"Abilities,omitempty" firestore:"Abilities"`
    Goals                         Goals                          `json:"Goals,omitempty" firestore:"Goals"`
    Roles                         []Role                         `json:"Roles,omitempty" firestore:"Roles"`
    TermsAndConditionsAcceptances []TermsAndConditionsAcceptance `json:"TermsAndConditionsAcceptances,omitempty" firestore:"TermsAndConditionsAcceptances"`
    TimeZone                      string                         `json:"TimeZone,omitempty" firestore:"TimeZone"`
    BaselineTests                 []BaselineTestResults          `json:"BaselineTests,omitempty" firestore:"BaselineTests"`
    UpdatedDate                   time.Time                      `json:"UpdatedDate,omitempty" firestore:"UpdatedDate"`
    FirstName                     *string                        `json:"FirstName,omitempty" firestore:"FirstName"`
    LastName                      string                         `json:"LastName,omitempty" firestore:"LastName"`
    DisplayName                   string                         `json:"DisplayName,omitempty" firestore:"DisplayName"`
    State                         string                         `json:"State"`
    Preferences                   map[string]interface{}         `json:"Preferences"`
    Thresholds                    Thresholds                     `json:"Thresholds"`
    StripeCustomerID              string                         `json:"-"` //Tells it to ignore exporting
}

【问题讨论】:

    标签: go protocol-buffers grpc grpc-go


    【解决方案1】:

    您无需在应用代码中创建结构。只需使用原始文件的消息。 这是 Go 中 GRPC 的快速入门。

    https://grpc.io/docs/quickstart/go/

    【讨论】:

    • 请查看how-to-answer Stackoverflow 上的一个问题:“鼓励链接到外部资源,但请添加上下文...始终引用重要链接中最相关的部分”
    【解决方案2】:

    虽然是一个非常基本的问题,但也是一个非常好的问题。人们通常会跳过它,并且随着代码库的增长而不得不在以后与代码库作斗争。

    当然,在处理请求所需的数据在 message 中完全可用的情况下,使用 message 作为核心结构会很有帮助。但在您需要从其他来源获取数据的情况下将无济于事。

    通常,消息数据由边界层/控制器处理,该控制器进一步使用多个服务来创建响应。因此,服务层/逻辑层独立于控制器层,消息结构和核心结构也应如此。

    始终尝试减少层之间的耦合,以便它们变得可重用。

    所以如果你有一个处理数据库的层,你也应该有单独的结构。

    【讨论】:

    • 谢谢!有用的反馈。听起来 GRPC 真的不在乎,但出于安全和可扩展性的目的,我应该将两者分离。我可以看到某些情况下它们是相同的,但在大多数情况下,有些数据需要被抑制。我想数据编组是为更好的解耦付出的小代价。感谢您的回复。
    • 没错。而且您很好地将“编组是一个很小的代价”放在项目的生命周期中。
    猜你喜欢
    • 2021-05-10
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 2011-06-14
    • 2010-12-24
    • 2023-03-12
    相关资源
    最近更新 更多