微服务创建实体实现 [英] Microservices Create Entity Implementation

查看:93
本文介绍了微服务创建实体实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题的后续问题,此处.

This is a follow-up question to my issue outlined here.

网关用作应用程序的入口点,客户端发出的每个请求都指向该入口点.然后,网关将请求分配给负责的微服务,并处理身份验证.

The Gateway serves as an entry point to the application, to which every request from the client is made. The gateway then allocates the request to the responsible microservices and also handles authentication.

在这种情况下,网关侦听HTTP POST /bok并通知Microservice A创建一本书.因此,Microservice A ist负责管理和存储有关图书实体的所有内容.

In this case the gateway listens for HTTP POST /bok and notifies the Microservice A to create a book. Thus Microservice A ist responsible for managing and storing everything about the book entity.

以下伪代码是此体系结构的简化实现:

The following pseudo-code is a simplified implementation of this architecture:

队列通信

网关

router.post('/book', (req, res) => {
  queue.publish('CreateBook', req.body);
  queue.consume('BookCreated', (book) => {
    const user = getUserFromOtherMicroService(book.userId);
    res.json({ book, user });
  });
});

微服务A

queue.consume('CreateBook', (payload) => {
  const book = createBook(payload);
  eventStore.insert('BookCreated', book);
  const createdBook = updateProjectionDatabase(book);
  queue.publish('BookCreated', createdBook);
})

但是由于以下原因,我对此不太确定:

But I am not quite sure about this because of the following reasons:

  1. 每次用户请求创建一本新书时,都会重新创建在Gateway中消费BookCreated的侦听器
  2. 如果2个用户同时创建一本书并返回错误的书怎么办?
  3. 我不知道如何获取其他数据(例如getUserFromOtherMicroService)
  1. The listener for consuming BookCreated in the Gateway will be recreated every time a user requests to create a new book
  2. What if 2 users simultaneously create a book and the wrong book will be returned?
  3. I don't know how to fetch additional data (e.g. getUserFromOtherMicroService)


这就是为什么我要实现这种体系结构的原因:


That's why I though about implementing this architecture:

直接和队列通信

网关

router.post('/book', async (req, res) => {
  const book = await makeHttpRequest('microservice-a/create-book', req.body);
  const user = await makeHttpRequest('microservice-b/getUser', book.userId);
  res.json({ book, user });
});

微服务A

router.post('/create-book', (req, res) => {
  const book = createBook(req.body);
  eventStore.insert('BookCreated', book);
  const createdBook = updateProjectionDatabase(book);
  queue.publish('BookCreated', createdBook);
  res.json(createdBook);
})

但是我也不太确定这种实现方式,因为:

But I am also not really sure about this implementation because:

  1. 在创建后退还图书时,我是否违反CQRS? (因为我只能返回OKERROR)
  2. 在微服务系统中发出另一个HTTP请求不是效率低下吗?

推荐答案

基于上述注释.

方法1

在这种情况下,您的api网关将用于将消息放入队列中.如果您的过程将花费很长时间,并且您有一个队列工作人员坐在后面来拾取消息和过程,则此方法更合适.但是您的客户端必须轮询才能获得结果.假设您要机票.您删除该消息.您将获得一个要轮询的ID.您的客户将继续轮询,直到结果可用为止.

In this case your api gateway will be used to drop the message in the queue. This approach is more appropriate if your process is going to take long time and you have a queue workers sitting behind to pick up the messages and process. But your client side has to poll to get the results. Say you are looking for airline ticket . You drop the message. Your get an ID to poll. Your client will keep on polling until the results are available.

但是在这种情况下,您将面临一个挑战,因为您会丢弃该消息,将如何生成客户端将轮询的ID?您是否将ID分配给Gateway上的消息并放入队列并返回相同的ID供客户端​​轮询以获取结果?同样,此方法适用于Web/工作人员这种情况.

But in this case you will have a challenge , as you drop the message how you are going to generate the ID that client would poll ? Do you assign the ID to message at Gateway and drop in the queue and return the same ID for the client to poll to get result ? again this approach is good for web/worker kind of scenario.

方法2

由于您的API网关是自定义应用程序,可以处理身份验证并将请求重定向到相应的服务.您的Microsvc A将创建书籍并发布该事件,而您的Microservice B和C将使用它.您的网关将等待微服务A返回创建的图书的ID(或新创建的对象的事件元数据)的响应,这样您以后就不必再对其进行轮询,客户端就会拥有它.如果您希望获得其他微服务的其他信息,则可以在此时获取并发送汇总的响应.

Since Your API gateway is custom application that would handle the authentication and redirect the request to respective service. Your Microsvc A would create book and publish the event and your Microservice B and C would be using it . Your Gateway will wait for the Microservice A to return response with ID (or event metadata of newly created object) of book that is created so you don't poll for it later and client has it. If you want you can have additional information from other microservices you can fetch at this time and can send aggregated response.

对于Microservice A,B,C中可用的任何数据,您将通过网关获取.确保您的网关高度可用.

For any data that is available in Microservice A,B,C you will be getting via Gateway. Make sure your gateway is highly available.

希望有帮助.让我知道您是否有任何疑问!

Hope that helps . Let me know if you have any questions !

这篇关于微服务创建实体实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆