database 具有共享数据库的微服务?使用多个 ORM?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43612866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:05:14  来源:igfitidea点击:

Microservices with shared database? using multiple ORM's?

databasedatabase-designormarchitecturemicroservices

提问by Eduardo Veras

I'm learning about microservices and I'm gonna build a project with a microservices architecture.

我正在学习微服务,我将使用微服务架构构建一个项目。

The thing is, one of my team mates want to use one database for all services, sharing all tables so "data doesn't get repeated", each service would be built with different frameworks and languages like django and rails which use very different ORM standards.

问题是,我的一个团队成员想要为所有服务使用一个数据库,共享所有表,这样“数据不会重复”,每个服务都将使用不同的框架和语言构建,例如 django 和 rails,它们使用非常不同的 ORM标准。

What would be the correct approach? Since I think working with one database would involve a lot of "hacking" the ORMs in order to make them work correctly.

什么是正确的方法?因为我认为使用一个数据库会涉及很多“黑客”ORM 以使其正常工作。

回答by Oswin Noetzelmann

You are not likely to benefit from a Microservices architecture if all the services share the same database tables. This is because you are effectively tightly coupling the services. If a database table changes all the services will have to change.

如果所有服务共享相同的数据库表,您就不太可能从微服务架构中受益。这是因为您有效地紧密耦合了服务。如果数据库表更改,则所有服务都必须更改。

You have to understand that the whole reason for a Microservices architecture is to reduce dependencies between development teams and allow them to move ahead independently with fast releases.

你必须明白,微服务架构的全部原因是为了减少开发团队之间的依赖关系,并允许他们通过快速发布独立推进。

Here is a quote from Werner Vogels, the Amazon CTO (Amazon pioneered a lot of the Microservices style architecture):

这是亚马逊 CTO Werner Vogels 的引述(亚马逊开创了许多微服务风格的架构):

For us service orientation means encapsulating the data with the business logic that operates on the data, with the only access through a published service interface. No direct database access is allowed from outside the service, and there's no data sharing among the services.

对我们来说,面向服务意味着使用对数据进行操作的业务逻辑封装数据,并且只能通过已发布的服务接口进行访问。不允许从服务外部直接访问数据库,并且服务之间没有数据共享。

For more information read thisand this.

有关更多信息,请阅读

回答by PiotrWolkowski

In general a microservice should be responsible for it's own data. That's a perfect world scenario.

一般来说,微服务应该负责它自己的数据。这是一个完美的世界场景。

In practice some of the services may be highly related to each other. E.g. CustomerShippingDetails and CustomerShoppingCheckout services may both access the same data - customer address. How would you then solve a problem of providing customer address to the customer checkout service. If the checkout service queries the shopping details directly then you break loose coupling between services. Other option is to introduce a shared database.

在实践中,一些服务可能彼此高度相关。例如 CustomerShippingDetails 和 CustomerShoppingCheckout 服务可能都访问相同的数据 - 客户地址。那么您将如何解决向客户结账服务提供客户地址的问题。如果结账服务直接查询购物详情,那么您就打破了服务之间的松散耦合。另一种选择是引入共享数据库。

There will always have to be some kind of compromise on the architecture. What is sacreficed is an architectural decision that highly depends on the big picture (the design of the whole system).

在架构上总会有某种妥协。牺牲的是一个高度依赖于大局(整个系统的设计)的架构决策。

Not having too many details about your system I would go with a mixed approach. That is, having a shared database for services that take care of similar business logic. So CustomerShippingDetails and CustomerShoppingCheckout can share a database. But a StoreItemsDetails would have a separate database.

没有太多关于您的系统的详细信息,我会采用混合方法。也就是说,为处理类似业务逻辑的服务拥有一个共享数据库。因此 CustomerShippingDetails 和 CustomerShoppingCheckout 可以共享一个数据库。但是 StoreItemsDetails 会有一个单独的数据库。

You can find more about shared database pattern for microservices at Microservice Architecture.

您可以在微服务架构中找到有关微服务共享数据库模式的更多信息。