如何查看本地 Node.js dockerized 微服务设置中分布式事件的顺序?

问题描述 投票:0回答:1

我本地很少有在 docker 容器中运行 Node.js 应用程序的服务。

流程如下所示:

基本上,如果服务 B 正在工作,并且它在数据库中记录了处理 S 的计算,则其他请求应该无法获取 S 结果数据(因为它不存在)。

我如何知道事件的顺序以及与它们相关的一些元数据(例如 - 服务 B 将 S 的状态设置为

started
,并且它是在服务 A 的第二个线程想要获取其他请求的 S 结果数据之前)?

我正在考虑集中式日志记录,但某些事件/分布式事件跟踪也可以解决问题。越简单越好。

解决方案应该仅使用我的本地计算机,可以是带有一些应用程序的一些 Docker 映像 - 但不是付费云服务。

node.js docker logging microservices metrics
1个回答
0
投票

我使用

fs.appendFile
来获得所需的结果(使用 macOS)。

在代码中创建了 recordLog.service.ts,所有应该捕获事件的地方都可以访问它:

import {appendFile} from 'fs';

const fileName = './centralized.log';

export const recordLog = async (message: string, serviceName: string, entityId: string | number) => {
  const timestamp = new Date().toISOString();
  const logMsg = `${timestamp} [${serviceName}] [${entityId}] ${message}\n`;

  appendFile(fileName, logMsg, {flag: 'a'}, () => {});
}
© www.soinside.com 2019 - 2024. All rights reserved.