简体   繁体   English

基于事件的微服务架构:查询补充事件数据的最佳实践

[英]Event-based microservice architecture: best practice for querying complementary event data

In an event-based microservice architecture what is best practice for querying for additional data needed for a microservice handling an event?在基于事件的微服务架构中,查询微服务处理事件所需的额外数据的最佳实践是什么?

Imagine this microservice constellation:想象一下这个微服务星座:

OrderService - receives orders via REST, writes the order to the order database and issues an OrderCreatedEvent, which includes the order data and a customerId. OrderService - 通过 REST 接收订单,将订单写入订单数据库并发出 OrderCreatedEvent,其中包括订单数据和 customerId。

CustomerService - REST Api for managing customers by using its own customer database, creates an receives different events which are not relevant for the use case CustomerService - REST Api 用于使用自己的客户数据库管理客户,创建一个接收与用例无关的不同事件

VoucherService - listens to OrderCreatedEvents to sent vouchers to customers. VoucherService - 侦听 OrderCreatedEvents 以将凭证发送给客户。

Here's the qestion: the VoucherService needs more information on the customer (eg the address) as is provided in the OrderCreatedEvent - what's the best way to provide the VoucherService with the customer data?这是问题:VoucherService 需要更多关于客户的信息(例如地址),如 OrderCreatedEvent 中提供的那样 - 向 VoucherService 提供客户数据的最佳方式是什么?

Possible Solutions:可能的解决方案:

  • The VoucherService could call the API of the CustomerService, but this violates the lose coupling of services. VoucherService 可以调用 CustomerService 的 API,但这违反了服务的丢失耦合。

  • The VoucherService could query the customer database, but this would lead to even tighter coupling as changes to the customer database now must be compatible to the VoucherService. VoucherService 可以查询客户数据库,但这会导致更紧密的耦合,因为现在对客户数据库的更改必须与 VoucherService 兼容。

  • The OrderService could eventually be able to fill the necessary customer data to the event, but this only works if the data is available for some reason and additionally this will lead to problems because in an enterprise environment there could be 50+ fields for a customer and the OrderService doesn't know (and shouldn't know) which of them are necessary for its clients. OrderService 最终可能能够将必要的客户数据填充到事件中,但这仅在数据由于某种原因可用的情况下才有效,另外这将导致问题,因为在企业环境中,客户可能有 50 多个字段,并且OrderService 不知道(也不应该知道)其中哪些是其客户所必需的。

Any suggestions how to solve this?任何建议如何解决这个问题?

The solution that you stated:你说的解决方案:
The OrderService could eventually be able to fill the necessary customer data to the event, but this only works if the data is available for some reason and additionally this will lead to problems because in an enterprise environment there could be 50+ fields for a customer and the OrderService doesn't know (and shouldn't know) which of them are necessary for its clients. OrderService 最终可能能够将必要的客户数据填充到事件中,但这仅在数据由于某种原因可用的情况下才有效,另外这将导致问题,因为在企业环境中,客户可能有 50 多个字段,并且OrderService 不知道(也不应该知道)其中哪些是其客户所必需的。

I would suggest to keep the OrderService dumb on customer data, but since you need the customer data in the VoucherService, you VoucherService should be interested on the events coming from CustomerService and store the necessary data that are crucial for the service functionality我建议让 OrderService 对客户数据保持沉默,但由于您需要 VoucherService 中的客户数据,您 VoucherService 应该对来自 CustomerService 的事件感兴趣并存储对服务功能至关重要的必要数据

There are no silver bullets on solving everything perfectly, but keeping stuff async it will be always the best solution, this requires data duplication and more work, but in the end every service is decoupled and will work without problems on their own完美解决所有问题没有灵丹妙药,但保持异步始终是最好的解决方案,这需要数据复制和更多工作,但最终每个服务都是解耦的,并且可以独立运行而不会出现问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM