如何使用Spring Cloud Stream Kafka在微服务事件采购架构中查询事件存储库

时间:2017-02-09 16:45:17

标签: apache-kafka spring-cloud microservices spring-cloud-stream spring-kafka

澄清:请注意,此问题与此问题不同:How to implement a microservice Event Driven architecture with Spring Cloud Stream Kafka and Database per service

这个是关于使用Kafka as the only repository (of events),不需要数据库,另一个是关于使用Database (MariaDB) per service + Kafka

我想实现一个Event Sourcing架构来处理分布式事务:

OrdersService <------------> | Kafka Event Store | <------------>PaymentsService
                subscribe/                           subscribe/
                   find                                 find

OrdersService收到订单请求并将新订单存储在代理中。

private OrderBusiness orderBusiness;    

@PostMapping
public Order createOrder(@RequestBody Order order){
    logger.debug("createOrder()");
    //do whatever
    //Publish the new Order with state = pending
    order.setState(PENDING);
    try{       
       orderSource.output().send(MessageBuilder.withPayload(order).build());
    }catch(Exception e){
        logger.error("{}", e);
    }
    return order;
}

这是我的主要疑问:我如何查询Kafka经纪人?想象一下,我想按用户/日期,州等搜索订单

1 个答案:

答案 0 :(得分:0)

简答:您无法查询代理,但您可以利用Kafka的Streams API和&#34;互动查询&#34;。

长答案:阅读Kafka主题的访问模式是线性扫描,而不是随机查找。当然,您也可以随时通过override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { } 重新定位,但只能通过偏移时间重新定位。此外,主题分为分区,数据(默认情况下)按键分区(数据模型是键值对)。所以有一个关键的概念。

但是,您可以使用Kafka的Streams API,它允许您构建一个保持当前状态的应用程序 - 基于Kafka主题,这是一个基本事实 - 作为物化视图(基本上缓存)。 "Interactive Queries"允许您查询此物化视图。

有关详细信息,请参阅以下两篇博文: