【问题标题】:Spring Boot docker microservices restTemplate exceptionSpring Boot docker 微服务 restTemplate 异常
【发布时间】:2018-01-14 07:13:54
【问题描述】:

我正在尝试通过restTemplateSpring Boot 微服务之间的docker 发出rest 请求,但我收到错误消息。

docker-compose.yml:

  api:
    image: api-service
    container_name: api-service
    restart: always
    depends_on:
      - product
    ports:
      - 8081:8080
    links:
      - product:product
    environment:
      - SERVICE_PORT_PRODUCT=8083

  product:
    image: product-service
    container_name: product-service
    restart: always
    ports:
      - 8083:8080

Exception日志:

ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://product:8083/api/products/": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)] with root cause
java.net.ConnectException: Connection refused (Connection refused)

看起来正确:

对“http://product:8083/api/products/”的 POST 请求

为什么它不起作用?

【问题讨论】:

    标签: docker spring-boot resttemplate


    【解决方案1】:

    Networking in Compose 的官方文档中所述

    网络服务到服务通信使用 CONTAINER_PORT

    因此,当您想从一个容器向另一个容器发出请求时,您需要使用容器端口而不是主机端口。

    请求应该是:http://product:8080/api/products/ 从 api 容器到产品容器。

    【讨论】:

    • 谢谢,你救了我发疯:D
    猜你喜欢
    • 2018-03-16
    • 2019-02-11
    • 2019-09-06
    • 1970-01-01
    • 2018-06-17
    • 2021-06-07
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    相关资源
    最近更新 更多