简体   繁体   English

微服务和多个数据库

[英]microservices and multiple databases

i have written MicroServices like for auth, location, etc. All of microservices have different database, with for eg location is there in all my databases for these services.When in any of my project i need a location of user, it first looks in cache, if not found it hits the database. 我已经为auth,location等编写了MicroServices。所有的微服务都有不同的数据库,例如,我的所有数据库都有这些服务的位置。当我的任何项目中我需要一个用户的位置时,它首先查看缓存,如果没有找到它会命中数据库。 So far so good.Now when location is changed in any of my different databases, i need to update it in other databases as well as update my cache. 到目前为止一直很好。现在,当我的任何一个不同的数据库中的位置发生变化时,我需要在其他数据库中更新它以及更新我的缓存。

currently i made a model (called subscription) with url as its field, whenever a location is changed in any database, an object is created of this subscription. 目前我创建了一个模型(称为订阅),其中url作为其字段,每当在任何数据库中更改位置时,都会创建此订阅的对象。 A periodic task is running which checks for subscription model, when it finds such objects it hits api of other services and updates location and updates the cache. 正在运行的周期性任务会检查订阅模型,当它找到此类对象时,它会访问其他服务的api并更新位置并更新缓存。 I am wondering if there is any better way to do this? 我想知道是否有更好的方法来做到这一点?

I am wondering if there is any better way to do this? 我想知道是否有更好的方法来做到这一点?

"better" is entirely subjective. “更好”完全是主观的。 if it meets your needs, it's fine. 如果满足您的需求,那很好。

something to consider, though: don't store the same information in more than one place. 但要考虑的事项是:不要在多个地方存储相同的信息。

if you need an address, look it up from the service that provides address, every time. 如果您需要地址,请每次从提供地址的服务中查找。

this may be a performance hit, but it eliminates the problem of replicating the data everywhere. 这可能会影响性能,但它消除了在任何地方复制数据的问题。

another option would be a more proactive approach, as suggested in comments. 正如评论中所建议的那样,另一种选择是更积极主动的做法。

instead of creating a task list for changes, and doing that periodically, send a message across rabbitmq immediately when the change happens. 而不是为更改创建任务列表,并定期执行此操作,在更改发生时立即在rabbitmq上发送消息。 let every service that needs to know, get a copy of the message and update it's own cache of info. 让每个需要知道的服务,获取消息的副本并更新它自己的信息缓存。

just remember, though. 但请记住。 every time you have more than one copy of the information, you reduce the "correctness" of the system, as a whole. 每次有多个信息副本时,都会降低整个系统的“正确性”。 it will always be possible for the information found in one of your apps to be out of date, because it did not get an update from the official source. 您的某个应用中发现的信息总是有可能过时,因为它没有从官方来源获得更新。

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

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