[문제 정의]
MSA-monorepo 형식으로 처음 작업을 하면서 member서버와 main서버를 키고 기능구현 테스트를 하려고 했다.
그리고 포스트맨을 통해 요청을 했더니 터미널에 아래와 같이 오류가 발생했다.
Exception Error: Connection closed
at ClientTCP.handleClose (/Users/shimjaedoo/Desktop/Study/MSA-monorepo/node_modules/@nestjs/microservices/client/client-tcp.js:102:25)
at Socket.<anonymous> (/Users/shimjaedoo/Desktop/Study/MSA-monorepo/node_modules/@nestjs/microservices/client/client-tcp.js:92:55)
at Socket.emit (node:events:526:35)
at TCP.<anonymous> (node:net:337:12)
[원인 추론]
터미널의 로그를 보면 TCP 관련 오류를 찾아볼 수 있다. TCP연결이 안되어있나? 라는 생각이 제일 먼저 들었고 try-catch문을 통해 오류를 추적하였는데 connect econnrefused 127.0 0.1 3000 이렇게 오류가 반환되었다. 이것을 가지고 구글에 그대로 검색을 했더니
서버가 실행되고 있지 않거나, 포트가 사용 중일 수도 있다는 글들이 있어서 확인을 하고 시도를 해보았다.
[조사 방법]
위의 방법대로 시도를 해봐도 똑같은 오류가 발생을 해서, 처음 이 프로젝트를 기획했을 때 봤던 공식문서를 다시 보기로 하였다.구글에서 nestjs msa connect ECONNREFUSED 이렇게 검색을 하였더니 NestJS 공식문서가 있어 참고하였다.
참고한 NestJS 공식문서 : https://docs.nestjs.com/microservices/basics
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Rea
docs.nestjs.com
[조사 방법 구현]
실수를 했던 것이 메인 서버의 members쪽의 모듈에서는 TCP와 통신할 준비를 하였는데, member 서버에서는 TCP와 통실 할 준비를 하지 않아서 였다. 그래서 연결을 못했던 것이였다.
const app = await NestFactory.create(MemberModule);
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
enableDebugMessages: true,
transform: true,
transformOptions: {
enableImplicitConversion: true,
},
}),
);
// member : main.ts
공식문서 버전
[결과]
TCP와 통신할 수 있게 바꿨다.
const app = await NestFactory.createMicroservice<MicroserviceOptions>(MemberModule, {
transport: Transport.TCP,
options: {
host: process.env.TCP_HOST,
port: process.env.TCP_PORT,
},
});
// member : main.ts
TCP통신에 대해 익숙치 않다보니 시간이 생각보다 더 오래걸렸던것 같고, MSA를 처음 진행하다 보니 생각할 게 많았던 것 같다.
한 쪽은 통신 할 준비가 됐는데 다른 한쪽은 안됐기 때문에 오류가 발생했던 것인데, 다음번에는 이번을 계기로 시간을 줄여봐야겠다.
그리고 options 중에서도 나는 host와 port만 사용했는데, 다른 TCP 옵션들이 많아서 다음에는 기획에 맞게 여러가지를 사용해봐야 겠다. 많이 사용 할 것 같은 옵션들을 남겨둔다.
host | 연결 호스트 이름 |
port | 연결 포트 |
retryAttempts | 메시지 재시도 횟수(기본값: 0) |
retryDelay | 메시지 재시도 간격(ms)(기본값: 0) |