本文介绍了微服务架构的数据库设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正计划使用微服务架构来实施我们的网站.我想知道在服务之间共享数据库是否合适,或者为每个服务都拥有一个单独的数据库是否更可取.在这方面,我可以考虑为所有服务使用一个通用数据库,还是违反微服务架构的本质?

I am planning to use the Microservices architecture for the implementation of our website. I wanted to know if it is right to share databases between services or if it is preferable to have a separate database for each service. In this regard, can I consider having one common database for all services or does it violate the very essence of Microservice architecture ?

推荐答案

微服务提供去耦.您必须将应用程序分解到独立的域中.每个域可以有一个数据库.如果其他MS需要访问某些其他微服务拥有的数据,则它们必须通过网络进行通信.

Microservices offers decoupling. You must break down your application into independent domains. Each domain can have a DB. In case other MS needs to access data owned by some other microservices, they have to communicate over the network.

如果您觉得依赖服务太多,并且网络调用太多,则可以定义一个域,将依赖服务聚集在一起.

In case you feel that there are too many dependent services and the network calls would be too much, then you can define a domain, clustering the dependent services together.

例如-假设我有一个在线测试评估服务,公司的经理可以在其中发布测试,并且他可以查看其部门中所有员工的结果.

For instance -- Suppose I have an online Test evaluation Service where a manager of a company can post tests and he can view results of all the employees in his department.

在这种情况下,我的微服务将是:

My Microservices for this scenario would be:

初始设计

  1. 用户服务:用于登录和用户信息.
  2. 测试服务:评估测试的服务.
  3. 员工:处理员工详细信息
  4. 公司:处理组织CRUD
  5. 部门:处理部门CRUD

在分解之后,似乎员工,组织和部门服务将进行过多的网络/API调用,因为它们之间相互依赖.因此最好将它们聚类.

After breaking it down, seems like employee, Organization and Department service would be making too much network/API calls as they are tightly dependent on each other. So it's better to cluster them.

更新的设计

  1. 用户服务:用于登录和用户信息.
  2. 测试服务:评估测试的服务
  3. 组织:处理公司,员工和部门的相关业务.

每个服务可以拥有自己的数据库,并且可以独立部署.用户和测试服务可以使用mongoDB或任何NoSql数据库和组织服务都可以使用RDBMS.

Each service could have it's own DB and it's independently deployable. User and Test Service can use mongoDB or any NoSql DB and Organization service can use RDBMS.

希望这会有所帮助.

这篇关于微服务架构的数据库设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 06:53