繁体   English   中英

Nestcloud 与 docker-compose consul 微服务连接被拒绝

[英]Nestcloud with docker-compose consul micro-service connection refused

我已经用 consul 和一堆微服务对一个 Nestcloud 应用程序进行了 docker 化。 但是,我无法启动任何微服务(例如下面的 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 领事容器:

docker exec -ti test-service ping consul

你觉得我的配置有什么问题吗,如果有,请告诉我如何让它工作?

你查了吗? https://github.com/juicycleff/ultimate-backend/issues/152

通过从 consul:host:localhost 修改每个服务 bootstrap-development.yml 解决了这个问题

领事:主机:${{CONSUL_HOST}}

我有同样的问题,但这个解决方案对我不起作用。 错误仍然出现。 也许这会对你有所帮助。

您尝试从 NestJs 服务使用 localhost 访问 consul,这是另一个容器,它的 localhost 与 consul localhost 不同,您需要使用容器名称作为主机访问 consule

根据 Raz Luvaton 的建议,我在 bootstrap-development.yaml 中将 consul host 更改为 ${{ CONSUL_HOST }}。

CONSUL_HOST 在 docker-compose.yaml 中定义: CONSUL_HOST=consul

新的引导开发。yaml:

consul:
  host: ${{CONSUL_HOST}}
  port: 8500
...

并使用以下命令重建容器:docker-compose --project-directory=。 -f docker-compose.dev.yml up --build

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM