繁体   English   中英

无法发现Docker组成的链接微服务

[英]Docker Compose Linked Microservices Not Discoverable

当使用docker compose时,我无法获得两项服务以相互通信。 这个想法是让Node.JS服务与Java / Spring服务对话。

我尝试使用“链接”,将它们放入相同的“网络”,然后将请求发送到http:// service_name:port ,它返回ERR_NAME_NOT_RESOLVED。

在Node.JS服务中,我尝试使用Axios和http模块,但均无效(ERR_NAME_NOT_RESOLVED)。 我也尝试过'localhost',它也不起作用(ERR_CONNECTION_REFUSED)。 实际上,Spring服务只是一个公开的REST端点(据我所知,这是可行的,因为我可以直接访问它)。

我也尝试通过环境变量(例如,在docker-compose.yml内部)传递对服务的引用。

environment:
  - SERVICE_HOST=serviceB

在下面调用时,Node.JS项目内的env变量未定义

process.env.SERVICE_HOST

我将Windows与Docker工具箱一起使用,但也在Ubuntu VM中尝试了相同的项目。

  serviceA:
    build: ./serviceA/
    ports: 
      - "8080:8080"
    networks:
      - my_network
  serviceB:
    build: ./serviceB/
    ports:
      - "9003:9003"
    networks:
      - my_network

networks:
  my_network:
    driver: bridge
axios.get("http://serviceB:9003/test")
  .then(function(res){
    console.log(res);
  })

我希望Node.JS服务中的console.log语句响应ServiceB的rest调用的结果,而不是错误消息。

我是使用docker-compose的新手,所以我期望这里只是缺少一些明显的东西,但是我无法在网上找到能够解决我的问题或类似问题的任何东西。

编辑添加了整个yaml文件和错误,以更好地理解。

version: '3'
services:

  # Consul
  consul:
    container_name: consul
    image: consul:latest
    ports:
      - "8500:8500"
      - "8300:8300"
    hostname: consul
    networks:
      - front_end
      - back_end

  # Actor Service   
  actor_service:
    build: ./actor_service/
    ports: 
      - "9001:8080"
    depends_on:
      - actor_service_db
      - consul
    networks:
      - back_end

  actor_service_db:
    image: postgres:9.4.5
    depends_on:
      - consul
    networks:
      - back_end
    environment:
      - POSTGRES_DB=actor
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password
      - CONSUL_HTTP_ADDR=https://consul:8500

  # Movie Service   
  movie_service:
    build: ./movie_service/
    ports: 
      - "9002:8080"
    depends_on:
      - movie_service_db
      - consul
    networks:
      - back_end

  movie_service_db:
    image: postgres:9.4.5
    depends_on:
      - consul
    networks:
      - front_end
    environment:
      - POSTGRES_DB=movie
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password
      - CONSUL_HTTP_ADDR=https://consul:8500

  # Movie Aggregate Service
  movie_aggregate_service:
    container_name: movie_aggregate_service
    build: 
      context: ./movie_aggregate_service/
    ports: 
      - "9003:8080"
    depends_on:
      - consul
      - movie_service
      - actor_service
    networks:
      - front_end
      - back_end
    hostname: movie_aggregate_service

  # Frontend
  front_end:
    build: ./front_end/
    ports: 
      - "8080:8080"
    networks:
      - front_end
    environment:
      - "CONSUL_HOST=consul"
    links:
      - consul
      - movie_aggregate_service

networks:
  front_end:
    driver: bridge
  back_end:
    driver: bridge

浏览器控制台错误和输出

您的撰写看起来不错-服务发现依赖于容器内的DNS,我尝试将execing( exec )放入serviceA,并检查/etc/resolv.conf没发生任何时髦事件。.您应该能够从exec ping serviceB贝壳。

您可能希望在每个服务上指定一个主机名,以简化对驻留在它们上的端点的访问并显式链接它们。 下面是一个示例Docker-compose文件。

version: "3"

services:
  ocelot.products:
    image: pogs/ocelot-products
    container_name: ocelot-products
    hostname: ocelot.products
    build:
      context: ./products
    ports:
      - "52790:3000"
    environment:
      "PUBLIC_PORT": "52790"
  ocelot.users:
    image: pogs/ocelot-users
    container_name: ocelot-users
    hostname: ocelot.users
    build:
      context: ./users
    ports:
      - "52791:3000"
    environment:
      "PUBLIC_PORT": "52791"
  ocelot.transactions:
    image: pogs/ocelot-transactions
    container_name: ocelot-transactions
    hostname: ocelot.transactions
    build:
      context: ./transactions
    ports:
      - "52792:3000"
    environment:
      "PUBLIC_PORT": "52792"
  ocelot.gateway:
    image: pogs/ocelot-gateway
    container_name: ocelot-gateway
    build:
      context: ./gateway/EShop
    ports:
      - "52793:80"
    links:
      - ocelot.products
      - ocelot.users
      - ocelot.transactions
    depends_on:
      - ocelot.products
      - ocelot.users
      - ocelot.transactions

整个仓库在这里

暂无
暂无

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

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