简体   繁体   English

Kafka微服务正确使用案例

[英]Kafka Microservice Proper Use Cases

In my new work's project, i discovered that instead of directly making post/put API calls from one microservice to another microservice, a microservice would produce a message to kafka, which is then consumed by a single microservice. 在我新工作的项目中,我发现微服务不会直接从一个微服务向另一个微服务发出post / put API调用,而是会向kafka生成一条消息,然后由单个微服务使用。

For example, Order microservice would publish a record to "pending-order" topic, which would then be consumed by Inventory microservice (no other consumer). 例如,订购微服务将发布一条记录到“待定订单”主题,然后由库存微服务(没有其他使用者)使用。 In turn, after consuming the record and done some processing, Inventory microservice would produce a record to "processed-order" which would then be consumed only by Order microservice. 反过来,在使用记录并进行了一些处理之后,库存微服务将产生一条记录为“已处理订单”,然后该记录仅由订单微服务使用。

Is this a correct use case? 这是正确的用例吗? Or is it better to just do API calls between microservices in this case? 还是在这种情况下仅在微服务之间进行API调用会更好?

There are two strong use cases of Kafka in a microservice based application: 在基于微服务的应用程序中,Kafka有两个强大的用例:

  1. You need to do a state change in multiple microservices as part of a single end user activity. 作为单个最终用户活动的一部分,您需要在多个微服务中进行状态更改。 If you do this by calling all the appropriate microservice APIs sequentially or parallely, there will be two issues: Firstly, you lose atomicity ie you canNot guarantee "all or nothing" . 如果通过依次或并行调用所有适当的微服务API来执行此操作,将有两个问题:首先,您失去原子性,即无法保证“全有或全无”。 It's very well possible that the call to microservice A succeeds but call to service B fails and that would lead to inconsistent data permanently. 对微服务A的调用很可能会成功,但对服务B的调用会失败,这将导致永久的数据不一致。 Secondly, in a cloud environment unpredictable latency and network timeouts are not uncommon and so when you make multiple calls as part of a single call, the probability of one of these calls getting delayed or failed is higher impacting user experience. 其次,在云环境中,不可预测的延迟和网络超时并不少见,因此当您在一个呼叫中进行多个呼叫时,这些呼叫之一被延迟或失败的可能性会极大地影响用户体验。 Hence, the general recommendation here is, you write the user action atomically in a Kafka topic as an event and have multiple consumer groups - one for each interested microservice consume the event and make the state change in their own database. 因此,这里的一般建议是,您将Kafka主题中的用户操作原子地写为一个事件,并具有多个使用者组-每个感兴趣的微服务都使用一个使用者组来消耗事件并在其自己的数据库中进行状态更改。 If the action is triggered by the user from a UI, you would also need to provide a "read your own write" guarantee where the user would like to see his data immediately after writing. 如果操作是由用户从UI触发的,则您还需要提供“自己读写”保证,即用户希望在写完后立即查看其数据。 Therefore, you'd need to write the event first in the local database of the first microservice and then do log based event sourcing (using an aporopriate Kafka Connector) to transfer the event data to Kafka. 因此,您需要先在第一个微服务的本地数据库中编写事件,然后再进行基于日志的事件源 (使用aporopriate Kafka Connector)将事件数据传输到Kafka。 This will enable you to show the data to the user from the local DB. 这将使您能够从本地数据库向用户显示数据。 You may also need to update a cache, a search index, a distributed file system etc. and all of these can be done by consuming the Kafka events published by the individual microservices. 您可能还需要更新缓存,搜索索引,分布式文件系统等,所有这些都可以通过使用各个微服务发布的Kafka事件来完成。

  2. It is not very uncommon that you need to pull data from multiple microservice to do some activity or to aggregate data and display to the user. 您需要从多个微服务中提取数据以执行某些活动或聚合数据并显示给用户,这并不少见。 This, in general, is not recommended because of the latency and timeout issue mentioned above. 通常,由于上述延迟和超时问题,因此不建议这样做。 It is usually recommended that we precompute those aggregates in the microservice local DB based on Kafka events published by the other microservices when they were changing their own state. 通常建议我们根据其他微服务更改状态时发布的Kafka事件,在微服务本地数据库中预先计算这些聚合。 This will allow you to serve the aggregate data to the user much faster. 这将使您更快地将聚合数据提供给用户。 This is called materialized view pattern. 这称为物化视图模式。

The only point to remember here is writing to Kafka log or broker and reading from it us asynchronous and there maybe a little time delay. 这里唯一要记住的一点是写Kafka日志或代理,并以异步方式从中读取信息,可能会有一点时间延迟。

Microservice as consumer, seems fishy to me. 微服务作为消费者,对我来说似乎是可疑的。 You might mean Listeners to that topic would consume the message and maybe they will call your second microservice ie Inventory Microservice. 您可能意味着该主题的侦听器将使用该消息,并且可能会调用您的第二个微服务,即库存微服务。

Yes, the model is fine, specially when you want to have asynchronous behavior and loads of traffic handled through it. 是的,该模型很好,特别是当您想要异步行为和通过它处理的流量负载时。

Imaging a scenario when you have more than 1 microservice to call from 1 endpoint. 当您有多个要从1个终结点调用的微服务时,请设想一个场景。 Here you need either aggregation layer which aggregates your services and you call it once, or you would like to publish several messages to Kafka which then will do the job. 在这里,您需要要么聚合服务的聚合层,然后调用它一次,要么您想要向Kafka发布几条消息,然后该任务就可以完成。

Also think about Read services, if you need to call a microservice to read some data from somewhere else, then you can't use Kafka. 还要考虑读取服务,如果您需要调用微服务从其他地方读取一些数据,那么您将无法使用Kafka。

It all depends on your requirements and design. 这完全取决于您的要求和设计。

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

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