+-
Docker撰写的链接微服务不可发现

当使用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的休息调用的结果,而不是错误消息。

我是使用docker-compose的新手,所以我希望我在这里找不到明显的东西,但我找不到能解决我在线问题的任何东西,也没有在SO上找到类似的问题。

编辑添加了整个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

Browser Console errors and output

0
投票

您可能希望在每个服务上指定主机名,以便于访问生活在其上的端点并明确链接它们。下面是一个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

整个回购可用here

0
投票

你的作品看起来很好 - 服务发现依赖于容器内的DNS,我会尝试将(exec)放入serviceA,并检查/etc/resolv.conf没有发生任何时髦的事情..你应该能够从exec shell ping serviceB。