Nestcloud 与 docker-compose consul 微服务连接被拒绝
Posted
技术标签:
【中文标题】Nestcloud 与 docker-compose consul 微服务连接被拒绝【英文标题】:Nestcloud with docker-compose consul micro-service connection refused 【发布时间】:2022-01-02 09:32:16 【问题描述】:我已经用 consul 和一堆微服务对一个 Nestcloud 应用程序进行了 dockerize。 但是,我无法启动任何微服务(例如下面的 test-service,因为 consul 似乎拒绝任何 tcp 连接:
[Nest] 1 - 11/23/2021, 9:47:34 PM [NestFactory] Starting Nest application...
[Nest] 1 - 11/23/2021, 9:50:56 PM [ConfigModule] Unable to initial ConfigModule, retrying...
[Nest] 1 - 11/23/2021, 9:47:34 PM [InstanceLoader] ServiceRegistryModule dependencies initialized +114ms
[Nest] 1 - 11/23/2021, 9:47:34 PM [InstanceLoader] LoggerModule dependencies initialized +0ms
[Nest] 1 - 11/23/2021, 9:47:34 PM [InstanceLoader] BootModule dependencies initialized +0ms
[Nest] 1 - 11/23/2021, 9:47:34 PM [InstanceLoader] HttpModule dependencies initialized +6ms
[Nest] 1 - 11/23/2021, 9:47:34 PM [InstanceLoader] AppModule dependencies initialized +2ms
[Nest] 1 - 11/23/2021, 9:47:34 PM [InstanceLoader] ConsulModule dependencies initialized +0ms
[Nest] 1 - 11/23/2021, 9:47:34 PM [ConfigModule] Unable to initial ConfigModule, retrying... +39ms
Error: consul: kv.get: connect ECONNREFUSED 127.0.0.1:8500
这是我的 docker-compose.yaml :
version: "3.2"
services:
test-service:
build:
context: .
dockerfile: apps/test-service/Dockerfile
args:
NODE_ENV: development
image: "test-service:latest"
restart: always
depends_on:
- consul
environment:
- CONSUL_HOST=consul
- DISCOVERY_HOST=localhost
ports:
- 50054:50054
consul:
container_name: consul
ports:
- "8400:8400"
- "8500:8500"
- "8600:53/udp"
image: consul
command: ["agent", "-server", "-bootstrap", "-ui", "-client", "0.0.0.0"]
labels:
kompose.service.type: nodeport
kompose.service.expose: "true"
kompose.image-pull-policy: "Always"
我的微服务测试服务的 Dockerfile:
FROM node:12-alpine
ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV
RUN mkdir -p /usr/src/app
ADD . /usr/src/app
WORKDIR /usr/src/app
RUN yarn global add @nestjs/cli
RUN yarn install --production=false
# Build production files
RUN nest build test-service
# Bundle app source
COPY . .
EXPOSE 50054
CMD ["node", "dist/apps/test-service/main.js"]
以及nestcloud用来启动微服务的bootstrap-development.yaml
consul:
host: localhost
port: 8500
config:
key: mybackend/config/$ service.name
service:
discoveryHost: localhost
healthCheck:
timeout: 1s
interval: 10s
tcp: $ service.discoveryHost :$ service.port
maxRetry: 5
retryInterval: 5000
tags: ["v1.0.0", "microservice"]
name: io.mybackend.test.service
port: 50054
loadbalance:
ruleCls: RandomRule
logger:
level: info
transports:
- transport: console
level: debug
colorize: true
datePattern: YYYY-MM-DD h:mm:ss
label: $ service.name
- transport: file
name: info
filename: info.log
datePattern: YYYY-MM-DD h:mm:ss
label: $ service.name
# 100M
maxSize: 104857600
json: false
maxFiles: 10
- transport: dailyRotateFile
filename: info.log
datePattern: YYYY-MM-DD-HH
zippedArchive: true
maxSize: 20m
maxFiles: 14d
我可以成功地从微服务容器ping consul容器:
docker exec -ti test-service ping consul
你觉得我的配置有什么问题吗?如果有,请告诉我如何让它工作?
【问题讨论】:
【参考方案1】:你试图从 NestJs 服务中使用 localhost 访问 consul,这是另一个容器,它的 localhost 与 consul localhost 不同,你需要使用容器名称作为主机访问 consule
将bootstrap-development.yaml
中的consul主机改为$ CONSUL_HOST
。
CONSUL_HOST
定义在docker-compose.yaml
: CONSUL_HOST=consul
新bootstrap-development.yaml
:
consul:
host: $CONSUL_HOST
port: 8500
...
并使用以下命令重建容器:docker-compose --project-directory=. -f docker-compose.dev.yml up --build
【讨论】:
是的,看起来像。谢谢! 如果对您有帮助,请点击V接受答案以上是关于Nestcloud 与 docker-compose consul 微服务连接被拒绝的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 和 Kafka,Producer 抛出异常,key='null'
将 docker-compose 与 CI 结合使用 - 如何处理退出代码和守护程序链接容器?