简体   繁体   English

Java Spring - 如何保持 2 个数据库/2 个微服务一致 state

[英]Java Spring - how to keep 2 databases/2 microservices in consistent state

I have the following task to do: when new item is saved in first (main) microservice , I should use API froms second microservice and save the same item in other database that is managed by my first microservice.我有以下任务要做:当新项目保存在第一个(主)微服务中时,我应该使用来自第二个微服务的 API 并将相同的项目保存在由我的第一个微服务管理的其他数据库中。 The question is what in case when an attempt to save item in database from second microservice will end up with fail?问题是,如果尝试从第二个微服务将项目保存到数据库中以失败告终怎么办? I want to have consistent databases: when data are saved in db 1 (first microservice), should be also saved in db 2 (second microservice) or shouldn't be saved anywhere.我想拥有一致的数据库:当数据保存在 db 1(第一个微服务)中时,也应该保存在 db 2(第二个微服务)中,或者不应该保存在任何地方。

  1. I have controller:我有 controller:
    @PostMapping(path = "store", consumes = "application/json")
    @ResponseStatus(value = HttpStatus.CREATED)
    public IdDto createStore(Authentication auth, @RequestBody StoreDto storeDto,
                                 BindingResult bindingResult) {

...
Authorization identity = authenticationService.getIdentity(auth);
Store store = commandBus.execute(new CreateStoreCommand(storeDto, details));
...
}

2.Command is sent and handler handles my command: 2.Command被发送并且处理程序处理我的命令:

public class CreateStoreCommandHandler implements CommandHandler<Store, CreateStoreCommand> {
   @Override
    public Store handle(CreateStoreCommand command) {
        ...
        Store store = createStore(...)
        return storeService.save(store);
}
  1. StoreService:商店服务:
public class StoreService {
 @Transactional
    public Store save(@NonNull Store store) {
        
        Store savedStore = storeRepository.save(store);
        publisher.publishEvent(PushEvent.created(savedStore));
        return savedStore;
    }
}
  1. I have to use API to save my object in other service/other database, below is my service that saves data in second microservice我必须使用 API 将我的 object 保存在其他服务/其他数据库中,下面是我在第二个微服务中保存数据的服务
public class SecondMicroserviceService {
 public void saveStore(long xUserId, StoreDto store) {
        try {
            newMicroserviceApi.saveStore(xUserId, store)
                    .firstOrError()
                    .retry(RetrofitUtils.bailOnDurableErrorPredicate(retryAttempts))
                    .blockingGet();
        } catch (HttpException ex) {
            RetrofitUtils.on404Return(ex, Optional.empty());
        }
    }
}

My questions: How to handle such a situation?我的问题:如何处理这种情况? Should I call method SecondMicroserviceService.saveStore in StoreService inside a method:我是否应该在方法内调用方法SecondMicroserviceService.saveStore中的StoreService

@Transactional
    public Store save

? ? And that's it?就这样?

I know that distributed transactions across microservices should be avoided and we should use @Transactional(propagation = Propagation.NEVER)我知道应该避免跨微服务的分布式事务,我们应该使用@Transactional(propagation = Propagation.NEVER)

On the first commit from your micro service A why don't you make a Feign call to another micro service to execute the update?在您的微服务 A 第一次提交时,您为什么不对另一个微服务进行 Feign 调用来执行更新? Or if you have a queuing to make it async then post it to micro service attached queue?或者,如果您有一个队列使其异步,然后将其发布到微服务附加队列?

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

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