服务设计
如图所示,热搜服务(Trend Service)从消息队列消费”search book“事件,分析数据并列出热门图书给用户。
热搜服务应该至少包含2个组件:
- 消费者: 负责消费并分析队列中的事件。
- API服务器: 负责向用户展示热搜。
本教程中有 3 个服务,但我们没必要整 3 个仓库。既然都是 Node.js 开发的,可以尝试一下单仓(monorepo)。
单仓 Monorepo
单仓即”单体仓库“(monolithic repository),是一种将多个项目或系统模块存放在一个仓库内的软件开发方式。
我们需要重整一下项目结构:
.
├── LICENSE
├── README.md
├── package-lock.json
├── package.json
├── src
│   ├── domain
│   │   └── model
│   │       ├── book.ts
│   │       └── index.ts
│   ├── recommendation
│   │   └── app.ts
│   ├── trend
│   │   └── app.ts
│   └── web
│       ├── adapter
│       │   ├── index.ts
│       │   ├── router.ts
│       │   └── templates
│       │       └── index.handlebars
│       ├── app.ts
│       ├── application
│       │   ├── executor
│       │   │   ├── book_operator.ts
│       │   │   └── index.ts
│       │   ├── index.ts
│       │   └── wire_helper.ts
│       ├── config.json
│       ├── domain
│       │   └── gateway
│       │       ├── book_manager.ts
│       │       └── index.ts
│       └── infrastructure
│           ├── config
│           │   ├── config.ts
│           │   └── index.ts
│           ├── database
│           │   ├── index.ts
│           │   └── mysql.ts
│           └── mq
│               ├── index.ts
│               └── kafka.ts
└── tsconfig.json
重组目录结构
- 移动 src/ 的子目录 adapter/,application/,domain/gateway/ 和 infrastructure/ 到 src/web/。
- 移动 app.ts 和 config.json 文件到 src/web/。
- 更新相关文件的 import 路径。
- 修改文件中路径配置值。
调整 src/web/app.ts:
- const config_filename = "config.json";
+ const config_filename = "src/web/config.json";
调整 src/web/config.json:
-     "templates_dir": "src/adapter/templates"
+     "templates_dir": "src/web/adapter/templates"
调整 package.json:
- "dev": "ts-node src/app.ts",
+ "dev-web": "ts-node src/web/app.ts",