go - GRPC 消息结构

标签 go protocol-buffers grpc grpc-go

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

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

是否有任何关于决定如何为我的消息建模的建议/最佳实践?它们应该与我的核心结构保持一致吗?我是否应该不关心所有必须从我的 Golang 结构到消息的编码?

如果我问的是这样一个基本问题,请原谅我,因为我对 Protocol Buffer 和 GRPC 还很陌生。

消息原型(prototype)定义:

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
}

最佳答案

虽然这是一个非常基本的问题,但也是一个非常好的问题。人们通常会跳过它,并且随着它的增长不得不在代码库中挣扎。

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

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

Always try to reduce coupling between layers so that they become reusable.

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

关于go - GRPC 消息结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56034049/

相关文章:

node.js - Tensorflow 服务 grpc 客户端错误 12

go - 使用 sync.Cond 时所有 go routines 都处于休眠状态

go - 如何在 golang 中调整数组以不随机化预定键?

java - Google Protocol Buffer 和 HTTP

protocol-buffers - 如何使用字节在 protobuf 中存储信息

go - 如何使用类型 func(*dialOptions)

rest - gRPC(HTTP/2) 是否比使用 HTTP/2 的 REST 更快?

c++ - golang select 语句是如何实现的?

go - Go 中标签的用途是什么?

c++ - 在 C++ 套接字编程中使用 google protobuf 序列化和反序列化消息