我正在使用 NestJS 和 gRPC 开发一个微服务项目。 当我在本地运行我的项目时,它运行成功,但是当项目上传到 docker 时,我收到错误消息“Error:14 UNAVAILABLE:未建立连接”。
这是我写的 docker-compose 文件。
version: '3.7'
services:
gateway-service:
container_name: gateway-service
restart: always
build:
context: .
dockerfile: './apps/gateway-service/Dockerfile'
volumes:
- .:/app
- /app/node_modules
env_file:
- .env
environment:
- MONGODB_HOST=mongodb://mongodb:27017/
ports:
- 3000:3000
command: npm run start:dev gateway-service
networks:
- sgod_network
depends_on:
- mongodb
user-service:
container_name: user-service
restart: always
build:
context: .
dockerfile: './apps/user-service/Dockerfile'
volumes:
- .:/app
- /app/node_modules
env_file:
- .env
environment:
- MONGODB_HOST=mongodb://mongodb:27017/
command: npm run start:dev user-service
networks:
- sgod_network
depends_on:
- mongodb
mongodb:
image: mongo
ports:
- 27018:27017
networks:
- sgod_network
networks:
sgod_network:
driver: bridge
这里我已经注册了gRPC。
@Module({
imports: [
ClientsModule.register([
{
name: USER_PACKAGE_NAME,
transport: Transport.GRPC,
options: {
package: USER_PACKAGE_NAME,
protoPath: join(__dirname, '.././user.proto'),
url: '127.0.0.1:50001',
},
},
]),
],
controllers: [GatewayServiceController],
providers: [GatewayServiceService],
})
在用户服务中我还配置了 gRPC
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
UserServiceModule,
{
transport: Transport.GRPC,
options: {
package: USER_PACKAGE_NAME,
protoPath: join(__dirname, '.././user.proto'),
url: '127.0.0.1:50001',
},
},
);
这里有两种方法可以解决这个问题。
有关更多详细信息,您应该探索 Docker 的两种网络类型:主机和桥接器。