簡體   English   中英

多容器 Docker 應用程序 - 容器之間的連接被拒絕

[英]Multi Container Docker app - Connection Refused between containers

所以我有一個多容器應用程序,前端、后端 API 都位於 API 網關后面。 目前,通過各自的命令(Java spring 應用程序和 Angular 前端)獨立啟動時,所有應用程序都可以正常工作。 但是,當我通過docker-compose up啟動應用程序時,沒有一個應用程序可以相互通信(連接被拒絕)。

Gateway 只是一個基本的 spring-cloud-gateway starter 應用程序,它將請求路由到正確的應用程序。 這是使用以下代碼配置的:

    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("api", route -> route.path("/api/**").uri("http://localhost:5001"))
                .route("front-end", route -> route.path("/**").uri("http://localhost:4200"))
                .build();
    }

錯誤信息

在向http://localhost:5000/api/categories發送 HTTP GET 請求后,網關應用程序中產生了此錯誤消息。

api-gateway_1      | 2021-01-11 00:05:14.514 ERROR 1 --- [or-http-epoll-5] a.w.r.e.AbstractErrorWebExceptionHandler : [d29e1cbf-1]  500 Server Error for HTTP GET "/api/categories"
api-gateway_1      | 
api-gateway_1      | io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:5001
api-gateway_1      |    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
api-gateway_1      | Error has been observed at the following site(s):
api-gateway_1      |    |_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
api-gateway_1      |    |_ checkpoint ⇢ HTTP GET "/api/categories" [ExceptionHandlingWebHandler]
api-gateway_1      | Stack trace:
api-gateway_1      | Caused by: java.net.ConnectException: finishConnect(..) failed: Connection refused
api-gateway_1      |    at io.netty.channel.unix.Errors.throwConnectException(Errors.java:124) ~[netty-transport-native-unix-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.channel.unix.Socket.finishConnect(Socket.java:251) ~[netty-transport-native-unix-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.doFinishConnect(AbstractEpollChannel.java:673) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.finishConnect(AbstractEpollChannel.java:650) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.epollOutReady(AbstractEpollChannel.java:530) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:470) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:378) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1      |    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1      |    at java.base/java.lang.Thread.run(Unknown Source) ~[na:na]

嘗試的步驟/更多信息

我嘗試定義自己的網絡,但這並沒有改變任何東西,所以我將容器留在了運行docker-compose up時創建的默認網絡上。

我可以通過cURL或 Postman 獨立訪問每個應用程序,並且可以通過瀏覽器訪問前端。

Docker-Compose.yml

version: "3"

services:
  api:
    build: ./api
    ports:
      - "5001:5001"
  gateway:
    build: ./gateway
    ports:
      - "5000:5000"
  frontend:
    build: ./frontend
    ports:
      - "4200:80"

Dockerfiles

網關

FROM openjdk:11 as build

COPY . .

RUN ./gradlew build --parallel

FROM openjdk:11-jre-slim as runtime

COPY --from=build /build/libs/gateway-0.0.1-SNAPSHOT.jar /usr/app/

WORKDIR /usr/app

ENTRYPOINT ["java", "-jar", "gateway-0.0.1-SNAPSHOT.jar"]

API

FROM openjdk:11 as build

COPY . .

RUN ./gradlew build --parallel

FROM openjdk:11-jre-slim as runtime

COPY --from=build /build/libs/api-0.0.1-SNAPSHOT.jar /usr/app/

WORKDIR /usr/app

ENTRYPOINT ["java", "-jar", "api-0.0.1-SNAPSHOT.jar"]

前端

FROM node:12.7-alpine AS build

WORKDIR usr/src/app

COPY package.json package-lock.json ./

RUN npm install

COPY . .

RUN npm run build

FROM nginx:1.17.1-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist/awards-frontend /usr/share/nginx/html

感謝@DavidMaze讓我注意到這一點。

好像我有點傻。 我的應用程序正在向 localhost 發送網絡請求。 當它們都在容器外運行時,這可以正常工作。 在容器中時,他們需要向其他容器的名稱發送請求。 例如:

app_1 在 8080 端口上運行 app_2 在 5000 端口上運行

在 docker 之外運行時,app_1 可以通過http://localhost:5000向 app_2 發送網絡請求。 這在容器內部不起作用,因為該容器中的localhost:5000上沒有運行任何內容。 相反,它需要引用其他容器,例如: http://app_2:5000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM