英文:
Synchronize tables in SpringBoot multitenant architecture
问题
我在一个多租户架构上运行着一个 SpringBoot 应用程序。
我有两个数据库,一个是 Admin,另一个是 Client(两者都是 MySQL),这两个数据库都有一个 User 表。
客户可以将用户添加到 User 表中,但我希望它们能够在 Admin 数据库的 User 表中同步。
有没有办法可以实现这一点?
我已经阅读了关于 Flyway 迁移的信息,但我认为它更多地适用于数据库架构的更改,而不是值的更改。
请忽略我的错误,因为这是我的第一个问题,非常感谢您的帮助。
英文:
I have a SpringBoot application running on a multitenant architecture.
I've two databases Admin and Client (both are MySQL) and both these databases have a User table
Client can add users to the User Table but I need them to get synchronized in the User table of Admin database.
Is there a way I can achieve this?
I've read about flyway migrations but I think it works more on database schema changes and not values.
Please ignore my mistakes as this my first question, any help would be appreciated.
答案1
得分: 3
这看起来像是解决您问题的方案:
SymmetricDS 是一种能够在多个数据库之间复制关系型数据库表的软件。它还可以用于在多个主机之间复制文件和目录。它使用轻量级的基于 Web 的协议来发送和接收数据,这使得它在使用防火墙时非常容易处理。复制是在后台异步完成的,允许在离线模式下进行数据更改。它支持大多数商业和开源数据库平台。
它是如何工作的?
在数据库中安装触发器以确保捕获数据更改。这意味着应用程序继续像往常一样使用数据库,而无需任何特殊的驱动程序软件。触发器被编写得尽可能小和高效。数据的路由和同步是在数据库外部的 SymmetricDS 进程中完成的。
SymmetricDS 支持许多数据库,并且可以在不同的数据库之间进行复制,包括 Oracle、MySQL、MariaDB、PostgreSQL、MS SQL 等等。
https://www.symmetricds.org/docs/faq
英文:
This looks like a solution to your problem:
SymmetricDS is software that replicates relational database tables between multiple databases. It can also be used to replicate files and directories between multiple hosts. It uses a light-weight, web-based protocol to send and receive data, which makes it easy to work with firewalls. Replication is done in the background asynchronously, allowing data changes in offline mode. It supports most commercial and open source database platforms.
How does it work?
Triggers are installed in the database to guarantee that data changes are captured. This means that applications continue to use the database as usual without any special driver software. The triggers are written to be as small and efficient as possible. Routing and syncing of data is done outside of the database in the SymmetricDS process.
SymmetricDS supports many databases and can replicate across different databases, including Oracle, MySQL, MariaDB, PostgreSQL, MS SQL and many more.
答案2
得分: 1
你需要在流程中创建一些事件,其中客户将用户添加到用户表中。
如果这个“客户端”流程在同一个Java服务中,那么你可以利用Spring的异步事件处理,或者使用一个被标记为@Async的方法(执行数据复制操作)。这可以确保数据复制在单独的线程中进行。
如果“客户端”流程在不同的Java服务中,那么可以使用任何发布-订阅模型(一些开源框架如Kafka、RabbitMQ等可用)。
现在要同时连接两个数据源,Spring的RoutingDataSource在这种情况下会很有用,因为它根据“查找键”选择数据源。或者,你可以在配置中硬编码两个数据源bean(因为在你的情况下是固定的)。
英文:
You need to create some event from the flow where client adds user to the User Table.
If this "client" flow is in same java service then you can make use of Spring's Asynchronous Event Handling or have a method(which does the data copy) marked with @Async. This ensures the data copy happens in separate thread.
If the "client" flow is in different java service, then any publisher-subscriber model can be used (some opensource frameworks available are kafka, rabbitmq etc).
Now to connect to two datasources at the same time, Spring's RoutingDataSource will come handy in this scenario as it works on "lookup key" to choose the datasource. Or else you can hardcode two datasource beans in your config (since it is fixed in your case).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论