如何将数据库与微服务(以及新服务)同步? [英] How to sync the database with the microservices (and the new one)?

查看:775
本文介绍了如何将数据库与微服务(以及新服务)同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有微服务架构的网站,并且每个服务都拥有一个数据库.该数据库存储微服务所需的数据.

I'm developing a website with the microservice architecture, and each of the service owns a database. The database stores the data which the microservice needs.

PostVideo服务需要用户信息,因此这两个服务都预订了NEW_USER_EVENT.

Post, Video services need the user information, so both of the services subscribed to the NEW_USER_EVENT.

NEW_USER_EVENT将在注册新用户时触发.

The NEW_USER_EVENT will be triggered when there's a new user registered.

服务收到NEW_USER_EVENT后,它们会将传入的用户信息放入各自的数据库中.这样他们就可以做事而无需询问User服务.

Once the services received the NEW_USER_EVENT, they put the incoming user information to each of their own database. So they can do things without asking the User service.

到目前为止,一切都很好.但是问题来了:

So far so good. But here comes the question:

  • 如果我要创建新服务怎么办?如何获取已注册的用户信息并将其放入新服务中?

也许我可以从现有服务中获取信息.但是事件由消息传递队列(NSQ)推送.

Maybe I can get the informations from the existing services. But the events are pushed by the messaging queue (NSQ).

如果我要从一个微服务中复制数据,如何确定哪个服务具有最新的用户信息? (因为某些服务没有收到最新事件)

If I'm going to copy the data from one of the microservices, how do I make sure which service has the latest user informations? (Because some services haven't received the latest event)

了解详情:

关于微服务的最困难部分:您的数据

微服务简介,第4部分:依赖关系和数据共享

推荐答案

如果我要创建新服务怎么办?如何获取已注册的用户信息并将其放入新服务中?

What if I'm going to create a new service? How do I get the registered user informations and put them in the new service?

您必须从头开始重播订阅此新服务的所有事件(您应该有一个事件存储",该事件存储将所有事件都保留在您的应用程序中).同样,从最近的事件开始并返回到过去,您可以在重播事件时使用更智能的逻辑.这样,您将能够首先还原最有价值的数据.只要注意正确处理相互依赖的事件即可.

You have to replay all events to which this new service is subscribed from the beginning of time (you should have an "event store" that keeps all events already happened in your application). Also, you can put a bit smarter logic when replaying events by starting from the most recent ones and going back in time. In this way, you will be able to restore most valuable data first. Just be careful to handle interdependent events correctly.

数据源:事件是由消息传递队列(NSQ)推送的,如果我要从其中一个微服务复制数据,如何确保复制源具有最新的用户信息?

Data source: The events are pushed by the messaging queue(NSQ), If I'm going to copy the data from one of the microservices, how do I make sure the copy source has the latest user informations?

不是谈论进行备份,对吗?

You are not talking about doing backups, right?

除备份外,在事件驱动的系统中,人们通常不会以经典的方式逐行复制数据.相反,它们只是从时间开始重播事件存储中的事件,并将这些事件提供给新服务(或新实例)的事件处理程序.结果,新服务最终将与系统的其他部分保持一致.

Aside from backups, in the event-driven systems people usually don't copy the data in a classical way, row by row. Instead, they just replaying events from event store from the beginning of time and feeding those events to the event handlers for the new service (or new instance). As a result, new service eventually becomes consistent with the other parts of the system.

这篇关于如何将数据库与微服务(以及新服务)同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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