[英]distributed transactions with micro-services and Spring Boot
currently i'm working on migrating a monolith app into microservices and the first issue i encountered is the famous distributed transactions problem. 目前,我正在将Monolith应用程序迁移到微服务中,遇到的第一个问题是著名的分布式事务问题。
i have a micro-service called authentication microservice having as a mission to authenticate users using Oauth2. 我有一个称为身份验证微服务的微服务,其任务是使用Oauth2对用户进行身份验证。
my problem is as follows : 我的问题如下:
the front-end is filling a form and sending a lot of data, some of it belong to the employee microservice and the other to the authentification service 前端正在填写表单并发送大量数据,其中一些属于员工微服务,另一种属于身份验证服务
so when i receive this data i must add at once a user and an employee. 因此,当我收到此数据时,必须立即添加一个用户和一个雇员。 imagine now that the user was added but the employee not due to some sort of failure ?
想象现在添加了用户,但员工不是由于某种原因失败了吗? or even worse imagine when i'm deleting a user and the employee won't get deleted ?
甚至更糟的是,当我删除用户时,员工不会被删除?
so you may have in mind the 2PC or saga pattern, i invested 2 days reading and weighing the possibilities of using those solutions but it complicates things and i think that my problem is not worth it. 因此您可能会想到2PC或传奇模式,我花了2天的时间阅读并权衡使用这些解决方案的可能性,但这使事情变得复杂,我认为我的问题不值得。
i posted the question in order to seek any new ideas or maybe there's some new technology i'm missing. 我发布问题是为了寻求任何新想法,或者也许我缺少一些新技术。
thank you 谢谢
Adding a user doesn't need to be the job of authentication service. 添加用户不必是身份验证服务的工作。 Insert both the employee and the user in the same service, and notify the auth service of the new user.
将员工和用户都插入同一服务,然后将新用户通知auth服务。 (using one of many events/messaging systems)
(使用许多事件/消息传递系统之一)
There are in general two ways to achieving the same: 通常有两种方法可以实现相同的目的:
This is very important to understand in microservices: 在微服务中了解这一点非常重要:
Sometimes the api that you expose lets say create employee, it should create the user and employee both and return employee id and user id at the same time so these things could not be handled in any async transaction. 有时,您公开的api可以说是创建雇员,它应该同时创建用户和雇员,并同时返回雇员ID和用户ID,这样在任何异步事务中都无法处理这些事情。
For these type of things we need to make system to auto correct such inconsistencies. 对于这些类型的事情,我们需要使系统能够自动纠正这种不一致。 For eg:
例如:
Lets say in CreateEmployee , first user id created the employee was not created because of some error, then again if somebody create a employee with same username what should id do. 可以说,在CreateEmployee中,创建第一个用户ID时由于某种错误而未创建该员工,然后再说一次,如果有人使用相同的用户名创建了一个员工,ID应该做什么。 In this case if create employee handle such case that if already a user exists for that check whether employee also exists if not create employee and return.
在这种情况下,如果创建员工来处理这样的情况:如果该用户已经存在,请检查是否存在员工(如果不创建员工并返回)。
There will be more such cases, so in microservices system should be auto correctable and eventual consistent. 这样的情况将会更多,因此在微服务系统中应该是自动可纠正的,并且最终是一致的。 Trying everyting completely consistent everytime is not the right goal.
每次尝试完全一致的做法不是正确的目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.