简体   繁体   English

事件驱动的微服务ID生成

[英]event-driven microservices id generation

I'm a newbie with microservices. 我是微服务的新手。 I'm trying to create a microservices architecture where there is an API gateway that should just receive the request and create an event accordingly. 我正在尝试创建一个微服务架构,其中有一个API网关应该只接收请求并相应地创建一个事件。 Then the event will be intercepted by a microservice that stores the needed data into a database. 然后,该事件将被微服务拦截,该微服务将所需的数据存储到数据库中。

Maybe I'm making a mistake with the design but I expect that after a client calls the API gateway the request proceeds asynchronously and the data consistency won't be guaranteed. 也许我在设计上犯了一个错误,但是我希望在客户端调用API网关后,请求将异步进行,并且不能保证数据的一致性。

So how the client knows if the resource has been created and its id? 那么客户端如何知道资源是否已创建及其ID?

Should the client listen to the events as well? 客户也应该听事件吗?

Is this the right architecture or am I going through the wrong path? 这是正确的体系结构还是我走错了路?

Thank you in advance for your comments! 预先感谢您的评论!

Note: I'm not using any structured framework. 注意:我没有使用任何结构化框架。 I like them but this is mostly an experiment and I'd like keep everything simple. 我喜欢它们,但这主要是一个实验,我希望一切都保持简单。 Anyway I'm opened if your suggestion involve spring or whatever java framework. 无论如何,如果您的建议涉及spring或任何Java框架,我都会开放。

(Edit) (编辑)

Another interesting point. 另一个有趣的观点。 Let's give that the API response is asynchronous, if the client has to insert an aggregated data made by two resources (identified by their own id), how this can be achieved through an event-driven architecture? 让我们假设API响应是异步的,如果客户端必须插入由两个资源(由其自己的ID标识)构成的聚合数据,那么如何通过事件驱动的体系结构来实现此目的?

You have two choose between synchronous calls and asynchronous calls, the latter permitting a more resilient architecture so if this is what you want then go with it. 在同步调用和异步调用之间有两种选择,后者允许更灵活的体系结构,因此如果您要这样做,则可以选择它。

Maybe I'm making a mistake with the design but I expect that after a client calls the API gateway the request proceeds asynchronously and the data consistency won't be guaranteed. 也许我在设计上犯了一个错误,但是我希望在客户端调用API网关后,请求将异步进行,并且不能保证数据的一致性。

As the call is asynchronous, you will have eventual consistency . 由于调用是异步的,因此最终将具有一致性

So how the client knows if the resource has been created and its id? 那么客户端如何知道资源是否已创建及其ID?

It doesn't know. 不知道 I see two choices: 我看到两个选择:

  1. the client generates the IDs, preferable GUIDs (or any stateless unique ID) - the preferred way for high scalability. 客户端生成ID,更可取的GUIDs (或任何无状态的唯一ID),这是实现高可伸缩性的首选方法。 Then, the client polls the server to check the status of the resource by using that GUID or HATEOAS URLs returned by the server. 然后,客户端使用服务器返回的GUIDHATEOAS URL轮询服务器以检查资源的状态。
  2. the client sends the requests without a preexisting ID but the server returns an endpoint URL where the client can poll for the command status, probably using an unique command ID (ex: /commands/1234-abcd-5678-efgh/status ); 客户端发送的请求没有预先存在的ID,但是服务器返回了一个端点URL,客户端可以在其中轮询命令状态,可能使用唯一的命令ID(例如: /commands/1234-abcd-5678-efgh/status ); after the command is executed, the server returns the created resource ID or the resource URL (in case of having a RESTFUL service/ HATEOAS ) 执行该命令后,服务器将返回创建的资源ID或资源URL(如果具有RESTFUL服务/ HATEOAS

Separate events and commands . 分开的事件命令

Events is something that happened in the past, sending an event to the server responds only with an ok that it is received. 事件是过去发生的事情,将事件发送到服务器仅会收到一个确定的响应。

Commands is something that you use when you need a response, for instance an id, or when you create a user and you need to check if the e-mail is already in use or not. 命令是您需要响应时使用的东西,例如id或创建用户时,需要检查电子邮件是否已在使用中。 The command then send an event "usercreated" with the user information, and return the id of the user to the one who ran the command (eg frontend client). 然后,该命令发送一个带有用户信息的“用户创建”事件,并将用户的ID返回给运行该命令的用户(例如,前端客户端)。

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

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