문제 인식
🚫 문제점
- 도커 컴포즈 파일을 만들고 컴포즈 파일 테스트 중 문제 발생
- 생성한 컨테이너가 서로 통신 불가능
- 모든 서버를 도커 컨테이너로 생성했을 때 서로 연결이 안되는 문제 발생
원인 파악
Distributor 서비스에 연결하는 함수
// TcpServer 클래스
connectToDistributor(host, port, notification) {
console.log('Connecting to distributor with:', { host, port });
this.clientToDistributor = new TcpClient(
host, // **<- 기존 host로 통신**
port,
...
...
);
gateway에 연결된 노드(서비스) 정보를 저장하는 함수
// GatewayServer 클래스
async onDistribute(data) {
const connectServices = data.microservices.map((service) => {
const node = service;
const key = node.host + ':' + node.port;
if (!this.mapClients.microservices[key] && node.name !== 'gateway') {
const client = new TcpClient(
node.host, // **<- 기존 host로 통신**
node.port,
(options) => {
...
...
this.mapClients.microservices[key] = {
client: client,
info: node,
};
client.connect();
}
});
🔍 분석
- 생성된 컨테이너의 ip주소는 가상 ip주소(일반적으로 172.x.x.x)
- host와 컨테이너 간의 포트 맵핑은 완료 되었으나 도커간의 통신과는 관련 없음
- 생성된 컨테이너 ip주소를 확인
해결 과정
Distributor 서비스에 연결하는 함수
// Distributor에 연결
connectToDistributor(host, port, notification) {
console.log('Connecting to distributor with:', { host, port });
this.clientToDistributor = new TcpClient(
'distributor', // **<-** docker-compose에서 정의한 서비스 이름 사용
port,
...
..
);
gateway에 연결된 노드(서비스) 정보를 저장하는 함수