why do we need to config different databases in micro services?
Because all the microservices are loose coupled with each other. so that means when one microservice change something in db, it won’t infect to other microservices.
但是我们又不能将DB完全分开 为什么呢?因为这样会造成大量的重复的data. 我们不能用之前的遵照ACID的tranactions.
所以 现在好像是个思路 即分不开(造成大量duplicate data)也不能分开(每个microservice都要用到同一个数据库中的某几张表)
The core characteristic of the microservices architecture is the loose coupling of services. To achieve that, each service must have its own private data store. (为什么一定要分开呢??一起使用然后用一个handler来解决并发问题不好吗????)
我们有5个pattern可以解决这个问题:
-
db per service pattern: (use when the microservice not too complicated)
就是说 每个service都要搞一个DB 但是如果这个service 需要访问其他数据库 就要通过API请求。
-
sage pattern:
就是说 我们现在不是数据库意直接访问数据库了
而是通过messaging system。 一个local事务update db and 通过messaging triggers另一个服务的local事务来update对应的db.
在这里插入图片描述 -
api composition pattern
当我们需要一个query会影响到几个微服务的数据库。用这个pattern. 就是用一个api composer来统筹所有的更新查询工作。
…知道这几个就差不多了。
All You Need to Know About Microservices Database Management