Reputation: 209
I am trying to set up a microservice cluster with docker and Nestjs but every time I try to call a microservice it gives me back a connection refused.
version: "3.8"
services:
api:
build:
dockerfile: Dockerfile
context: .
depends_on:
- postgres
- microservice
ports:
- "8080:3000"
environment:
DATABASE_URL: postgres://user:password@postgres:5432/db
NODE_ENV: development
PORT: 3000
networks:
- local
postgres:
image: postgres:10.4
ports:
- "35000:5432"
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: db
networks:
- local
microservice:
build:
dockerfile: Dockerfile
context: ../microservice
depends_on:
- postgres
ports:
- "8877:9000"
environment:
DATABASE_URL: postgres://user:password@postgres:5432/db
NODE_ENV: development
networks:
- local
networks:
local:
driver: bridge
In my main application code I connect to the microservice through the following code:
constructor() {
this.client = ClientProxyFactory.create({
transport: Transport.TCP,
options: {
host: '127.0.0.1',
port: 8877
}
})
}
And in the microservice I use the following code to set it up:
const microserviceOptions = {
transport: Transport.TCP,
options: {
host: '127.0.0.1',
port: 8877
}
}
async function bootstrap() {
const app = await NestFactory.createMicroservice(AppModule, microserviceOptions);
app.listen(() => {
logger.log('Microservice is listening ...');
});
}
bootstrap();
However, when I perform a request my main container gives the following error back:
[Nest] 19 - 03/24/2021, 8:00:36 AM [ExceptionsHandler] connect ECONNREFUSED 127.0.0.1:8877 +329786ms
api_1 | Error: connect ECONNREFUSED 127.0.0.1:8877
api_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
Upvotes: 0
Views: 4883
Reputation: 1260
I noticed two potential problems:
In case you main app runs in a container within the same network of your microservice, try to set the host to: microservice
(the name of the service in the docker compose file) instead of 127.0.0.1
and the port to the internal ip of the microservice, in your case: 9000
You are telling the microservice to listen on port 8877, i guess you need to listen to 9000 as your docker compose file suggests.
Upvotes: 2