微服务数据库设计 [英] database design of microservice

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

问题描述

我想了解微服务的数据库设计.情况如下所示

I would like to know about the database design of microservice. The scenario is given below

微服务A正在使用mysql数据库.如果我要创建微服务A的多个实例:

Microservice A is using mysql database. If I am creating multiple instance of microservice A:

  1. 每个微服务A都应指向同一个数据库(所有实例的公共数据库)或

  1. Each microservice A should point to the same database (common database for all instances) or

每个微服务A都指向其自己的私有数据库

Each microservice A points to its own private database

如果每个微服务实例都指向其自己的私有数据库,如何同步每个实例的数据?你能解释一下最好的方法吗?

If each microservice A instance points to its own private database how to sync the data of each instance? Can you please explain the best way.

推荐答案

我正在创建微服务A的多个实例:

I am creating multiple instance of microservice A:

我假设您的意思是,如果要将微服务A的多个实例作为该微服务的单独实例部署到一台或多台服务器上?

I assume by this you mean that if you are deploying multiple instances of your micro-service A to one or multiple servers as separate instances of that micro-service?

简短答案:

通常,每个微服务都应具有一个数据库.这意味着您的微服务A有一个数据库(叫它)micro-service-A_db. 不管您将微服务A部署到多台服务器(水平扩展)还是在1台服务器上,微服务A的部署实例都在访问并使用一个数据库微服务A_db.
您的微服务A的所有实例都应访问相同的数据库.这是标准且最常见的用例.对此有例外.

In general every micro-service should have one database. This means that your micro-service A has a database (lets call it) micro-service-A_db. Regardless of the fact that you micro-service-A is deployed to multiple servers(horizontal scaling) or on 1 server the deployment instances of your micro-service-A are accessing and using your one database micro-service-A_db.
All your instances of your micro-service-A should access the same database. This is the standard and most common use case. There are exceptions to this.

详细答案:

每个微服务A应该指向同一个数据库(公用数据库) 对于所有实例)或

Each microservice A should point to the same database (common database for all instances) or

总之是.

每个微服务A都指向其自己的私有数据库

Each microservice A points to its own private database

简而言之.但是...

In short no. But...

如上所述,如果您的意思是微服务A的多个实例应分别指向私有数据库,则为否.你不应该那样做.

As assumed above if you mean multiple instances of micro-service-A should point each to private database then NO. You should not do that.

通常,您的应用程序域/业务逻辑会根据某些条件分为微服务.将您的系统划分为每个域多个微服务后(您可以在

In general your application Domain/Business logic is split into micro-services based on some criteria. As soon as you split your system to multiple micro-service per Domain(you can read about it here) each micro-service is responsible only for that part of Domain means all the logic related to it and its storage. Let me give an example. Lets say you have an online shop application and you have 3 micro-services:

  • 产品库存微服务
  • 微型支付服务
  • 订单微服务

每个微服务都有自己的数据库:

Each of these micro-service has its own database:

  • 产品库存微服务具有库存微服务数据库
  • 支付微服务有支付微服务数据库
  • 订单-微服务有订单-微服务-数据库

这些数据库只能由其所有者微服务使用.如果将微服务作为单独的进程/实例部署在一个或多个服务器上,则没有关系.他们所有人都访问同一个数据库.另一方面,您可以肯定地使用多种不同类型的微服务数据库部署,例如具有多个只读副本,使用分片对数据进行分区等等.有很多扩展数据库的方法.关键是微服务只能使用其自己的数据库.

Those databases can only be used by their owner micro-service. If the micro-service is deployed on one or multiple servers as separate process/instance it does not matter. All of them access the same database. On the other hand you can for sure use a lot of different types of deployment of your micro-service databases like having multiple Read-only replicas, use Sharding to partition your data and so on. There are a lot of ways to scale your database. The point is that a micro-service can only use its own database.

除此之外,我可以说此规则有例外:1个数据库-1个微服务.有时,每个微服务域使用2个数据库.在这些示例中,第二个db是针对特定读取/查询操作的某种读取优化的db.这通常与CQRS模式一起使用.如果您想进一步了解它,可以在Stack Overflow以及一般在网络上阅读有关CQRS的更多信息.

Additionally to this I can say that there are exceptions to this rule: 1 database - 1 micro-service. Sometimes people use 2 databases per micro-service Domain. These are examples where the second db is some kind of read optimized db for particular read/query operations. This is often used together with CQRS pattern. If you want to know more about it you can read a lot about CQRS here on Stack Overflow and in general on the web.

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

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