如何在微服务世界中解析作者的书籍名称?

Rod*_*Rod 11 amazon-web-services node.js microservices aws-lambda

所以我开始了微服务的旅程.我花了几个小时在网上尝试沉浸在这个话题中.

我还没有完全理解的一个概念是不使用SQL连接的想法,因此为作者提供了一个小的独立数据库,对于书籍也有相同的独立数据库.

所以我理解以下SQL:

BooksTable - id, name, authorid
AuthorsTable - id, name

select book.name, author.name from book 
join author on book.authorId = author.id
Run Code Online (Sandbox Code Playgroud)

在Node.js世界中

index.js

app.get('/api/books' bookDomain.get());
Run Code Online (Sandbox Code Playgroud)

bookDomain.js

exports.get = () => {
  const books = bookService.get();

  const authors = authorService.get();

  /*
    This is where I'm lost: how do you achieve the simple SQL 
    above? I'm assuming in the domain is where this information is 
    "joined"? am I correct?
  */
};
Run Code Online (Sandbox Code Playgroud)

服务

Database1
**bookService.js**
database context

Database2
**authorService.js**
database context
Run Code Online (Sandbox Code Playgroud)

预期的数据(类似的东西,基本上我说JSON应该是返回类型)

[{
  book {
    "name": "Book 1",
    "author": "Author Name 1"
  }
},
{
  book {
    "name": "Book 2",
    "author": "Author Name 2"
  }
}]
Run Code Online (Sandbox Code Playgroud)

ent*_*erd 8

我至少有三种方法可以解决这个问题.

  1. 文档模型NoSQL DB:考虑使用像Mongo DB这样的文档模型NoSQL DB,而不是使用SQL DB.因此,而不是与像表一个关系数据库BooksAuthorsAuthorBooks表都加入了一对外国键-这看起来几乎可以使用文档的NoSQL数据库一样蒙戈您的书籍存储为文档类型,以BSON格式与您问题中的JSON相同.像Mongo这样的文档数据库的一个很好的功能是你可以在Book文档中设置JSON索引Author,从而缩短你的查询时间.这在Martin Fowler推出的NoSQL Distilled中得到了很好的讨论(Sec.2.2和Chp.9).
  2. 打破外键关系,以便服务而不是数据库维护参照完整性:而不是依靠关系数据库为您强制执行参照完整性(通过维护外键)限制数据库访问您的微服务和通过您的服务本身维护外键的完整性.Sam Newman在建筑微服务第84-85页讨论了这种策略.
  3. 对数据库进行非规范化:只需将每个表用于书籍和作者,而不是将它们组合成非规范化表.因此,请创建一个新表格,其中您的图书信息会重复,并且每行每本图书的作者信息都是唯一的.它很丑.搜索现在有更大的搜索空间,但也很简单.例如,类似以下格式:

    book_id | book_name              | book_author
    =====================================================
          1 | NoSQL Distilled        | Pramod J. Sadalage
    -----------------------------------------------------
          1 | NoSQL Distilled        | Martin Fowler
    -----------------------------------------------------
          2 | Building Microservices | Sam Newman
Run Code Online (Sandbox Code Playgroud)