简体   繁体   English

使用微服务和Spring Boot进行分布式事务

[英]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: 通常有两种方法可以实现相同的目的:

  1. SAGA Pattern - You read it SAGA模式-您已阅读
  2. You make transaction in one service , then another service listen to the event and perform update. 您在一项服务中进行交易,然后另一项服务侦听该事件并执行更新。 Like on user removal , generate a event UserRemoved and Employee service listen to it and remove employee. 就像在删除用户时一样,生成事件UserRemoved,然后Employee服务监听该事件并删除employee。

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.

相关问题 用于Spring启动微服务的MySQL配置 - MySQL configuration for Spring boot micro-services Java Spring-boot 微服务异常处理 - Java Spring-boot micro-services Exception handling 如何在spring-boot中启用跨域微服务 - How to enable cross-origin in spring -boot for micro-services 微服务授权和会话维护-Spring Boot - Micro-services Authorization and session maintenance - spring boot spring启动微服务中的分布式redis缓存 - Distributed redis cache in spring boot micro services Netflix Arcaius充当多个Spring Boot微服务的配置服务 - Netflix Arcaius serving as config service for multiple Spring Boot micro-services 使用Angular和Spring Boot微服务的基于PKI证书的身份验证 - PKI certificate based authentication using Angular and Spring Boot Micro-services Apache Camel 是否替代或补充使用 Spring Boot 创建微服务? - Does Apache Camel replace or complement creating micro-services with Spring Boot? 如何将Spring-boot调度器设计为Service,以便其他微服务可以使用 - How to Design a Spring-boot scheduler as Service, so that other micro-services can use it 如何在两个微服务(Spring-boot)之间进行两阶段提交? - How to do 2 phase commit between two micro-services(Spring-boot)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM